各种排序算法

  1. #include<iostream>
  2. #include<list>
  3. #include<stdlib.h>
  4. using namespace std;
  5. void Swap(int &a,int &b)
  6. {
  7.     int t=a;
  8.     a=b;
  9.     b=t;
  10. }
  11. void Output(int *a,int n)
  12. {
  13.     for(int i=0;i<n;i++)
  14.         cout<<a[i]<<" ";
  15.     cout<<endl;
  16. }
  17. //直接插入排序  
  18. //时间复杂度O(n^2)
  19. void InsertSort(int *a,int n)   
  20. {
  21.     int i,j,t;
  22.     for(i=1;i<n;i++)
  23.     {
  24.         if(a[i]<a[i-1])
  25.         {
  26.             t=a[i];
  27.             for(j=i-1;j>=0&&a[j]>t;j--) a[j+1]=a[j];
  28.             a[j+1]=t;
  29.         }
  30.     }
  31. }
  32. //折半插入排序  O(n^2)
  33. //只能减少排序过程中关键字比较的时间,并不能减少记录移动的时间
  34. void BInsertSort(int *a,int n)
  35. {
  36.     int i,j,t,low,high,mid;
  37.     for(i=1;i<n;i++)
  38.     {
  39.           t=a[i],low=0,high=i-1;
  40.             while(low<=high)
  41.             {
  42.                  mid=(low+high)/2;
  43.                 if(t<a[mid]) high=mid-1;
  44.                 else low=mid+1;
  45.             }
  46.             for(j=i-1;j>=high+1;--j)
  47.                 a[j+1]=a[j];
  48.             a[high+1]=t;
  49.     }
  50. }
  51. //2-路插入排序
  52. //只能减少移动记录的次数,不能绝对避免移动 
  53. //时间复杂度 o(n^2) 
  54. void P2_InsertSort(int *a,int n)
  55. {
  56.      int i,j,first=0,final=0,*d=new int[n];
  57.      d[0]=a[0];
  58.      for(i=1;i<n;i++)
  59.           if(a[i]<d[first])
  60.         {
  61.             first=(first-1+n)%n;
  62.             d[first]=a[i];
  63.         }
  64.         else if(a[i]>d[final])
  65.         {
  66.             final=(final+1)%n;
  67.             d[final]=a[i];
  68.         }
  69.         else
  70.         {
  71.             j=final;
  72.             for(final=(final+1)%n;a[i]<d[j];j=(j-1+n)%n)
  73.                 d[(j+1)%n]=d[j];
  74.             d[(j+1)%n]=a[i];
  75.         }
  76.      for(i=0;i<n;i++)
  77.      {
  78.             a[i]=d[first];
  79.             first=(first+1)%n;
  80.         }
  81.         delete d;           
  82. }
  83. //表插入排序
  84. //O(n^2) 
  85. void SLinkSort(int *a,int n)
  86. {
  87.     int *next = new int[n],low = 0,i,j,p;
  88.     next[0] = -1;
  89.     for(i = 1; i < n; i++)          //排序
  90.        if(a[i] < a[low])
  91.        {
  92.           next[i] = low;
  93.           low = i;
  94.         }
  95.        else 
  96.        {
  97.          j = low;
  98.          while(next[j] != -1 && a[next[j]] < a[i])
  99.                 j = next[j];
  100.          next[i] = next[j],next[j] = i;
  101.         }
  102.      for (i=0; i<n; i++)        //调整
  103.      { 
  104.         while (low < i) 
  105.              low = next[low]; 
  106.         p = low; 
  107.         low = next[low]; 
  108.         if (p != i) 
  109.         { 
  110.              Swap(a[i], a[p]); 
  111.              Swap(next[i], next[p]); 
  112.              next[i] = p; 
  113.         } 
  114.    } 
  115.   delete next;  
  116. }
  117. //希尔排序
  118. //时间复杂度 o(n^2) 
  119. void ShellInsert(int *a,int n,int dk)
  120. {
  121.     int i,j,t;
  122.     for(i = dk;i<n;++i)
  123.             if(a[i]<a[i-dk])
  124.             {
  125.                 t=a[i];
  126.                 for(j=i-dk;j>=0&&t<a[j];j-=dk)
  127.                      a[j+dk]=a[j];
  128.                 a[j+dk]=t;
  129.             }   
  130. }
  131. void ShellSort(int *a, int n)
  132. {
  133.       int dlta[]={5,3,1};
  134.       for(int i=0;i<sizeof dlta/sizeof dlta[0];i++)
  135.           ShellInsert(a,n,dlta[i]);
  136. }
  137. //快速排序
  138. //平均O(nlogn) 最坏O(n^2)   
  139. int Partition(int *a,int low,int high)
  140. {
  141.     int pivotkey=a[low];
  142.     while(low<high)
  143.     {
  144.         while(low<high&&a[high]>=pivotkey) --high;
  145.         a[low]=a[high];
  146.         while(low<high&&a[low]<=pivotkey) ++low;
  147.         a[high]=a[low];
  148.     }
  149.     a[low]=pivotkey;
  150.     return low;
  151. }
  152. void QuickSort(int *a,int low,int high)  
  153. {
  154.     if(low<high)
  155.     {
  156.         int pivotloc=Partition(a,low,high);
  157.         QuickSort(a,low,pivotloc-1);
  158.         QuickSort(a,pivotloc+1,high);
  159.     }
  160. }
  161. //冒泡排序
  162. //O(n^2) 
  163. void bubble_sort(int *a,int n)
  164. {
  165.         int i,j;
  166.         bool change=true;
  167.         for(i=1;i<n&&change;i++)
  168.         {
  169.                  change=false;
  170.                  for(j=n-1;j>=i;j--)
  171.                     if(a[j-1]>a[j]) 
  172.                     {
  173.                             Swap(a[j-1],a[j]);
  174.                             change=true;
  175.                     }
  176.         } 
  177. }
  178. //简单选择排序
  179. O(n^2) 
  180. void SelectSort(int *a,int n)    
  181. {
  182.     int i,j,k;
  183.     for(i=0;i<n-1;i++)
  184.     {
  185.         k=i;
  186.         for(j=i+1;j<n;j++)
  187.             if(a[j]<a[k]) k=j;
  188.         if(k!=i)
  189.         Swap(a[i],a[k]);
  190.     }
  191. }
  192. //堆排序 
  193. //注意数组下标 
  194. //平均最坏时间复杂度都是O(nlogn) 
  195. void HeapAdjust(int *a,int n,int s)
  196. {
  197.     int t=a[s-1];
  198.     for(int j=2*s;j<=n;j*=2)
  199.     {
  200.         if(j<n&&a[j-1]<a[j]) j++;
  201.         if(t>a[j-1]) break;
  202.         a[s-1]=a[j-1],s=j;
  203.     }
  204.     a[s-1]=t;
  205. }
  206. void HeapSort(int *a,int n)
  207. {
  208.     int i;
  209.     for(i=n/2;i>=1;i--)
  210.         HeapAdjust(a,n,i);
  211.     for(i=n;i>1;i--)
  212.     {
  213.         Swap(a[0],a[i-1]);
  214.         HeapAdjust(a,i-1,1);
  215.     }
  216. }
  217. //2-路归并排序
  218. //平均最坏时间复杂度都是O(nlogn) 
  219. void Merge(int *a,int i,int m,int n)
  220. {
  221.     for(int j = m + 1,k=i,t,p; j<=n; ++j)
  222.     {
  223.         t=a[j];
  224.         while(t>a[k])  k++;
  225.         for(p=j-1;p>=k;p--)
  226.              a[p+1]=a[p];
  227.         a[k]=t;
  228.     }
  229. }
  230. void MSort(int *a,int s,int t)
  231. {
  232.     if(s!=t)
  233.     {
  234.         int mid=(s+t)/2;
  235.         MSort(a,s,mid);
  236.         MSort(a,mid+1,t);
  237.         Merge(a,s,mid,t);
  238.         }   
  239. }
  240. void MergeSort(int *a,int n)
  241. {
  242.     MSort(a,0,n-1);
  243. }
  244. //非递归归并
  245. void UMergeSort(int *a,int n)
  246. {
  247.     int i = 1, j;
  248.     while(i < n)
  249.     {
  250.             j = 0;
  251.             while(j <= n - 2 * i)
  252.             {
  253.                 Merge(a, j, j + i -1, j + 2*i -1  );
  254.                      j+=2 * i;
  255.             }
  256.             if (j + i < n) Merge(a, j , j + i -1 , n-1 );
  257.     i+=i;
  258.     }
  259.     
  260. }
  261. //自然归并排序
  262. //先找出数组里逆序的点,以该点分组
  263. void NaturalMSort(int *a,int n)
  264. {
  265.      int i=0,x,p;
  266.      while(i<n&&a[i]<=a[i+1]) i++; 
  267.      while(i<n)
  268.      {
  269.          p=i-1;      
  270.          do
  271.          {
  272.              x=a[i++];
  273.          }while(i<n&&x<=a[i]); 
  274.          Merge(a,0,p,i-1);
  275.      }
  276. //基数排序
  277. //O(d(n+rd)) 
  278. void RadixSort(int *a,int n,int d) //d为a[]内最大元素的位数
  279. {
  280.     list<int> lst[10];
  281.     int i,j,m,k=1;
  282.     for(i=0;i<d;i++,k*=10)
  283.     {
  284.         for(j=0;j<n;j++)
  285.             lst[a[j]/k%10].push_back(a[j]);
  286.         for(j=0,m=0;j<10;j++)
  287.             while(!lst[j].empty())
  288.             {
  289.                 a[m++]=lst[j].front();
  290.                 lst[j].pop_front();
  291.             }
  292.     }
  293. }
  294. int main()
  295. {
  296. int n,*a;
  297. cin>>n;
  298. a=new int[n];
  299. for(int i=0;i<n;i++)
  300.     cin>>a[i];
  301. //InsertSort(a,n);  
  302. //BInsertSort(a,n);
  303. //P2_InsertSort(a,n);
  304. //ShellSort(a,n);
  305. //QuickSort(a,0,n);
  306. //bubble_sort(a,n);
  307. //SelectSort(a,n);
  308. //SLinkSort(a,n);
  309. //MergeSort(a,n);
  310. //UMergeSort(a,n);
  311. NaturalMSort(a,n);
  312. //HeapSort(a,n);
  313. //RadixSort(a,n,5);
  314. Output(a,n);
  315. delete a;
  316. return 0;
  317. }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值