快速排序

#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();
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值