Help with Quicksort

In the quick sort algorithm, how would I choose a "pivot"? My assignment is to count how many comparisons the quick sort algorithm makes but I first need to implement part of the quick sort. This is what was given:

1
2
3
4
5
6
7
8
9
/** Partitions an array for quicksort. */
void partition(int theArray[],
               int first, int last, int& pivotIndex)
{
   // place pivot in theArray[first]
   choosePivot(theArray, first, last);
   int pivot = theArray[first];     // copy pivot
...
}

but the choosePivot function was not entirely given, only this was:
1
2
3
4
5
6
7
/** Chooses a pivot for quicksort's partition algorithm and swaps */
void choosePivot(int theArray[], int first, int last){

// what should be done here?

}

Don't shy away dude, just try a place in the array and see what you get. You might be surprised. :-)
Last edited on
Topic archived. No new replies allowed.