8642 快速排序
来源:互联网 发布:wemall商业版java源码 编辑:程序博客网 时间:2024/05/20 00:12
快排在面试时候或者笔试时经常遇到,代码就是不会写也要记住思想怎么排。用笔和纸好好画画,最经典的排序。
8642
时间限制:1000MS
题型:编程题
Input第一行:键盘输入待排序关键的个数n 第二行:输入n个待排序关键字,用空格分隔数据
Output每行输出每趟排序的结果,数据之间用一个空格分隔
Sample Input 10 5 4 8 0 9 3 2 6 7 1
Sample Output 1 4 2 0 3 5 9 6 7 8
0 1 2 4 3 5 9 6 7 8
0 1 2 4 3 5 9 6 7 8
0 1 2 3 4 5 9 6 7 8
0 1 2 34 5 8 6 7 9
0 1 2 3 4 5 7 6 8 9
0 1 2 3 4 5 6 7 8 9
Hint
作者 yqm
答案:
#include<stdio.h>#include<malloc.h>#define OK 1#define ERROR 0#define ElemType inttypedef struct{ int *elem; int length; int listsize;}SqList;int InitList_Sq(SqList &L,int m){ L.elem=(ElemType*)malloc(m*sizeof(ElemType)); if(!L.elem) return ERROR; L.length=0; L.listsize=m; return OK;}int Load_Sq(SqList &L){ int i; if(L.length==0) printf("The List is empty!"); else { for(i=0;i<L.length;i++) printf("%d ",L.elem[i]); } printf("\n"); return OK;}int Partition(SqList &L,int low,int high){ int m,pivotkey=L.elem[low]; while(low<high) { while(low<high&&L.elem[high]>=pivotkey) high--; {m=L.elem[low];L.elem[low]=L.elem[high];L.elem[high]=m;} while(low<high&&L.elem[low]<=pivotkey) low++; {m=L.elem[low];L.elem[low]=L.elem[high];L.elem[high]=m;} } return low;}void QSort(SqList &T,int low,int high){ int pivotloc; if(low<high) { pivotloc=Partition(T,low,high); Load_Sq(T); QSort(T,low,pivotloc-1); QSort(T,pivotloc+1,high); }}int main(){ SqList T; int m,i; ElemType e, x; scanf("%d",&m); if(InitList_Sq(T,m)) // 判断顺序表是否创建成功 { for(i=0;i<m;i++) { scanf("%d",&x); T.elem[i] = x; } T.length = m; QSort(T,0,m - 1); }}
1 0
- 8642 快速排序
- 快速排序
- 快速排序
- 快速排序
- 快速排序!
- 快速排序
- 快速排序
- 快速排序
- 快速排序
- 快速排序
- 快速排序
- 快速排序
- 快速排序
- 快速排序
- 快速排序
- 快速排序
- 快速排序
- 快速排序
- 【VS2013】windows多线程编程_并行评测
- js中encodeURI与python中urllib.quote对应
- HLJUOJ1144(Dijkstra模板应用)
- Python对象的浅拷贝和深拷贝
- hash 在 perl 中的用法
- 8642 快速排序
- 玩转WMI --- 用脚本获取硬盘传感器温度和SMART讯息
- MyEclipse编辑PHP总结
- JS中showModalDialog 详细使用
- tyvj1117拯救ice-cream&&nyoj284坦克大战
- 写在项目即将收官时的反思
- bzoj 1064: [Noi2008]假面舞会
- 使用更加清晰的DebugLog工具开发调试
- 【牛腩新闻发布系统】----你的验证码正确么