**This is the C code for Quick sort algorithm, I’m facing errors if I give input for ‘n’ above a certain limit (mostly above 40). It just stops midway. **
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
void swap(int *a, int *b)
{
int t;
t = *a;
*a = *b;
*b = t;
}
int partition(int a[], int low, int high)
{
int i,j,pivot,t;
pivot = a[low];
i = low;
j = high;
while (i<j) {
while (a[i] < pivot)
i=i+1;
while(a[j] > pivot)
j=j-1;
if(i<j) {
swap(&a[i],&a[j]);
}
}
swap(&pivot,&a[j]);
return j;
}
void quicksort(int a[], int low, int high)
{
if (low < high)
{
int p = partition(a, low, high);
quicksort(a, low, p-1);
quicksort(a, p+1, high);
}
}
void main()
{
int n,a[5000];
clock_t s,e;
printf("Enter number of elements: ");
scanf("%d", &n);
printf("Before sorting:");
for (int i = 0; i < n; i++) {
a[i] = rand() % 100;
printf("%d ",a[i]);
}
s = clock();
quicksort(a, 0, n - 1);
e = clock();
double time_taken = (double)(e-s)/CLOCKS_PER_SEC;
printf("Time taken to sort %d elements: %f secondsn", n, time_taken);
printf("After sorting:");
for(int i = 0; i < n; i++)
printf("%dt",a[i]);
}
I can’t figure out why isn’t this working for inputs above 40. Please can anyone solve this.
The errors I face are,
Enter number of elements: 50
Before sorting:
It would stop right there.