I was trying out different sorting algorithms. I was interested to really see the difference. I tried it on an array of 10 integers and everything works nicely, but of course, the run time is negligible on small amounts of data. So I tried it on 100,000 integers, and thats where I started to notice which ones are faster than the others.
I believe quicksort is known to be faster than mergesort so I was wondering what the problem in my code was. Referenced from ( https://www.youtube.com/watch?v=COk73cpQbFQ&list=PL2_aWCzGMAwKedT2KfDMB9YA5DgASZb3U&index=7 ) sorting algorithm series.
void quickSort(int num[], int start, int end) {
if (start < end) {
printf("\n.");
int partitionIndex;
partitionIndex = randomizedPartition(num, start, end);
quickSort(num, 0, partitionIndex - 1);
quickSort(num, partitionIndex + 1, end);
}
}
int partition(int num[], int start, int end) {
int partitionIndex = start;
int pivot = end;
int i;
for (i = start; i < end; i++) {
if (num[i] <= num[pivot]) {
swap(num, i, partitionIndex);
partitionIndex++;
}
}
swap(num, partitionIndex, pivot);
return partitionIndex;
}
int randomizedPartition(int num[], int start, int end) {
int partitionIndex;
int pivot = (rand() % (end - start + 1)) + start;
swap(num, pivot, end);
partitionIndex = partition(num, start, end);
return partitionIndex;
}
the code above runs forever on an array of 100,000 integers while mergeSort
runs for 18 seconds on my computer.