快速排序
来源:互联网 发布:学初中英语哪个软件好 编辑:程序博客网 时间:2024/06/04 18:29
快序排序
快速排序是由冒泡排序改进得到的。在冒泡排序过程中,只能元素对相邻的两个元素进行比较,所以一趟下来只能消除两个逆序的,快速排序是通过两个不相邻的元素进行比较,一趟下来可以消除多个逆序。
原始数列:{ 13 28 70 59 94 89 10 76 51 1 };
╋╬╋╬╋╬╋╬╋╬╋╬╋╬╋华丽的分界线╋╬╋╬╋╬╋╬╋╬╋╬╋╬╋
定义三个变量,key = 13 【整体的第一个元素】;low 指向整体第一个元素, high 指向整体最后一个元素;
【下文中的high.data表示high所指的元素大小,high为其下标,亦可理解为指针,low同high】
【⊙-⊙】先用high.data值与key值比较,如果high.data >= key && high != low,high向前移一位,再用high.data值与key值比较,如果high.data < key && high != low,把high.data的值赋给low.data,low.data后移一位,然后用low.data值与key比较,如果low.data <= key && high != low,low后移一位,再用比较,如果low.data > key && high != low,用low.data 值赋给 high.data,重复【⊙-⊙】,直到low == high,将key值赋给low.data,然后把一当前low为分界线把整体一分为二,然后两个部分都重复【华丽分界线】以下的内容,直至每个后期独立出来的整体都只剩一个元素。
原始数列:{ 13 28 70 59 94 89 10 76 51 1 };
第一趟:{ 1 10 } 13 { 59 94 89 70 76 51 28 };
第二趟:1 { 10 } 13 { 28 51 } 59 { 70 76 89 94 };
第三趟:1 10 13 28 { 51 } 59 70 { 76 89 94 };
第四趟:1 10 13 28 51 59 70 76 { 89 94 };
第五趟:1 10 13 28 51 59 70 76 89 { 94 };
第六趟:1 10 13 28 51 59 70 76 89 94;
源代码如下【c++】
#include <cstdio>#include <cstring>#include <cstdlib>#include <algorithm>#define MAXSIZE 100using namespace std;int Q = 1;int n;typedef struct {int data;}Node;typedef struct {Node *elem;int len;}List;void Set_Up(List &L) {L.elem = new Node[MAXSIZE];for(int i = 1; i <= L.len; i++)scanf("%d", &L.elem[i].data);return ;}void Quick_Sort(List &L, int s, int e) {int a = s, b = e;if(s >= e)return ;int key = L.elem[s].data;while(s < e) {while(s < e && L.elem[e].data >= key)e--;L.elem[s].data = L.elem[e].data;while(s < e && L.elem[s].data <= key) s++;L.elem[e].data = L.elem[s].data;}L.elem[s].data = key;printf("第%d次交换结果:\n", Q++);for(int i = 1; i <= n; i++)printf("%d ", L.elem[i].data);printf("\n");Quick_Sort(L, a, s - 1);Quick_Sort(L, s + 1, b);}void Print(List L, int n) {for(int i = 1; i <= n; i++)printf("%d ", L.elem[i].data);printf("\n\n");}int main() {int t;while(1) {List L;printf("请输入数列的长度:");scanf("%d", &n);L.len = n;printf("请输入长度为%d的数列:", n); Set_Up(L);Quick_Sort(L, 1, n);printf("排序后的数列如下:\n");Print(L, n);}system("pause");return 0;}
- 快速排序
- 快速排序
- 快速排序
- 快速排序!
- 快速排序
- 快速排序
- 快速排序
- 快速排序
- 快速排序
- 快速排序
- 快速排序
- 快速排序
- 快速排序
- 快速排序
- 快速排序
- 快速排序
- 快速排序
- 快速排序
- uImage与zImage的区别
- 单片机: EEPROM和串口通信
- [Android]资源分享
- iOS CoreData(Swift版本)
- 《剑指offer》——数值的整数次方
- 快速排序
- dz设置伪静态后复制帖子链接还是动态解决办法
- 初窥OpenGL
- 出现 I/Choreographer: Skipped 160 frames! The application may be doing too much work on its main thre
- AppBarLayout: onOffsetChanged 多次滑动之后不再调用问题
- 算法学习——海量数据处理
- 结构体的基本概念
- 数据推送功能之客户端部分(待总结)
- JS中常用操作技巧(页面动态增加div,contain方法扩展,JSON对象鱼字符串转换)