/* 题目:快速排序 */ /* 思路:将一个数组分为两份,左边的数字小于index,右边的数字大于index,递归划分后形成一个排序后的数组。 */ void QuickSort(int data[],int length,int start,int end){ if(start == end) return; int index = Partition(data,length,start,end); if(index > start){ QuickSort(data,length,start,index-1); } if(index < end){ QuickSort(data,length,index+1,end); } } int Partition(int data[],int length,int start,int end){ if(data == null || length < 0 || start < 0 || end >= length){ throw new std::exception("invalid Parameters"); } int index = RandomInRange(start,end); Swap(&data[index],&data[end]); int small = start-1; for(int index = start; index < end; index++){ if(data[index] < data[end]){ small++; if(small != index){ Swap(&data[small],&data[index]); } } } small++; Swap(&data[small],&data[end]); return small; }