#include<stdio.h>
#define MAXSIZE 25
typedef int DataType;
typedef struct
{
int key;
DataType other;
}Node;
typedef struct
{
Node r[MAXSIZE+1];
int Length;
}RecordList;
int QSpass(RecordList *L, int low, int high)
{
L->r[0]= L->r[low];
while (low < high)
{
while (low < high&&L->r[high].key >= L->r[0].key) high--;
L->r[low] = L->r[high];
while (low < high&&L->r[low].key < L->r[0].key) low++;
L->r[high] = L->r[low];
}
L->r[low] = L->r[0];
return low;
}
void QKSort(RecordList *L, int low, int high)
{
int pos;
if (low < high)
{
pos = QSpass(L,low,high);
QKSort(L, low, pos - 1);
QKSort(L, pos + 1, high);
}
}
int main()
{
int i;
RecordList L;
printf("请输入元素个数:\n");
scanf("%d",&L.Length);
for (i = 1; i <= L.Length; i++)
{
printf("请输入第%d个元素:\n",i);
scanf("%d",&L.r[i].key);
}
QKSort(&L, 1, L.Length);
for (i = 1; i <= L.Length; i++)
{
printf("%-4d",L.r[i].key);
}
}
快速排序的相关代码
于 2019-02-05 12:10:26 首次发布