Quicksort, also known as partition-exchange sort, uses these steps.
Choose any element of the array to be the pivot.
Divide all other elements (except the pivot) into two partitions.
- All elements less than the pivot must be in the first partition.
- All elements greater than the pivot must be in the second partition.
Use recursion to sort both partitions.
Join the first sorted partition, the pivot, and the second sorted partition.
voidswap(int*a,int*b){intt=*a;*a=*b;*b=t;}voidsort(intarr[],intbeg,intend){if(end>beg+1){intpiv=arr[beg],l=beg+1,r=end;while(l<r){if(arr[l]<=piv)l++;elseswap(&arr[l],&arr[--r]);}// swapping elements within the array // to avoid the memory allocation of more arrays.swap(&arr[--l],&arr[beg]);sort(arr,beg,l);sort(arr,r,end);}}