Link to home
Start Free TrialLog in
Avatar of jovanvliet
jovanvliet

asked on

Counting # of comparisons in a quick sort and printing them

For part of a homework, my program has to output the number of comparisons of data elements made by a quick sort and an insertion sort.  I got the insertion sort to work. Not so the quick sort.

I have added a counter to the quickSort method provided, but it is in a loop and just keeps printing forever. I have tried putting the counter elsewhere and get an "unreachable" error.  
Would you please look at my code and tell me where I have messed up?
I can provide the program where I call the quickSort function if that helps.

Many, many thanks for helping me get this straight!!
 Jo


//Quick sort algorithm.
  //Postcondition: list objects are in ascending order.
  public void quickSort(T[] list, int length)
  {
    recQuickSort(list, 0, length - 1);
  }//end quickSort
  
  //Method to partition the list between first and last.
  //The pivot is choosen as the middle element of the list.
  //This method is used by the recQuickSort method.
  //Postcondition: After rearranging the elements,
  //               according to the pivot, list elements
  //               between first and pivot location - 1,
  //               are smaller the the pivot and list
  //               elements between pivot location + 1 and
  //               last are greater than or equal to pivot.
  //               The position of the pivot is also
  //               returned.
  private int partition(T[] list, int first, int last)
  {
    T pivot;
    
    int smallIndex;
    int counter = 0;         //initialize counter
    swap(list, first, (first + last) / 2);
    
    pivot = list[first];
    smallIndex = first;
    
    for (int index = first + 1; index <= last; index++)
    { 
      Comparable<T> compElem = (Comparable<T>) list[index];
      counter=counter+1;        //increment counter
      
      if (compElem.compareTo(pivot) < 0)
      {
        smallIndex++;
        swap(list, smallIndex, index);
        
      }

    }

        System.out.print(counter);   // print out number of comparisons
     System.out.print("The total number of comparisons in the quickSort was  ");
    swap(list, first, smallIndex);
    
    return smallIndex;
        

} 
  //end partition

//Method to sort the elements of list between first
//and last using quick sort algorithm,
//Postcondition: list elements between first and last
//               are in ascending order.
private void recQuickSort(T[] list, int first, int last)
{
 
  if (first < last)
  {
    int pivotLocation = partition(list, first, last);
    recQuickSort(list, first, pivotLocation - 1);
    recQuickSort(list, pivotLocation + 1, last);
    
  }

}//end recQuickSort

Open in new window

Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Make 'counter' an instance variable and do
    //Quick sort algorithm.
    //Postcondition: list objects are in ascending order.
    public void quickSort(T[] list, int length) {
	recQuickSort(list, 0, length - 1);
	System.out.printf("The total number of comparisons in the quickSort was %d\n", counter);
    } //end quickSort

Open in new window

Avatar of jovanvliet
jovanvliet

ASKER

Thanks!
It looks like it would work, but when I paste it in and run the programs, it says it can't find counter (which it can't because counter get initialized later).
Have I just messed up copying your code into my program? I'll attach the folder with the SearchSortAlgorithms, SearchSortADT and my test file
ExamInsertionQuickSort4-files--2.zip
>>it says it can't find counter (which it can't because counter get initialized later).

It must be an instance variable of the class containing the sorting methods
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you!  Great solution! It works perfectly and I have learned something new.

Many, many thanks!!!
Jo
Great solution that worked perfectly and taught me a new bit about Java as well.
Hi,
I'm new at this.
I meant to assign 400 point to object for an outstanding solution and 100 point to CEHJ for making a great suggestion based onmy llimited input.

If I didn't get them assigned like that,would you let me know or fix it?

I REALLY appreciate the help!
>>I meant to assign 400 point to object for an outstanding solution and 100 point to CEHJ

I find that odd. The only difference between what i posted and objects posted is that he's repeated more of your code, and showed the instance variable, which i didn't think necessary.

>>If I didn't get them assigned like that,would you let me know or fix it?

I'll get the question reopened