Quick Sort in C
Here you will know the logic of Quick Sort and get an easy example program for quick sort in c language.
Quick Sort is an efficient sorting algorithm that is based on the divide-and-conquer approach. It is a type of sorting algorithm that works by partitioning a list of items into two sub-lists. It then recursively sorts the sub-lists until the base case is reached. Quick Sort is considered to be one of the fastest sorting algorithms and is often used in applications where speed is critical.
Quick Sort Animation
Quick Sort in C Program
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | //quick sort program in c #include<stdio.h> #include<conio.h> #define max 100 int a[max],n,i,l,h; int main() { void quick_sort(int a[],int i,int h); void printarray(int a[],int n); printf("Enter Number of elements :"); scanf("%d",&n); printf("\n"); printf("Enter value of elements :\n"); for(i=0;i<=n-1;i++) { scanf("%d",&a[i]); } fflush(stdin); i=0; h=n-1; quick_sort(a,i,h); printf("\n Sorted array:\n"); printarray(a,n); fflush(stdout); getch(); } void quick_sort (int a[],int i,int h) { int temp,key,low,high; low=i; high=h; key=a[(low+high)/2]; do { while(key >a[low]) { low++; } while(key<a[high]) { high--; } if(low<=high) { temp=a[low]; a[low++]=a[high]; a[high--]=temp; } } while(low<=high); if(l<high) quick_sort(a,l,high); if(low<h) quick_sort(a,low,h); } void printarray(int a[],int n) { for(i=0;i<=n-1;i++) { printf(" %d\n",a[i]); } } |
Output
Read Also
Check out our DAA Lab Manual and Programs