I am trying to implement quicksort in C but I am facing difficulty in it. I have tried to debug multiple time and referred and matched my code with code available online and I am still not able to figure out the error. Also, I had a similar difficulty when I tried to implement merge sort and keep on getting sigsegv. This is really affecting my learning and any advice would mean a lot.
please do help
#include <stdio.h>
void printarr(int [],int);
void quickSort(int [],int,int);
int partion(int arr[],int start,int end);
int main(void) {
int arr[10];
int len;
printf("\n Enter the number of elements");
scanf("%d",&len);
for(int k=0;k<len;k++)
scanf("%d",&arr[k]);
printf("\nThe array is :");
printarr(arr,len);
quickSort(arr,0,len-1);
printf("\nThe Sorted array is :");
printarr(arr,len);
return 0;
}
void quickSort(int arr[],int start,int end)
{
int pivotPoint;
if(start<end)
{
pivotPoint=partion(arr,start,end);
quickSort(arr,start,pivotPoint-1);
quickSort(arr,pivotPoint+1,end);
}
}
int partion(int arr[],int left,int right)
{
int i,j,temp;//i=current element j=last swapped element
int pivot=arr[right];
j=left;
for(i=left;i<right-1;i++)
{
if(arr[i]<=pivot)
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
j++;
}
}
temp=arr[j];
arr[j]=arr[right];
arr[right]=temp;
printf("\nj is%d",j);
return j;
}
void printarr(int arr[],int length)
{
for(int i=0;i<length;i++)
printf("\n%d",arr[i]);
}
GeeksforGeeks is a treasure of various DS and Algorithm courses and questions. Their solutions are provided in C, C++, Java and Python. They also have courses on Programming Languages and CS Subjects. That site is strongly recommended for building a good base.
For problem solving, you have Codeforces, Codechef, Hackerearth, SPOJ etc.