问题描述:
/*
* Copyright (c)2015级,烟台大学计算机与控制工程学院
* All rights reserved.
* 文件名称:11.cpp
* 作 者:彭友程
* 完成日期:2016年12月12日
* 版 本 号:v1.0
*问题描述:验证快速排序,完成测试。
*输入描述:无
*程序输出:测试数据
*/
2.以中间位置的元素为基准
实现代码:
- #include <stdio.h>
- #define MaxSize 20
- typedef int KeyType;
- typedef char InfoType[10];
- typedef struct
- {
- KeyType key;
- InfoType data;
- } RecType;
- void QuickSort1(RecType R[],int s,int t)
- {
- int i=s,j=t;
- KeyType pivot;
- RecType tmp;
- pivot = R[(s+t)/2].key;
- if (s<t)
- {
- while (i!=j)
- {
- while (j>i && R[j].key>pivot)
- j--;
- while (i<j && R[i].key<pivot)
- i++;
- if(i<j)
- {
- tmp=R[i];
- R[i]=R[j];
- R[j]=tmp;
- }
- }
- QuickSort1(R,s,i-1);
- QuickSort1(R,j+1,t);
- }
- }
- int main()
- {
- int i,n=10;
- RecType R[MaxSize];
- KeyType a[]= {6,8,7,9,0,1,3,2,4,5};
- for (i=0; i<n; i++)
- R[i].key=a[i];
- printf("排序前:");
- for (i=0; i<n; i++)
- printf("%d ",R[i].key);
- printf("\n");
- QuickSort1(R,0,n-1);
- printf("排序后:");
- for (i=0; i<n; i++)
- printf("%d ",R[i].key);
- printf("\n");
- return 0;
- }
运行结果:
知识点总结:
快速排序
心得体会:
以中间为基准展开排序。