#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define MAXSIZE 20
typedef int KeyType;
typedef int InfoType;
typedef int Status;
typedef struct {
KeyType key;
InfoType otherinfo;
}RedType;
typedef struct {
RedType r[MAXSIZE + 1];
int length;
}SqList;
void CreateSqList(SqList &L) {
int n, i;
printf("建立顺序表:\n");
printf("请输入顺序表长:");
scanf("%d", &n);
L.length = n;
printf("随机生成顺序表里的所有数据:");
srand((unsigned)time(NULL));
for (i = 1; i <= L.length; i++) {
L.r[i].key = rand() % 100;
}
for (i = 1; i <= L.length; i++)
printf("%d ", L.r[i].key);
}
int n = 0;
Status Count(int n) {
n++;
return n;
}
Status Partition(SqList &L, int low, int high,int n) {
int pivotkey;
L.r[0] = L.r[low];
pivotkey = L.r[low].key;
while (low < high) {
while ((low<high)&&L.r[high].key >= pivotkey) --high;
L.r[low] = L.r[high];
while ((low<high)&&L.r[low].key <= pivotkey) ++low;
L.r[high] = L.r[low];
}
printf("\n");
printf("第%d趟排序后的顺序表:\n",n);
for (int i = 1; i <= L.length; i++)
printf("%d ", L.r[i].key);
L.r[low] = L.r[0];
return low;
}
void QSort(SqList &L, int low, int high) {
int mid;
if (low < high) {
n = Count(n);
mid = Partition(L, low, high,n);
QSort(L, low, mid - 1);
QSort(L, mid + 1, high);
}
}
void QuitSort(SqList &L) {
QSort(L, 1, L.length);
}
void main() {
SqList L;
CreateSqList(L);
printf("\n**************************");
int low = 1, high = L.length;
QSort(L, low, high);
printf("\n");
printf("**************************\n输出快速排序后的数据:");
for (int i = 1; i <= L.length; i++)
printf("%d ", L.r[i].key);
getchar();
getchar();
}
#include<stdlib.h>
#include<time.h>
#define MAXSIZE 20
typedef int KeyType;
typedef int InfoType;
typedef int Status;
typedef struct {
KeyType key;
InfoType otherinfo;
}RedType;
typedef struct {
RedType r[MAXSIZE + 1];
int length;
}SqList;
void CreateSqList(SqList &L) {
int n, i;
printf("建立顺序表:\n");
printf("请输入顺序表长:");
scanf("%d", &n);
L.length = n;
printf("随机生成顺序表里的所有数据:");
srand((unsigned)time(NULL));
for (i = 1; i <= L.length; i++) {
L.r[i].key = rand() % 100;
}
for (i = 1; i <= L.length; i++)
printf("%d ", L.r[i].key);
}
int n = 0;
Status Count(int n) {
n++;
return n;
}
Status Partition(SqList &L, int low, int high,int n) {
int pivotkey;
L.r[0] = L.r[low];
pivotkey = L.r[low].key;
while (low < high) {
while ((low<high)&&L.r[high].key >= pivotkey) --high;
L.r[low] = L.r[high];
while ((low<high)&&L.r[low].key <= pivotkey) ++low;
L.r[high] = L.r[low];
}
printf("\n");
printf("第%d趟排序后的顺序表:\n",n);
for (int i = 1; i <= L.length; i++)
printf("%d ", L.r[i].key);
L.r[low] = L.r[0];
return low;
}
void QSort(SqList &L, int low, int high) {
int mid;
if (low < high) {
n = Count(n);
mid = Partition(L, low, high,n);
QSort(L, low, mid - 1);
QSort(L, mid + 1, high);
}
}
void QuitSort(SqList &L) {
QSort(L, 1, L.length);
}
void main() {
SqList L;
CreateSqList(L);
printf("\n**************************");
int low = 1, high = L.length;
QSort(L, low, high);
printf("\n");
printf("**************************\n输出快速排序后的数据:");
for (int i = 1; i <= L.length; i++)
printf("%d ", L.r[i].key);
getchar();
getchar();
}