简体   繁体   中英

(segmentation fault) core dump in quicksort code in c

I have made code for Quicksort .It works well in some cases but in most of the cases it causes core dump problem.mostly cases are long input >10 , already big sorted array. why it is happening ?

This is my code.

#include<stdio.h>

void quicksort(int arr[],int s,int l)
{
    int temp;

    if(l-s <= 1) return ;

    int i=s+1,j=s+1;

    for(i;i<l;i++)
    {
        if(arr[i]<=arr[s])
        {
            temp = arr[i];
            arr[i] = arr[j];
            arr[j] = temp;
            j++;
        }
    }

    temp = arr[j-1];
    arr[j-1] = arr[s];
    arr[s] = temp;  
    quicksort(arr,s,j);
    quicksort(arr,j,l);
}
int main()
{
    int arr[50],n,i;
    scanf("%d",&n);

    for(i=0;i<n;i++)
    scanf("%d",&arr[i]);

    quicksort(arr,0,n);
for(i=0;i<n;i++)
    printf("\n%d\n",arr[i]);

}

Correct the line (remove '=')

if(arr[i]<=arr[s])

to

if(arr[i]<arr[s])

It is going into an inifinite loop and hence causing a stack over flow.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM