In this tutorial you will learn about the Quick Sort Algorithm and its application with practical example.
Quick Sort Program In C
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 |
#include<stdio.h> void quicksort(int number[25],int first,int last){ int i, j, pivot, temp; if(first<last){ pivot=first; i=first; j=last; while(i<j){ while(number[i]<=number[pivot]&&i<last) i++; while(number[j]>number[pivot]) j--; if(i<j){ temp=number[i]; number[i]=number[j]; number[j]=temp; } } temp=number[pivot]; number[pivot]=number[j]; number[j]=temp; quicksort(number,first,j-1); quicksort(number,j+1,last); } } int main(){ int i, n, nums[30]; printf("Enter number of elements: "); scanf("%d",&n); printf("Enter %d integer numbers\n", n); for(i=0;i<n;i++) scanf("%d",&nums[i]); quicksort(nums,0,n-1); printf("Elements after sorting: "); for(i=0;i<n;i++) printf(" %d",nums[i]); return 0; } |
Output:-