排序
来源:互联网 发布:python 删除列表元素 编辑:程序博客网 时间:2024/05/01 20:51
请原谅我,上来就是在大家眼中水的1B的冒泡排序
可是我相信冒泡排序也是大家看到算法--排序这个大家庭的第一朵鲜花了
冒泡排序
/*冒泡排序法 容易实现,但是要进行所有轮的比较,运行速度较慢*/#include<iostream>using namespace std;void bubble(int*,int);void main(){int array[]={5,4,6,8,9,3,1,2,7};const int len=sizeof(array)/sizeof(int);for(int i=0;i<len;++i)cout<<array[i]<<" ";cout<<endl;bubble(array,len);}void bubble(int*a,int size){//数组名会自动转化为指向第一个元素的指针,虽然不能直接传递数组,但函数的形参可以写成数组的形式(int*,int[],int[10])形参类型都是int*int i,temp;for(int pass=1;pass<size;++pass){for(i=0;i<size-pass;++i)if(a[i]>a[i+1]){temp=a[i];a[i]=a[i+1];a[i+1]=temp;}for(i=0;i<size;++i)cout<<a[i]<<" ";cout<<endl;}}
快速排序
快速排序可谓是排序大家族中的姣姣者
#include<iostream>#include<vector>using namespace std;int pivotIndex(vector<int>&v, int first, int last){// index for the midpoint of [first,last) and the// indices that scan the index range in tandemint mid, scanUp, scanDown;// pivot value and object used for exchangesint pivot, temp;if (first == last)return last;else if (first == last-1)return first;else{mid = (last + first)/2;pivot = v[mid];// exchange the pivot and the low end of the range// and initialize the indices scanUp and scanDown.v[mid] = v[first];v[first] = pivot;scanUp = first + 1;scanDown = last - 1;// manage the indices to locate elements that are in// the wrong sublist; stop when scanDown <= scanUpfor(;;){// move up lower sublist; stop when scanUp enters// upper sublist or identifies an element >= pivotwhile (scanUp <= scanDown && v[scanUp] < pivot)scanUp++;// scan down upper sublist; stop when scanDown locates// an element <= pivot; we guarantee we stop at arr[first]while (pivot < v[scanDown])scanDown--;// if indices are not in their sublists, partition completeif (scanUp >= scanDown)break;// indices are still in their sublists and identify// two elements in wrong sublists. exchangetemp = v[scanUp];v[scanUp] = v[scanDown];v[scanDown] = temp;scanUp++;scanDown--;}// copy pivot to index (scanDown) that partitions sublists// and return scanDownv[first] = v[scanDown];v[scanDown] = pivot;return scanDown;}}void quicksort(vector<int>& v, int first, int last){ // index of the pivot int pivotLoc;// temp used for an exchange when [first,last) has// two elementsint temp; // if the range is not at least two elements, return if (last - first <= 1)return;// if sublist has two elements, compare v[first] and// v[last-1] and exchange if necessary else if (last - first == 2){if (v[last-1] < v[first]){temp = v[last-1];v[last-1] = v[first];v[first] = temp;}return;} else{pivotLoc = pivotIndex(v, first, last);// make the recursive callquicksort(v, first, pivotLoc);// make the recursive callquicksort(v, pivotLoc +1, last);}}int main(){vector<int>ivec;int array[]={2,4,5,6,78,88,34,32,43,24,23,43,24,23,4,23,484};int length=sizeof(array)/sizeof(int);for(int i =0 ;i<length;++i)ivec.push_back(array[i]);quicksort(ivec,0,length);for(i =0 ;i<length;++i)cout<<ivec[i]<<" ";}
基数排序
想象第一次知道的不基于比较的排序,我记得我当时就直接“震惊了”
#include<iostream>#include<queue>#include<vector>#include "time.h";using namespace std;void distribute(const vector<int>& v, queue<int> digitQueue[], int power){int i;// loop through the vector, inserting each element into// the queue (v[i] / power) % 10for (i = 0; i < v.size(); i++)digitQueue[(v[i] / power) % 10].push(v[i]);}// support function for radixSort()// gather elements from the queues and copy back to the vectorvoid collect(queue<int> digitQueue[], vector<int>& v){int i = 0, digit;// scan the vector of queues using indices 0, 1, 2, etc.for (digit = 0; digit < 10; digit++)// collect items until queue empty and copy items back// to the vectorwhile (!digitQueue[digit].empty()){v[i] = digitQueue[digit].front();digitQueue[digit].pop();i++;}}void radixSort(vector<int>& v, int d){int i;int power = 1;queue<int> digitQueue[10];for (i=0;i < d;i++){distribute(v, digitQueue, power);collect(digitQueue, v);power *= 10;}}void main(){vector<int>ivec;int i;srand(time(NULL));for(i=0;i<20;++i){ivec.push_back(rand()%20);}radixSort(ivec,2);for(i=0; i<ivec.size();++i){cout<<ivec[i]<<" ";if((i+1)%5==0)cout<<endl;}cout<<endl;}
归并排序
#include<iostream>#include<vector>using namespace std;template <typename T>void merge(vector<T>& v, int first, int mid, int last){// temporary vector to merge the sorted sublistsvector<T> tempVector;int indexA, indexB, indexV;// set indexA to scan sublistA (index range [first,mid)// and indexB to scan sublistB (index range [mid, last)indexA = first;indexB = mid;// while both sublists are not exhausted, compare v[indexA] and// v[indexB]copy the smaller to vector temp using push_back()while (indexA < mid && indexB < last)if (v[indexA] < v[indexB]){tempVector.push_back(v[indexA]);// copy element to tempindexA++;// increment indexA}else{tempVector.push_back(v[indexB]);// copy element to tempindexB++;// increment indexB}// copy the tail of the sublist that is not exhaustedwhile (indexA < mid){tempVector.push_back(v[indexA]);indexA++;}while (indexB < last){tempVector.push_back(v[indexB]);indexB++;}// copy vector tempVector using indexV to vector v using indexA// which is initially set to firstindexA = first;// copy elements from temporary vector to original listfor (indexV = 0; indexV < tempVector.size(); indexV++){v[indexA] = tempVector [indexV];indexA++;}}// sorts v in the index range [first,last) by merging// ordered subliststemplate <typename T>void mergeSort(vector<T>& v, int first, int last){// if the sublist has more than 1 element continueif (first + 1 < last) {// for sublists of size 2 or more, call mergeSort()// for the left and right sublists and then// merge the sorted sublists using merge()int midpt = (last + first) / 2;mergeSort(v, first, midpt);mergeSort(v, midpt, last);merge(v, first, midpt, last); }}int main(){vector<int> ivec;int array[]={1,2,3,4,5,8,9,7,1,5,2,54,544,54,45,4,5,4,454,45,45,44,54,4,34,2,256,34,926};int length=sizeof(array)/sizeof(int);for(int i=0;i<length;++i){ivec.push_back(array[i]);}mergeSort(ivec,0,length);for(i=0;i<length;++i){cout<<ivec[i]<<" ";}return 0;}
鸽巢排序(来自C++版块精华帖)
排序字节串、宽字节串最快的排序算法,计数排序的变种(将计数缓冲区大小固定,少一次遍历开销),速度是STL中std::sort的20多倍,更重要的是实现极其简单!缺点是需要一个size至少等于待排序数组取值范围的缓冲区,不适合int等大范围数据
#include<iostream>#include"time.h"using namespace std;//鸽巢排序void PigeonholeSort(int *array,int length){int b[256]={0};int i,j,k;for(i=0;i<length;i++)b[array[i]]++;for(i=0,j=0;i<256;i++)for(k=0;k<b[i];k++)array[j++]=i;}void main(){/*time_t nowtime;nowtime=time(NULL);cout<<nowtime;*/int array[]={2,5,6,4,1,9,8,7};int length=sizeof(array)/sizeof(int);PigeonholeSort(array,length);for(int m=0; m !=length; ++m)cout<<array[m]<<" ";}
先附上好东西一个
冒泡和选择排序该被踢出教材了
这是C++版块关于排序的一个精华帖,很不错的的说
今天写不完了,明天继续。
我的新浪微博是@酥西黄
我的邮箱是suziewong@163.com
- 排序
- 排序
- 排序
- 排序
- 排序
- 排序
- 排序
- 排序
- 排序
- 排序
- 排序
- 排序
- 排序
- 排序
- 排序
- 排序
- 排序
- 排序
- SQL Server中创建链接服务器图解教程
- oracle 行转列
- 一致性哈希算法及其在分布式系统中的应用
- 雅虎WEB前端网站优化 -- 34条军规
- 如何动态载入到sencha-touch的combobox(selectfield)中
- 排序
- 去掉html标签的js
- 图解aclocal、autoconf、automake、autoheader、configure
- Managing Hierarchical Data in MySQL
- VMware虚拟机的网络设置
- 养成写高效的SQL语句的习惯
- 字符编码详解及由来
- MySQL递归查询——数据平坦化新思维
- C++中的cast