(1)插入排序——适合小数据量的排序
最坏复杂度:O(n^2),平均复杂度:O(n^2),最好复杂度:O(n)——原始数列已经排好序
#include<iostream> using namespace std; void sort(int a[],int N) { for(int p=1;p<N;p++) { int j,temp=a[p]; for(j=p;j>0 && a[j-1]>temp;j--) a[j]=a[j-1]; a[j]=temp; } } int main() { int N=8; int a[]={3,2,98,4,2,89,1002,1}; sort(a,N); for(int i=0;i<N;i++) cout<<a[i]<<" "; system("pause"); return 0; }
(2)快速排序——实践中已知的最快的排序算法
最坏复杂度:O(n^2),平均复杂度:O(nlgn),最好复杂度:O(nlgn)
#include<iostream> using namespace std; void swap(int *a,int *b) { int temp=*a; *a=*b;*b=temp; } int middle(int a[],int left,int right) { int m=(left+right)/2; if(a[left]>a[m]) swap(&a[left],&a[m]); if(a[left]>a[right]) swap(&a[left],&a[right]); if(a[m]>a[right]) swap(&a[m],&a[right]); swap(&a[m],&a[right-1]); return a[right-1]; } void quick_sort(int a[],int left,int right) { if(left+2<=right) { int pivot=middle(a,left,right); int i=left,j=right-1; while(1) { while(a[i]<pivot){i++;} while(a[j]>=pivot){j--;} if(i<j) swap(&a[i],&a[j]); else break; } swap(&a[i],&a[right-1]); quick_sort(a,left,i-1); quick_sort(a,i+1,right); } else { if(left+1==right) { if(a[left]>a[right]) swap(&a[left],&a[right]); } } } int main() { int a[]={23,1,34,2,90,1,2}; int N=7,left=0,right=N-1; cout<<"排序前的数组:"<<endl; for(int j=0;j<N;j++) cout<<a[j]<<" "; cout<<endl; quick_sort(a,left,right); cout<<"排序后的数组:"<<endl; for(int i=0;i<N;i++) cout<<a[i]<<" "; cout<<endl; system("pause"); return 0; }
(3)归并排序
时间复杂度:O(nlgn),缺点:需要额外的存储空间
#include<iostream> #include<vector> using namespace std; void merge(int a[],int left,int middle,int right) { vector<int>temp; int p=left,q=right; int dex=left; int N=right-left+1; int right_begin=middle+1; while(left<=middle && right_begin<=right) { if(a[left]<=a[right_begin]) temp.push_back(a[left++]); else temp.push_back(a[right_begin++]); } while(left<=middle) temp.push_back(a[left++]); while(right_begin<=right) temp.push_back(a[right_begin++]); for(int i=temp.size()-1;i>=0;i--,q--) a[q]=temp[i]; } void msort(int a[],int left,int right) { int middle; vector<int> result; vector<int> num; if(left<right) { middle=(left+right)/2; msort(a,left,middle); msort(a,middle+1,right); merge(a,left,middle,right); } } int main() { int a[]={3,4,2,1,89,2,2,1,1}; int N=9; cout<<"before sort:"<<endl; for(int j=0;j<N;j++) cout<<a[j]<<" "; cout<<endl; int left=0,right=N-1; msort(a,left,right); cout<<"after sort:"<<endl; for(int i=0;i<N;i++) cout<<a[i]<<" "; cout<<endl; system("pause"); return 0; }
(4)计数排序(桶式排序)
复杂度:O(M+N),M:待排序数组中的最大值,N:元素个数
#include<iostream> #include<vector> using namespace std; void bucket_sort(vector<int>a,int max) { vector<int>temp; for(int i=0;i<max;i++) temp.push_back(0); for(int j=0;j<max;j++) { for(int k=0;k<a.size();k++) { if(a[k]==j+1) temp[j]++; } } int count; for(int p=0;p<max;p++) { count=temp[p]; while(count) { cout<<p+1<<" "; count--; } } } int main() { vector<int>a; int x;char c; while(cin>>x) { a.push_back(x); c=cin.get(); if(c==' ')break; } int max=a[0]; for(int i=1;i<a.size();i++) { if(a[i]>max) max=a[i]; } bucket_sort(a,max); system("pause"); return 0; }
(5)shell排序,也称缩小增量排序
#include<iostream> using namespace std; void shell_sort(int A[],int N) { int d=N; int temp; while(d>1) { d=(d+1)/2; for(int i=0;i<N-d;i++) { if(A[i+d]<A[i]) { temp=A[i]; A[i]=A[i+d]; A[i+d]=temp; } } } for(int i=0;i<N;i++) cout<<A[i]<<endl; } int main() { int A[]={2,3,4,9,3,8,10,8,2,7}; shell_sort(A,10); return 0; }