- #include<iostream>
- #include<list>
- #include<stdlib.h>
- using namespace std;
- void Swap(int &a,int &b)
- {
- int t=a;
- a=b;
- b=t;
- }
- void Output(int *a,int n)
- {
- for(int i=0;i<n;i++)
- cout<<a[i]<<" ";
- cout<<endl;
- }
- //直接插入排序
- //时间复杂度O(n^2)
- void InsertSort(int *a,int n)
- {
- int i,j,t;
- for(i=1;i<n;i++)
- {
- if(a[i]<a[i-1])
- {
- t=a[i];
- for(j=i-1;j>=0&&a[j]>t;j--) a[j+1]=a[j];
- a[j+1]=t;
- }
- }
- }
- //折半插入排序 O(n^2)
- //只能减少排序过程中关键字比较的时间,并不能减少记录移动的时间
- void BInsertSort(int *a,int n)
- {
- int i,j,t,low,high,mid;
- for(i=1;i<n;i++)
- {
- t=a[i],low=0,high=i-1;
- while(low<=high)
- {
- mid=(low+high)/2;
- if(t<a[mid]) high=mid-1;
- else low=mid+1;
- }
- for(j=i-1;j>=high+1;--j)
- a[j+1]=a[j];
- a[high+1]=t;
- }
- }
- //2-路插入排序
- //只能减少移动记录的次数,不能绝对避免移动
- //时间复杂度 o(n^2)
- void P2_InsertSort(int *a,int n)
- {
- int i,j,first=0,final=0,*d=new int[n];
- d[0]=a[0];
- for(i=1;i<n;i++)
- if(a[i]<d[first])
- {
- first=(first-1+n)%n;
- d[first]=a[i];
- }
- else if(a[i]>d[final])
- {
- final=(final+1)%n;
- d[final]=a[i];
- }
- else
- {
- j=final;
- for(final=(final+1)%n;a[i]<d[j];j=(j-1+n)%n)
- d[(j+1)%n]=d[j];
- d[(j+1)%n]=a[i];
- }
- for(i=0;i<n;i++)
- {
- a[i]=d[first];
- first=(first+1)%n;
- }
- delete d;
- }
- //表插入排序
- //O(n^2)
- void SLinkSort(int *a,int n)
- {
- int *next = new int[n],low = 0,i,j,p;
- next[0] = -1;
- for(i = 1; i < n; i++) //排序
- if(a[i] < a[low])
- {
- next[i] = low;
- low = i;
- }
- else
- {
- j = low;
- while(next[j] != -1 && a[next[j]] < a[i])
- j = next[j];
- next[i] = next[j],next[j] = i;
- }
- for (i=0; i<n; i++) //调整
- {
- while (low < i)
- low = next[low];
- p = low;
- low = next[low];
- if (p != i)
- {
- Swap(a[i], a[p]);
- Swap(next[i], next[p]);
- next[i] = p;
- }
- }
- delete next;
- }
- //希尔排序
- //时间复杂度 o(n^2)
- void ShellInsert(int *a,int n,int dk)
- {
- int i,j,t;
- for(i = dk;i<n;++i)
- if(a[i]<a[i-dk])
- {
- t=a[i];
- for(j=i-dk;j>=0&&t<a[j];j-=dk)
- a[j+dk]=a[j];
- a[j+dk]=t;
- }
- }
- void ShellSort(int *a, int n)
- {
- int dlta[]={5,3,1};
- for(int i=0;i<sizeof dlta/sizeof dlta[0];i++)
- ShellInsert(a,n,dlta[i]);
- }
- //快速排序
- //平均O(nlogn) 最坏O(n^2)
- int Partition(int *a,int low,int high)
- {
- int pivotkey=a[low];
- while(low<high)
- {
- while(low<high&&a[high]>=pivotkey) --high;
- a[low]=a[high];
- while(low<high&&a[low]<=pivotkey) ++low;
- a[high]=a[low];
- }
- a[low]=pivotkey;
- return low;
- }
- void QuickSort(int *a,int low,int high)
- {
- if(low<high)
- {
- int pivotloc=Partition(a,low,high);
- QuickSort(a,low,pivotloc-1);
- QuickSort(a,pivotloc+1,high);
- }
- }
- //冒泡排序
- //O(n^2)
- void bubble_sort(int *a,int n)
- {
- int i,j;
- bool change=true;
- for(i=1;i<n&&change;i++)
- {
- change=false;
- for(j=n-1;j>=i;j--)
- if(a[j-1]>a[j])
- {
- Swap(a[j-1],a[j]);
- change=true;
- }
- }
- }
- //简单选择排序
- O(n^2)
- void SelectSort(int *a,int n)
- {
- int i,j,k;
- for(i=0;i<n-1;i++)
- {
- k=i;
- for(j=i+1;j<n;j++)
- if(a[j]<a[k]) k=j;
- if(k!=i)
- Swap(a[i],a[k]);
- }
- }
- //堆排序
- //注意数组下标
- //平均最坏时间复杂度都是O(nlogn)
- void HeapAdjust(int *a,int n,int s)
- {
- int t=a[s-1];
- for(int j=2*s;j<=n;j*=2)
- {
- if(j<n&&a[j-1]<a[j]) j++;
- if(t>a[j-1]) break;
- a[s-1]=a[j-1],s=j;
- }
- a[s-1]=t;
- }
- void HeapSort(int *a,int n)
- {
- int i;
- for(i=n/2;i>=1;i--)
- HeapAdjust(a,n,i);
- for(i=n;i>1;i--)
- {
- Swap(a[0],a[i-1]);
- HeapAdjust(a,i-1,1);
- }
- }
- //2-路归并排序
- //平均最坏时间复杂度都是O(nlogn)
- void Merge(int *a,int i,int m,int n)
- {
- for(int j = m + 1,k=i,t,p; j<=n; ++j)
- {
- t=a[j];
- while(t>a[k]) k++;
- for(p=j-1;p>=k;p--)
- a[p+1]=a[p];
- a[k]=t;
- }
- }
- void MSort(int *a,int s,int t)
- {
- if(s!=t)
- {
- int mid=(s+t)/2;
- MSort(a,s,mid);
- MSort(a,mid+1,t);
- Merge(a,s,mid,t);
- }
- }
- void MergeSort(int *a,int n)
- {
- MSort(a,0,n-1);
- }
- //非递归归并
- void UMergeSort(int *a,int n)
- {
- int i = 1, j;
- while(i < n)
- {
- j = 0;
- while(j <= n - 2 * i)
- {
- Merge(a, j, j + i -1, j + 2*i -1 );
- j+=2 * i;
- }
- if (j + i < n) Merge(a, j , j + i -1 , n-1 );
- i+=i;
- }
- }
- //自然归并排序
- //先找出数组里逆序的点,以该点分组
- void NaturalMSort(int *a,int n)
- {
- int i=0,x,p;
- while(i<n&&a[i]<=a[i+1]) i++;
- while(i<n)
- {
- p=i-1;
- do
- {
- x=a[i++];
- }while(i<n&&x<=a[i]);
- Merge(a,0,p,i-1);
- }
- }
- //基数排序
- //O(d(n+rd))
- void RadixSort(int *a,int n,int d) //d为a[]内最大元素的位数
- {
- list<int> lst[10];
- int i,j,m,k=1;
- for(i=0;i<d;i++,k*=10)
- {
- for(j=0;j<n;j++)
- lst[a[j]/k%10].push_back(a[j]);
- for(j=0,m=0;j<10;j++)
- while(!lst[j].empty())
- {
- a[m++]=lst[j].front();
- lst[j].pop_front();
- }
- }
- }
- int main()
- {
- int n,*a;
- cin>>n;
- a=new int[n];
- for(int i=0;i<n;i++)
- cin>>a[i];
- //InsertSort(a,n);
- //BInsertSort(a,n);
- //P2_InsertSort(a,n);
- //ShellSort(a,n);
- //QuickSort(a,0,n);
- //bubble_sort(a,n);
- //SelectSort(a,n);
- //SLinkSort(a,n);
- //MergeSort(a,n);
- //UMergeSort(a,n);
- NaturalMSort(a,n);
- //HeapSort(a,n);
- //RadixSort(a,n,5);
- Output(a,n);
- delete a;
- return 0;
- }
各种排序算法
最新推荐文章于 2024-09-12 13:19:42 发布