#include<stdio.h>#include<stdlib.h>#include<time.h>#define RANDOM_INIT() srand(time(NULL))#define RANDOM(L, R) (L + rand() % ((R) - (L) + 1)) // gen a random integer in [L, R]/**
* Email 956324914@qq.com
* Author xlf
*//**
* swap 2-element, orignal value
*/template<typename T>staticvoidswap(T &x, T &y){
T _t(x);
x = y;
y = _t;}/**
* the quick-sort partition routine
*/template<typename T>staticintpartition_(T list[],int begin,int end){int pivot_idx =RANDOM(begin,end);
T pivot = list[pivot_idx];swap(list[begin], list[pivot_idx]);int i = begin +1;int j = end;while(i <= j){while((i <= end)&&(list[i]<= pivot))
i++;while((j >= begin)&&(list[j]> pivot))
j--;if(i < j)swap(list[i],list[j]);}swap(list[begin],list[j]);return j;// final pivot position}/**
* quick sort an array of range [begin, end]
*/template<typename T>staticvoidquicksort(T list[],int begin,int end){if( begin < end){int pivot_idx = partition_<T>(list, begin, end);quicksort(list, begin, pivot_idx-1);quicksort(list, pivot_idx+1, end);}}/**
* print all of the elements in `list` with size `n`
*/template<typename T>staticvoidprintlist(T & list,int count){int i;for(i=0;i<count;i++)printf("%d\t ",list[i]);printf("\n");}intmain(){RANDOM_INIT();constint MAX_ELEMENTS =10;int list[MAX_ELEMENTS];int i =0;srand(time(NULL));// generate random numbers and fill them to the listfor(i =0; i < MAX_ELEMENTS; i++){
list[i]=rand()%100;}printf("The list before sorting is:\n");printlist(list,MAX_ELEMENTS);// sort the list using quicksortquicksort(list,0,MAX_ELEMENTS-1);// print the resultprintf("The list after sorting using quicksort algorithm:\n");printlist(list,MAX_ELEMENTS);return0;}