On the wikipedia article about quicksort, there's a sketch of a particular way to code it (the "simplest"), which I attempted
#include <cstdlib>
#include <iostream>
using namespace std;
int partition(int list[10], int high, int low);
void quicksort(int list[10], int high, int low);
int partition(int list[], int high, int low){
int pivot = list[high];
int i = list[low];
int swap_temp;
for (int j=0; j<high; j++){
if (list[j]<pivot){
swap_temp=list[high];
list[high]=list[j];
list[j]=swap_temp;
i++;
}
}
swap_temp = list[high];
list[high]=list[i];
list[i]=swap_temp;
return i;
}
void quicksort(int list[], int high, int low){
if (low<high){
int p=partition(list, high, low);
quicksort(list,low,p-1);
quicksort(list,p+1,high);
}
}
int main() {
int list[10]={3,5,1,6,34,224,23,62,124,57};
int low=0, high=10;
quicksort(list,low,high);
for (int k=0; k<high; k++){
cout << list[k] << endl;
}
return 0;
}
This compliles fine, but the output is just the original list, completely untouched. I've looked around about passing arrays to functions, and what I've done here has worked in other programs (I specifically wrote one to check, which took an array of integers and doubled each entry, and it was fine). The algorithm is entirely specified here. Why doesn't the program alter the array?