Is this code for quicksort using the first index as a pivot correct? Also if I want to use the last or middle index as a pivot how would the whole code change?
#include <iostream>
using namespace std;
void quicksort(int list[], int arraySize);
void quickSort(int list[], int first, int last);
int partition(int list[], int first, int last);
void quickSort(int list[], int arraySize) {
quickSort(list,0, arraySize - 1);
}
void quickSort(int list[], int first, int last) {
if (first < last) {
int pivotIndex = partition(list, first, last);
quickSort(list, first, pivotIndex -1);
quickSort(list, pivotIndex + 1, last);
}
}
int partition(int list[],int first, int last) {
int pivot = list[first];
int low = first + 1;
int high = last;
do {
while(low <= high && list[low] <= pivot)
low++;
while(low <= high && list[high] > pivot)
high--;
if (low < high) {
int temp = list[low];
list[low] = list[high];
list[high] = temp;
}
} while (low <= high);
int temp = list[high];
list[high] = list[first];
list[first] = temp;
return high;
}