Need help in quick sort and coding

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]);
}

Provided comments below

#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; // j=left-1
     for(i=left;i<right-1;i++) //i<right
     {
     if(arr[i]<=pivot)
     {
     temp=arr[i];
     arr[i]=arr[j];
     arr[j]=temp;
     j++; // Move it to top, before storing arr[i] in temp
     }
     }
     
     temp=arr[j]; //change to j+1
     arr[j]=arr[right]; //change to j+1
     arr[right]=temp;
     printf("\nj is%d",j); //why this line?
     return j; // Change to j+1
    }

    void printarr(int arr[],int length)
    {
    for(int i=0;i<length;i++)
    printf("\n%d",arr[i]);
    }
1 Like

thanks a ton my friend for the help. Can you please tell me how i can improve and be better at this?

Practice is the only answer.

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.

1 Like

thank you,i will keep your advice in mind. It would be great if i can learn along with you if possible .

I work 10 hrs a day so I hardly get any time to practice, which is why Iā€™m just 2 stars :slight_smile:

But thanks for that!