您的位置:首页 > 理论基础 > 数据结构算法

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

2012-11-10 12:43 429 查看
/*
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);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐