Reputation: 616
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]);
}
Upvotes: 0
Views: 167
Reputation: 155
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.
Upvotes: 4