+ 1

quick sort: idk why this program is showing error

// Online C compiler to run C program online #include <stdio.h> int swap(int *x,int *y){ int temp; temp=*x; *x=*y; *y=temp; } int pivot(int arr[],int first,int last) { int pivot=arr[first]; int left=first+1; int right=last; while(left<right){ while(left<right && arr[left]<pivot){ left++; } while(left<right && arr[right]>pivot){ right--; } if(right<left)//done to isolate the break; else swap(&arr[left],&arr[right]); } swap(&arr[first],&arr[right]); return right; } int quick_sort(int arr[],int first,int last) { int p=pivot(arr,first,last); quick_sort(arr,p+1,last); quick_sort(arr,first,p-1); } int main() { int a[]={19,23,7,21,27,17,30,3,1,15}; int size=sizeof(a)/sizeof(a[0]); quick_sort(a,0,size-1); return 0; }

7th Jan 2023, 7:33 AM
Dipankar Verma
Dipankar Verma - avatar
1 Respuesta
+ 4
Your quick_sort function calls itself infinitely. When you write recursive functions, always create a base case which tells the program when the recursion must stop.
7th Jan 2023, 8:36 AM
Tibor Santa
Tibor Santa - avatar