#include<iostream>
using namespace std;
const int MaxSize = 20;
typedef int KeyType;
typedef struct {//顺序表的结构
KeyType key;
int data;
}RecType;
typedef struct {//顺序表
RecType data[MaxSize];
int length;
}Sqlist;
void CreateList(Sqlist*& L, int a[], int n) {//建立顺序表
int i = 0, k = 0;
L = (Sqlist*)malloc(sizeof(Sqlist));
while (i < n)
{
L->data[k].key = a[i];
k++, i++;
}
L->length = k;
}
int partition(RecType R[], int s, int t)
{
int i = s, j = t;
RecType tmp = R[i];
while (i < j)
{
while (i < j && R[j].key >= tmp.key)
j--;
R[i] = R[j];
while (i < j && R[i].key <= tmp.key)
i++;
R[j] = R[i];
}
R[i] = tmp;
return i;
}
void QuickSort(RecType R[], int s, int t)
{
int i;
if (s < t)
{
i = partition(R, s, t);
QuickSort(R, s, i - 1);
QuickSort(R, i + 1, t);
}
}
void DestroyList(Sqlist*& L) {//销毁顺序表
free(L);
}
int main()
{
int a[] = { 6,8,7,9,0,1,3,2,4,5 };
int n = 10;
Sqlist* b;
CreateList(b, a, n);
QuickSort(b->data, 0, n-1);
for (int i = 0; i < n; i++) {
cout << b->data[i].key;
cout << " ";
}
DestroyList(b);
return 0;
}
顺序表的快速排序
最新推荐文章于 2022-01-12 14:33:15 发布