How to write a C Program to Sort Array using Quick Sort with a practical example?.
C Program for Quick Sort Example
This quick sort program in C allows the user to enter the array size and the row elements of an Array. Next, we are using Nested For Loop to order or arrange the array elements using a quick sort.
Here, in this C program for quick sort, we separated the logic using Functions and Pointers. The function will swap and quick sort the Array elements.
#include <stdio.h> void Swap(int *x, int *y) { int Temp; Temp = *x; *x = *y; *y = Temp; } void quickSort(int a[], int first, int last) { int pivot, i, j; if(first < last) { pivot = first; i = first; j = last; while (i < j) { while(a[i] <= a[pivot] && i < last) i++; while(a[j] > a[pivot]) j--; if(i < j) { Swap(&a[i], &a[j]); } } Swap(&a[pivot], &a[j]); quickSort(a, first, j - 1); quickSort(a, j + 1, last); } } int main() { int a[100], number, i; printf("\n Please Enter the total Number of Elements : "); scanf("%d", &number); printf("\n Please Enter the Array Elements : "); for(i = 0; i < number; i++) scanf("%d", &a[i]); quickSort(a, 0, number - 1); printf("\n Selection Sort Result : "); for(i = 0; i < number; i++) { printf(" %d \t", a[i]); } printf("\n"); return 0; }
Refer to C programs, Functions, Pointers, swap, and Array articles in C Programming.
