I made a quicksort algorithm out of the visual presentation of the algorithm in youtube that I watched, but my recursion does not work at all. :( If I commented out these 2 lines,
quicksort(array,0,start-1);
quicksort(array,start+1,temp);
.. The program does not crash and the output becomes 2,1,3,5,4 which is partly correct.. But it crashes when it enters the recursion. After the whole while loop, the start becomes the same as the end..
#include <stdio.h>
#include <conio.h>
void swap(int *a, int *b){
int temp = *a;
*a = *b;
*b = temp;
}
void quicksort(int *array, int start, int end){
int pivot = start;
int temp = end;
while(start != end){
if(pivot==start){
if(array[pivot] > array[end]){
swap(&array[end],&array[pivot]);
pivot = end;
start++;
}
else
end--;
}
else{
if(array[pivot] < array[start]){
swap(&array[start],&array[pivot]);
pivot = start;
end--;
}
else
start++;
}
}
quicksort(array,0,start-1);
quicksort(array,start+1,temp);
}
main(){
int x[5] = {3,1,5,2,4};
int i;
quicksort(x,0,4);
for(i=0;i<5;i++)
printf("%d ", x[i]);
getch();
}