当年抓破头皮没想出来,溜了一眼算法导论豁然开朗。其实就是排以前交换一下pivot。
#include <stdlib.h>
#include <time.h>
#include <stdio.h>
typedef int Record; 
void QuickSort (Record r[],int s,int t);
void QuickSort (Record r[],int s,int t)
...{
int low=s;
int high=t;
Record pivot;
...{
int t,tn;
srand(time(NULL));
tn=rand()%(high-low+1)+low;
t=r[low];
r[low]=r[tn];
r[tn]=t;
}
pivot=r[s];
while (low<high)...{
while (low<high && r[high]>pivot)
high--;
if (low<high) r[low++]=r[high];
while (low<high && r[low]<pivot)
low++;
if (low<high) r[high--]=r[low];
}
r[low]=pivot;
if (s<low) QuickSort(r,s,low-1);
if (t>high) QuickSort(r,high+1,t);
}
本文介绍了随机化快速排序的实现思路,通过选取合适的枢轴元素(pivot)进行交换,简化了传统快速排序中的问题。内容包括算法的核心逻辑和代码实现。
4036

被折叠的 条评论
为什么被折叠?



