-1

I was working on QUICKSORT and I wrote this whole program by myself. The code is as follows

#include<stdio.h>
#include<stdlib.h>
void quicksort(int*,int,int);
int partition(int*,int,int);
swap(int*,int*);
int main()
{
    int *arr,n,i;
    printf("\nEnter the size of the array");
    scanf("%d",&n);
arr=(int*)malloc(n*sizeof(int));
printf("\nEnter the elements of the array one by one");
for(i=0;i<n;i++)
{
    scanf("%d",arr+i);
}    
quicksort(arr,0,n-1);
printf("\nThe Sorted Array is as follows");
for(i=0;i<n;i++)
{
    printf("%d",*(arr+i));
}
return 0;
}

void quicksort(int* arr,int a,int b)
{   
    int c;
        if(a<b)
        c=partition(arr,a,b);
    quicksort(arr,a,c-1);
    quicksort(arr,c+1,b);
}
int partition(int* arr,int a,int b)
{
    int x,y,index=*(arr+b);
    x=a,y=b-1;
    while(x<y)
    {
        if(*(arr+x)<index)
        {
            x++;

        }
        if(index<*(arr+y))
        {
            y--;

        }
        swap((arr+x),(arr+y));
    }
    swap((arr+x),(arr+b));

    return x;
}

swap(int *a,int *b)
{
    int temp;
    temp=*a;
    *a=*b;
    *b=temp;
}

I tried to run this program but after the array input it says segmentation error. I spent few minutes looking at the code and checked the code again and again but I do not seem to move anywhere. can someone tell me where is the mistake.

  • 2
    Have you tried using a debugger? https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems – João Neto Mar 17 '19 at 19:07
  • 5
    `quicksort(arr,0,n);` ==> `quicksort(arr, 0, n - 1);` – pmg Mar 17 '19 at 19:07
  • @pmg, I corrected that but its still not working – infiniteloops Mar 17 '19 at 19:11
  • A faintly modern C compiler (one from the 21st Century) should be complaining about the declaration and definition of `swap()`. You should give an explicit return type (`void`). And you should turn on compiler warnings and heed what it says (because the compiler knows a lot more about C than you do). However, that said, it is unlikely that the missing `void` is directly the cause of your troubles, but you should take pride in your code compiling without warnings. – Jonathan Leffler Mar 17 '19 at 19:19
  • 1
    It's also puzzling to see you writing the longhand `*(arr+x)` notation instead of `arr[x]`. Granted there's no difference in the machine code; there is in the readability. – Jonathan Leffler Mar 17 '19 at 19:20
  • How have you attempted to debug your code? Have you added print statements to ensure that your partition code is producing the expected result? **More significantly** what is the value of `c` when you recurse after you find that `a >= b`? I don't know — neither do you. That's because `c` is uninitialized. Maybe you should use `if (a >= b) return;`? There could be other problems, but that's going to lead to crashes etc. – Jonathan Leffler Mar 17 '19 at 19:23
  • well,It doesn't give me any warning. Also regarding that swap I will keep that in mind. But adding void doesnt give me a correct output. – infiniteloops Mar 17 '19 at 19:24

1 Answers1

0

What should be happening if a is greater than or equal to b? this should solve the segmentation problem.

void quicksort(int* arr, int a, int b) {
    int c;
    if (a < b) {
        c = partition(arr,a,b);
        quicksort(arr, a, c-1);
        quicksort(arr, c+1, b);
    }
}
Srikanth Chekuri
  • 1,944
  • 1
  • 9
  • 19