C Program to Sort Numbers in Ascending Order using QUICK  SORT


#include <stdio.h>
void quick_sort (int [], int, int);
int main()
{
int array[50];
int size, i;

printf("Enter the number of elements:\n");
scanf("%d", &size);
printf("Enter the elements to be sorted:\n");
for (i = 0; i < size; i++)
{
scanf("%d", &array[i]);
}
quick_sort(array, 0, size - 1);
printf("After quick sort the sorted elements are \n");
for (i = 0; i < size; i++)
{
printf("%d ", array[i]);
}
printf("\n");

return 0;
}
void quick_sort(int array[], int low, int high)
{
int pivot, i, j, temp;
if (low < high)
{
pivot = low;
i = low;
j = high;
while (i < j)
{
while (array[i] <= array[pivot] && i <= high)
{
i++;
}
while (array[j] > array[pivot] && j >= low)
{
j--;
}
if (i < j)
{
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}

temp = array[j];
array[j] = array[pivot];
array[pivot] = temp;
quick_sort(array, low, j - 1);
quick_sort(array, j + 1, high);
}
}