排序算法总结(一)
来源:互联网 发布:dojo中js方法跳转页面 编辑:程序博客网 时间:2024/05/16 13:06
排序是最基本的算法,曾经学过很多种排序算法,但从没有总结过,这次总结一下,排序算法有快速排序,桶排序,插入排序,基数排序,归并排序,冒泡排序,选择排序,希尔排序,堆排序,计数排序等,再次一一总结一下。
快速排序:通过一趟扫描将要排序的数据分割成独立的两部分,其中一部分的所有数据都比另外一部分的所有数据都要小,然后再按此方法对这两部分数据分别进行快速排序,整个排序过程可以递归进行,以此达到整个数据变成有序序列
代码:
#include <iostream>using namespace std;void QuickSort(int a[],int low,int high){ //if(low >= high) return; int temp = a[high/2]; int i = low; int j = high; while(i < j) { while(a[i]<temp) i++; while(a[j]>temp) j--; if(i<j) { int swap = a[i]; a[i] = a[j]; a[j] = swap; i++; j--; } } if(low<i) QuickSort(a,low,i); if(i+1 < high) QuickSort(a,i+1,high); return;}int main(){ int a[] = {2,4,6,3,5}; QuickSort(a,0,4); for(int i = 0;i<5;i++) cout<<a[i]<<" "; cout<<endl; return 0;}
桶排序:假定:输入是由一个随机过程产生的[0, 1)区间上均匀分布的实数。将区间[0, 1)划分为n个大小相等的子区间(桶),每桶大小1/n:[0, 1/n), [1/n, 2/n), [2/n, 3/n),…,[k/n, (k+1)/n ),…将n个输入元素分配到这些桶中,对桶中元素进行排序,然后依次连接桶输入0 ≤A[1..n] <1辅助数组B[0..n-1]是一指针数组,指向桶(链表)。最简单的示例:
#include <iostream>using namespace std;void BucketSort(int a[],int n){ int temp[100] = {}; for(int i = 0; i < n; i++) { int index = a[i]; temp[index] = a[i]; } for(int j = 0;j < 100;j++) { if(temp[j] != 0) cout<<temp[j]<<" "; } cout<<endl; return;}int main(){ int a[] = {2,4,3,6,5}; BucketSort(a,5); return 0;}
插入排序:插入排序就是每一步都将一个待排数据按其大小插入到已经排序的数据中的适当位置,直到全部插入完毕。 示例代码:
#include <iostream>using namespace std;void InsertSort(int a[],int n){ int i,j,temp=0; for(i = 0; i < n; i++) { for(j = i; j < n; j++) { if(a[j] < a[i]) { temp = a[i]; a[i] = a[j]; a[j] = temp; } } } return;}int main(){ int a[] = {3,6,4,1,7}; InsertSort(a,5); for(int i = 0; i < 5; i++) cout<<a[i]<<" "; cout<<endl; return 0;}基数排序:
原理类似桶排序,这里总是需要10个桶,多次使用
首先以个位数的值进行装桶,即个位数为1则放入1号桶,为9则放入9号桶,暂时忽视十位数
再次入桶,不过这次以十位数的数字为准,进入相应的桶,
由于前边做了个位数的排序,所以当十位数相等时,个位数字是由小到大的顺序入桶的,就是说,入完桶还是有序。
#include<iostream>using namespace std;#include <vector>int data[10]={73, 22, 93, 43, 55, 14, 28, 65, 39, 81};int tmp[10];int count[10];int maxbit(int data[],int n){ int d=1; for(int i=0;i<n;i++) { int c=1; int p=data[i]; while(p/10) { p=p/10; c++; } if(c>d) d=c; } return d;}void RadixSort(int data[],int n){ int d=maxbit(data,n); int r=1; for(int i=0;i<d;i++) { for(int i=0;i<10;i++)//装桶之前要先清桶 count[i]=0; for(i=0;i<n;i++) //记录每个桶的记录数 { int k=data[i]/r; int q=k%10; count[q]++; } for(i=1;i<10;i++)//计算位置 { count[i]+=count[i-1]; //cout<<count[i]<<" "; } for(int j=n-1;j>=0;j--) { int p=data[j]/r; int s=p%10; tmp[count[s]-1]=data[j]; count[s]--; //cout<<data[j]<<" "; } for(i=0;i<n;i++) { data[i]=tmp[i]; //cout<<tmp[i]<<" "; } // cout<<endl; r=r*10; }}int main(){ cout<<"基数排序c++实现"<<endl; //cout<<maxbit(data,10)<<endl; cout<<"排序之前的数值:"; for(int i=0;i<10;i++) cout<<data[i]<<" "; cout<<endl; RadixSort(data,10); cout<<"排序之前的数值:"; for(i=0;i<10;i++) cout<<data[i]<<" "; cout<<endl; return 0;}
- 排序算法总结(一)
- 排序算法总结(一)
- 排序算法总结(一)
- 排序算法总结(一)
- 排序算法总结(一)
- 排序算法总结(一)
- 排序算法总结(一)
- 排序算法总结(一)
- 排序算法总结(一)
- 排序算法总结(一)
- 排序算法总结(一)
- 排序算法总结(一)计数排序
- 排序算法总结一
- 常用排序算法总结(一)----冒泡排序,归并排序
- 排序算法 总结&思考(一)
- Java常见排序算法总结(一)
- 排序算法大总结(一)
- 数据结构-各类排序算法总结(一)
- 如何用BCB打开EXCEL文件
- myeclipse注册机GUI程序
- Linux下搭建JSP环境
- java IO之 RandomAccessFile类
- 【Html5每日练习】本地存储localStorage
- 排序算法总结(一)
- PERT网络分析法
- eclipse 3.6+版本如何同步外部jar到服务器的lib下
- 用动态规划解决两个装配站最优化问题
- POJ 1113 wall
- cssd for bug
- PowerDesigner基础教程
- iOS指南系列:如何解决奔溃问题-关于内存访问续
- oracle常识