#include <stdio.h>
#define MAXSIZE 10
typedef int KeyType;
typedef struct {
KeyType key; // 关键字项
// int otherinfo; // 其它数据项
} RedType;
typedef struct {
RedType r[MAXSIZE+1]; // r[0]闲置
int length; // 顺序表长度
} SqList;
int Partition (RedType R[], int low, int high) {
int pivotkey;
R[0] = R[low]; pivotkey = R[low].key; // 枢轴
while (low<high) {
while(low<high&& R[high].key>=pivotkey)
-- high; // 从右向左搜索
R[low] = R[high];
while (low<high && R[low].key<=pivotkey)
++ low; // 从左向右搜索
R[high] = R[low];
}
R[low] = R[0]; return low;
}// Partition
void QSort (RedType R[], int s, int t ) {
// 对记录序列R[s..t]进行快速排序
int pivotloc;
if (s < t) { // 长度大于1
pivotloc = Partition(R, s, t);
// 对 R[s..t] 进行一次划分
QSort(R, s, pivotloc-1);
// 对低子序列递归排序,pivotloc是枢轴位置
QSort(R, pivotloc+1, t); // 对高子序列递归排序
}
} // QSort
void QuickSort( SqList & L) {
// 对顺序表进行快速排序
QSort(L.r, 1, L.length);
} // QuickSort
void main()
{
SqList L={{0,21,34,12,6,78,45,10,20,70,30},10};
QuickSort(L);
int i;
for(i=1;i<=L.length;i++)
printf("%d ",L.r[i]);
}
C语言实现快速排序算法
最新推荐文章于 2024-01-25 11:00:35 发布