#include < stdio.h > void quicksort(int arr[],int left,int right); int main() { int a[25],n,i; printf("How many elements : "); scanf("%d",&n); for(i=0;i < n;i++) a[i]=rand()%100; for(i=0;i<n;i++) printf(" %d \t",a[i]); a[n]=500; quicksort(a,0,n-1); printf("\n sorted array is : \n"); for(i=0;i<n;i++) printf(" %d \t",a[i]); getch(); return 0; } void quicksort(int arr[],int left,int right) { int i,j,pivot,temp; if(left<right) { int i=left,j=right+1; pivot=arr[left]; do { do i++; while(arr[i]<pivot); do j--; while(arr[j]>pivot); if(i<j) { temp=arr[i]; arr[i]=arr[j]; arr[j]=temp; } }while(i<j); temp=arr[left]; arr[left]= arr[j]; arr[j]=temp; quicksort(arr,left,j-1); quicksort(arr,j+1,right); } }
Quick sort function is invoked with Left and Right boundaries 0,n-1.Initially we set PIVOT element is a[Left] and the function go forward and array is divided into 2 parts and quicksort function is called for both of the parts.For each part a separate sorting is made and again these parts are divided into sub parts recursively.
Quick sort technique Time complexity :
Best case : O(n)
Average case : O(n log n)
Worst case : O(n x n)
To sort a[1:n] the function invocation is quicksort(a,0,n-1)and Quick sort assumes that a[n] has been set a value at least as large as remaining data to sorted in array.I have used a[n] is 500 because array value never be over by 100.