Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

So I have a java program where in my main method the program reads from a text f

ID: 3664173 • Letter: S

Question

So I have a java program where in my main method the program reads from a text file and fills an array of ints.

I then set it up so that a static method from a different class is called, which sorts this array via bubbleSory, insertionSort, etc..

Within these sorting algorithms I have also added a counter that keeps track of the number of swaps/comparisons that has taken place.

Now, back in the main method I want to be able to print out the value of this counter.....but how do I access this value?

Explanation / Answer

/**The java program that demonstrates the comparisions
* made in bubble and insertion sort and prints the
* number of comparisions .*/
//ComparisionsCounter.java
public class ComparisionsCounter
{
   public static void main(String[] args)
   {
       //an array of unsorted elements
       int arr[]={5,3,6,8,1,2};

       //call bubbleSort
       int bubbleCounter=bubbleSort(arr);
       System.out.println("Bubble sort");
       System.out.println("Number of comparisions : "+bubbleCounter);
       //call insertionSort
       int insertionCounter=insertionSort(arr);
       System.out.println("Insertion sort");
       System.out.println("Number of comparisions : "+insertionCounter);

   }

   //Returns the number of comparisions made in insertion sort
   private static int insertionSort(int[] arr)
   {
       int comparisions = 0;
       for(int i = 1; i < arr.length; i++)
       {          
           int j = i;                       
           // compare i with sorted elements and insert it
           // sorted element
           while (j > 0 && arr[j] < arr[j - 1])
           {              
               int temp = arr[j];
               arr[j] = arr[j - 1];
               arr[j - 1] = temp;
               comparisions++;
               j--;
           }
           comparisions++;
       }
       //return comparision
       return comparisions;
   }
   //Returns the number of comparisions made in bubble sort
   private static int bubbleSort(int[] arr)
   {  
       int comparisions=0;  
       for(int i = 0;i<arr.length;i++)
       {
           for(int j = 0;j<arr.length-1;j++)
           {
               //increment the comparisions by one
               comparisions++;          
               if(arr[j]>arr[j+1])
               {
                   int temp=arr[j];
                   arr[j]=arr[j+1];
                   arr[j+1]=temp;                                 
               }              
           }
       }
       return comparisions;
   }
}

-----------------------------------------------------------------------------------------------

Note: Return the counter value from the static method will give the number of comparisions in for the sorting.

----------------------------------------------------------------------------------------------------

sample outut:

Bubble sort
Number of comparisions : 30
Insertion sort
Number of comparisions : 5

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote