微软等数据结构+算法面试100题(12)--快速排序

来源:互联网 发布:死亡岛低配置优化补丁 编辑:程序博客网 时间:2024/04/30 11:37
/*3.快速排序(东软喜欢考类似的算法填空题,又如堆排序的算法等)*/int Parition(int *p,int low,int high){int pivotkey=p[low];while(low<high){while((low<high)&&(p[high]>=pivotkey))//这里相等不能丢掉high--;p[low]=p[high];while((low<high)&&(p[low]<=pivotkey))low++;p[high]=p[low];}p[low]=pivotkey;return low;}void QuickSort(int *p,int low,int high){if(low>=high)return;int pivot=Parition(p,low,high);QuickSort(p,low,pivot-1);QuickSort(p,pivot+1,high);}
void QuickSortTest(){int p[]={10,0,15,12,-10,-5,9,2,8,6,4,20,19,6,8,7};int len=sizeof(p)/sizeof(int);cout<<"the array : ";ShowArray(p,len);cout<<"after sort : ";QuickSort(p,0,len-1);ShowArray(p,len);}


原创粉丝点击