首先先介绍排序:
1,大家都知道的冒泡排序:
#include #include using namespace std; template void swap(type x[],int,int); template void BubbleSort(type x[],int); int main() { srand(time(0)); const int n=10; int x[n]; for(int i=0;i x[ i]=rand()%99; for(int i=0;i cout<<" "< BubbleSort(x,n); cout<<"\nAfter Sort:"< for(int i=0;i cout<<" "<; system("pause"); return 0; } template void BubbleSort(type x[],int n) { for(int i=n-1;i>=0;i--) { int flag=0; for(int j=0;j if(x[ j]>x[ j+1]) { swap(x,j,j+1); flag=1; } if(flag==0) return; } } template void swap(type x[],int n,int m) { int temp=x[n]; x[n]=x[m]; x[m]=temp; }
2,简单的选择排序
#include #include using namespace std; template void swap(type x[],int,int); template void SlectSort(type x[],int); int main() { srand(time(0)); const int n=10; int x[n]; for(int i=0;i x[ i]=rand()%99; for(int i=0;i cout<<" "< SlectSort(x,n); cout<<"\nAfter Sort:"< for(int i=0;i cout<<" "< system("pause"); return 0; } template void swap(type x[],int n,int m) { int temp=x[n]; x[n]=x[m]; x[m]=temp; } template void SlectSort(type x[],int n) { for(int i=0;i { for(int j=i+1;j if(x[ j] swap(x,i,j); } }
3,插入排序
#include<iostream> #include<cstdlib> using namespace std; template<class type> void swap(type x[],int,int); template<class type> void InsertSort(type x[],int); int main() { srand(time(0)); const int n=10; int x[n]; for(int i=0;i<n;i++) x[ i]=rand()%99; for(int i=0;i<n;i++) cout<<" "<<x[ i]; InsertSort(x,n); cout<<"\nAfter Sort:"<<endl; for(int i=0;i<n;i++) cout<<" "<<x[ i]; system("pause"); return 0; } template<class type> void swap(type x[],int n,int m) { int temp=x[n]; x[n]=x[m]; x[m]=temp; } template<class type> void InsertSort(type x[],int n) { for(int i=1;i<n;i++) for(int j=i;j>0;j--) if(x[ j]<x[ j-1]) swap(x,j,j-1); }
4:希尔排序
#include<iostream> #include<cstdlib> using namespace std; template<class type> void swap(type x[],int,int); template<class type> void ShellSort(type x[],int); template<class type> void ShellSorthelper(type x[],int,int); int main() { srand(time(0)); const int n=10; int x[n]; for(int i=0;i<n;i++) x[ i]=rand()%99; for(int i=0;i<n;i++) cout<<" "<<x[ i]; ShellSort(x,n); cout<<"\nAfter Sort:"<<endl; for(int i=0;i<n;i++) cout<<" "<<x[ i]; system("pause"); return 0; } template<class type> void swap(type x[],int n,int m) { int temp=x[n]; x[n]=x[m]; x[m]=temp; } template<class type> void ShellSort(type x[],int n) { for(int i=n/2;i>=1;i/=2) for(int j=0;j<i;j++) ShellSorthelper(&x[ j],i,n-j); } template<class type> void ShellSorthelper(type x[],int len,int n) { for(int i=len;i<n;i+=len) for(int j=i;j>0;j-=len) if(x[ j]<x[ j-len]) swap(x,j,j-len); }
5,快速排序
#include<iostream> #include<cstdlib> using namespace std; template<class type> void QuicklySort(type x[],int n); template<class type> void QuicklySorthelper(type x[],int,int); template<class type> void swap(type x[],int,int); int main() { srand(time(0)); const int n=10; int x[ n]; for(int i=0;i<n;i++) x[ i]=rand()%99; for(int i=0;i<n;i++) cout<<" "<<x[ i]; QuicklySort(x,n); cout<<"\nAfter Sort:"<<endl; for(int j=0;j<n;j++) cout<<" "<<x[ j]; system("pause"); return 0; } template<class type> void QuicklySort(type x[],int n) { QuicklySorthelper(x,0,n-1); } template<class type> void QuicklySorthelper(type x[],int l,int r) { if(r>l) { swap(x,l,(l+r)/2);//尽量找出好的轴值; int i=l;int j=r+1;type pivot=x[l]; while(i<j) { while(x[--j]>pivot && i<j); if(i<j) swap(x,i,j); while(x[++i]<pivot && i<j); if(i<j) swap(x,i,j); } x[ i]=pivot; QuicklySorthelper(x,l,i-1); QuicklySorthelper(x,i+1,r); } } template<class type> void swap(type x[],int n,int m) { type temp=x[n]; x[n]=x[m]; x[m]=temp; }
6,归并排序(2路)
#include<iostream> #include<cstdlib> using namespace std; template<class type> void swap(type x[],int,int); template<class type> void MegicSort(type x[],int); template<class type> void MegicSorthelper(type x[],type y[],int,int); template<class type> void MegicSortPass(type x[],type y[],int,int,int); int main() { srand(time(0)); const int n=10; int x[n]; for(int i=0;i<n;i++) x[ i]=rand()%99; for(int i=0;i<n;i++) cout<<" "<<x[ i]; MegicSort(x,n); cout<<"\nAfter Sort:"<<endl; for(int i=0;i<n;i++) cout<<" "<<x[ i]; system("pause"); return 0; } template<class type> void swap(type x[],int m,int n) { type temp=x[m]; x[m]=x[n]; x[n]=temp; } template<class type> void MegicSort(type x[],int n) { type *y=new type[n]; int len=1; while(len<n) { MegicSorthelper(x,y,len,n);len*=2; MegicSorthelper(y,x,len,n);len*=2; } delete []y; } template<class type> void MegicSorthelper(type x[],type y[],int len,int n) { int i; for(i=0;i+2*len<n;i+=2*len) MegicSortPass(x,y,i,i+len,i+2*len); if(i+len<n) MegicSortPass(x,y,i,i+len,n); else for(;i<n;i++) y[ i]=x[ i]; } template<class type> void MegicSortPass(type x[],type y[],int l,int m,int n) { int i=l,j=m,k=l; for(;i<m && j<n;k++) { if(x[ i]>x[ j]) y[ k]=x[ j++]; else y[ k]=x[ i++]; } for(;i<m;i++) y[ k++]=x[ i]; for(;j<n;j++) y[ k++]=x[ j]; }
7,堆排序(不用Heap类)
#include<iostream> #include<cstdlib> using namespace std; template<class type> void swap(type x[],int,int); template<class type> void HeapSort(type x[],int); template<class type> void SiftDown(type x[],int,int); int main()
{ srand(time(0)); const int n=10; int x[n]; for(int i=0;i<n;i++) x[ i]=rand()%99; for(int i=0;i<n;i++) cout<<" "<<x[ i]; HeapSort(x,n); cout<<"\nAfter Sort:"<<endl; for(int i=0;i<n;i++) cout<<" "<<x[ i]; system("pause"); return 0; } template<class type> void SiftDown(type x[],int i,int n) { int cur=2*i+1; while(cur<n) { if(cur+1<n && x[cur+1]>x[cur]) cur++; if(x[cur]>x[(cur-1)/2]) swap(x,(cur-1)/2,cur); cur=2*cur+1; } } template<class type> void swap(type x[],int n,int m) { int temp=x[n]; x[n]=x[m]; x[m]=temp; } template<class type> void HeapSort(type x[],int n) { for(int cur=(n-2)/2;cur>=0;cur--) SiftDown(x,cur,n); for(int i=n-1;i>0;i--) { swap(x,i,0); for(int cur=(i-2)/2;cur>=0;cur--) SiftDown(x,cur,i); } }
8,基数排序
#include<iostream> #include<cstdlib> #include"Queue.h" using namespace std; template<class type> void RadixSort(type x[],int); template<class type> void RadixSorthelper(type x[],int,int); int main() { srand(time(0)); const int n=10; int x[n]; for(int i=0;i<n;i++) x[ i]=rand()%99; for(int i=0;i<n;i++) cout<<" "<<x[ i]; RadixSort(x,n); cout<<"\nAfter Sort:"<<endl; for(int i=0;i<n;i++) cout<<" "<<x[ i]; system("pause"); return 0; } template<class type> void RadixSort(type x[],int n) { int key=1; while(key<=10000) { RadixSorthelper(x,n,key); key*=10; } } template<class type> void RadixSorthelper(type x[],int n,int key) { int temp,k=0; Queue<int> *a=new Queue<int>[10]; for(int i=0;i<n;i++) { temp=x[ i]/key%10; a[temp].In(x[ i]); } for(int j=0;j<10;j++) { while(!a[ j].IsEmpty()) { a[ j].Out(x[ k++]); } } }
基数排序中用到的队列:
#include<iostream> template<class type> class QueueNode { public: QueueNode<type> *next; type data; QueueNode(type _data):data(_data),next(0){} ~QueueNode(){} }; template<class type> class Queue { private: QueueNode<type> *front; QueueNode<type> *rear; QueueNode<type> *getnew(type); public: Queue():front(0),rear(0){} ~Queue(); bool IsEmpty(); void In(type); void Out(type &); }; template<class type> void Queue<type>::Out(type &temp) { if(IsEmpty()) return; QueueNode<type> *cur=front; front=front->next; temp=cur->data; delete cur; } template<class type> void Queue<type>::In(type value) { QueueNode<type> *newptr=getnew(value); if(front==0) front=rear=newptr; else { rear->next=newptr; rear=rear->next; } } template<class type> bool Queue<type>::IsEmpty() { return front==0; } template<class type> QueueNode<type> *Queue<type>::getnew(type value) { QueueNode<type> *temp=new QueueNode<type>(value); return temp; }
9,链表排序(非模板编写,自己想到的,其实也算一种插入排序
只是实现方式不同)
#include<iostream> #include<iomanip> #include<cstdlib> using namespace std; class node { friend class listnode; private: int number; node *next; node *forward; public: node(int); }; node::node(int m) :number(m),next(0),forward(0) { } class listnode { private: node *firstpoint; node *getnewpoint(int); int n; public: listnode(int m):firstpoint(0),n(m){} void print(); void creat(); void insertpoint(int); }; void listnode::creat() { int i;int value; node *temp; cout<<"please cin the number:"<<endl; for(i=1;i<=n;i++) { cin>>value; insertpoint(value); } } void listnode::insertpoint(int value) { node *newpoint=getnewpoint(value); if(firstpoint==0) firstpoint=newpoint; else { node *temp=firstpoint;node *current; if(temp->number>value && temp->forward==0) { newpoint->next=temp; temp->forward=newpoint; firstpoint=firstpoint->forward; } else { while(temp->next!=0 && temp->number<value) temp=temp->next; if(temp->next==0 && temp->number<value) { temp->next=newpoint; newpoint->forward=temp; } else { current=temp; temp=temp->forward; current->forward=newpoint; newpoint->forward=temp; temp->next=newpoint; newpoint->next=current; } } } } node *listnode::getnewpoint(int value) { node *newptr=new node(value); return newptr; } void listnode::print() { cout<<"the sorted number is:"<<endl; while(firstpoint!=0) { cout<<" "<<firstpoint->number<<endl; firstpoint=firstpoint->next; } } int main() { listnode first(10); first.creat(); first.print(); system("pause"); return 0; }