Quick Sort
This is a one kind of sorting technique. The complexity of this sorting technique is,Worst-case performance : O(n^2)
Best-case performance : O(n log n)
Average-case performance : O(n log n)
For more details click here
Code
/******Quick Sort*****/ #include<stdio.h> #include<malloc.h> #include<stdlib.h> //Function definition int* quick_sort(int *arr,int low,int high){ int pivot,i,temp,j; if(low<high) { pivot=arr[low]; i=low; j=high; } else return arr; while(i<j){ while((arr[i]<=pivot)&&(i<high)) i++; while((arr[j]>=pivot)&&(j>low)) j--; if(i<j){ temp=arr[i]; arr[i]=arr[j]; arr[j]=temp; } } temp=arr[low]; arr[low]=arr[j]; arr[j]=temp; quick_sort(arr,low,j-1); quick_sort(arr,j+1,high); } //Main function started int main(){ int *arr,n,i; while(1){ //Taking the number of element printf("\nEnter the number of element you want to store:"); scanf("%d",&n); if(n<=0){ printf("\nAn array size must be a positive integer."); continue; } else break; } //Creating the array dynamically arr=(int*)malloc(n*sizeof(int)); if(!arr) { printf("\nNot enough memory."); exit(0); } printf("\nEnter the element(s) in the array:"); for(i=0;i<n;i++){ printf("\nElement[%d]:",i+1); scanf("%d",&arr[i]); } printf("\nThe element(s) before sorting:"); for(i=0;i<n;i++) printf(" %d",arr[i]); printf("\nThe element(s) after sorting:"); arr=quick_sort(arr,0,n-1); for(i=0;i<n;i++) printf(" %d",arr[i]); return 0; }
Output
Enter the number of element you want to store:7 Enter the element(s) in the array: Element[1]:5 Element[2]:6 Element[3]:2 Element[4]:88 Element[5]:554 Element[6]:11 Element[7]:36 The element(s) before sorting: 5 6 2 88 554 11 36 The element(s) after sorting: 2 5 6 11 36 88 554