各種排序來源程式(附帶驅動程式,直接可以測試)

來源:互聯網
上載者:User

#include <iostream><br />#include <ctime><br />using namespace std;</p><p>void ArrayCopy(int a[],int b[],int n)<br />{<br />for(int i=0;i<n;i++)<br />b[i] = a[i];<br />}</p><p>void InsertSort(int a[],int b[],int n)<br />{<br />ArrayCopy(a,b,n); //還原原始的資料</p><p>int temp;<br />for(int i=1;i<n;i++)<br />{<br />temp = a[i];<br />int j = i-1;<br />while(j>=0 && a[j]>temp)<br />{<br />a[j+1] = a[j];<br />j--;<br />}<br />a[j+1] = temp;<br />}<br />cout<<"InsertSort:"<<endl;<br />for(int i=0;i<n;i++)<br />cout<<a[i]<<" ";<br />cout<<endl;<br />}</p><p>void InsertSortBin(int a[],int b[],int n)<br />{<br />ArrayCopy(a,b,n);<br />int j,low,high,mid;<br />int temp;<br />for(int i=1;i<n;i++)<br />{<br />temp = a[i];<br />low = 0;<br />high = i-1;<br />while(low <= high)<br />{<br />mid = low + (high-low)/2; //取中間位置<br />if(temp < a[mid])<br />high = mid-1; //插入點在左半區<br />else<br />low = mid+1; //插入點在右半區<br />}<br />j = i-1;<br />while(j >= high)<br />{<br />a[j+1] = a[j]; //記錄後移<br />j--;<br />}<br />a[high+1] = temp; //插入<br />}<br />cout<<"BinaryInsertSort:"<<endl;<br />for(j=0;j<n;j++)<br />cout<<a[j]<<" ";<br />cout<<endl;<br />}</p><p>void ShellSort(int a[],int b[],int n)<br />{<br />ArrayCopy(a,b,n);<br />int i,j;<br />int temp;<br />int d = n/2; //d取初值為n/2<br />while(d>0)<br />{<br />for(i=d;i<n;i++) //將R[d..n-1]分別插入各組當前有序區<br />{<br />j = i-d;<br />while(j>=0 && a[j]>a[j+d])<br />{<br />temp = a[j];<br />a[j] = a[j+d];<br />a[j+d] = temp;<br />j = j-d;<br />}<br />}<br />d /= 2; //遞減增量d<br />}<br />cout<<"ShellSort:"<<endl;<br />for(i=0;i<n;i++)<br />cout<<a[i]<<" ";<br />cout<<endl;<br />}</p><p>void BubbleSort(int a[],int b[],int n)<br />{<br />ArrayCopy(a,b,n);<br />int temp;<br />for(int i=0;i<n-1;i++)<br />{<br />for(int j=n-1;j>i;j--)<br />{<br />if(a[j]<a[j-1])<br />{<br />temp = a[j];<br />a[j] = a[j-1];<br />a[j-1] = temp;<br />}<br />}<br />}<br />cout<<"BubbleSort:"<<endl;<br />for(int i=0;i<n;i++)<br />cout<<a[i]<<" ";<br />cout<<endl;<br />}</p><p>void BubbleSortAdv(int a[],int b[],int n)<br />{<br />ArrayCopy(a,b,n);<br />int temp;<br />int exchange;<br />for(int i=0;i<n-1;i++)<br />{<br />exchange = 0;<br />for(int j=n-1;j>i;j--)<br />{<br />if(a[j]<a[j-1])<br />{<br />temp = a[j];<br />a[j] = a[j-1];<br />a[j-1] = temp;<br />exchange = 1;<br />}<br />}<br />//未發生交換,說明有序區已是全域有序了,則中途退出迴圈<br />if(exchange == 0)<br />break;<br />}<br />cout<<"Advanced BubbleSort:"<<endl;<br />for(int i=0;i<n;i++)<br />cout<<a[i]<<" ";<br />cout<<endl;<br />}</p><p>void QuickSort(int a[],int s,int t)<br />{<br />int i = s,j = t;<br />int temp;<br />if(s<t)<br />{<br />temp = a[i];<br />while(i != j)<br />{<br />while(j>i && a[j]>=temp)<br />j--;<br />a[i] = a[j];<br />while(i<j && a[i]<=temp)<br />i++;<br />a[j] = a[i];<br />}<br />a[i] = temp;<br />QuickSort(a,s,i-1);<br />QuickSort(a,i+1,t);<br />}<br />}</p><p>void SelectSort(int a[],int b[],int n)<br />{<br />ArrayCopy(a,b,n);<br />int temp,k;<br />for(int i=0;i<n-1;i++) //做第i趟排序<br />{<br />k = i;<br />for(int j=i+1;j<n;j++) //在[i...n-1]中選key最小的R[k]<br />{<br />if(a[j] < a[k])<br />k = j;<br />}<br />if(k != i)<br />{<br />temp = a[i];<br />a[i] = a[k];<br />a[k] = temp;<br />}<br />}<br />cout<<"Select Sort:"<<endl;<br />for(int i=0;i<n;i++)<br />cout<<a[i]<<" ";<br />cout<<endl;<br />}</p><p>//堆排序<br />//建最大堆:複雜度是O(logn)<br />void Sift(int a[],int low,int high)<br />{<br />int i = low;<br />int j = 2*i; //左孩子<br />int temp = a[i];<br />while(j<=high)<br />{<br />//右孩子大,指向右孩子<br />if(j<high && a[j]<a[j+1])<br />j++;<br />if(temp<a[j])<br />{<br />a[i] = a[j]; //將a[j]調整到雙親節點的位置上<br />i = j;<br />j = 2*i;<br />}<br />else<br />break;<br />}<br />a[i] = temp; //被篩選節點的值放入最終位置<br />}</p><p>void HeapSort(int a[],int b[],int n)<br />{<br />ArrayCopy(a,b,n);<br />int i,temp;</p><p>//迴圈建立初始化堆:這裡的複雜度為O(n)<br />for(i = n/2-1;i>=0;i--)<br />Sift(a,i,n-1);</p><p>//進行n-1次迴圈,完成堆排序<br />for(i=n-1;i>=1;i--)<br />{<br />//將第一個元素的值同當前區內的a[i]交換<br />temp = a[0];<br />a[0] = a[i];<br />a[i] = temp;</p><p>//篩選a[0]節點(下標從0到i-1),得到一個i個節點的堆<br />Sift(a,0,i-1);<br />}<br />cout<<"Heap Sort:"<<endl;<br />for(i=0;i<n;i++)<br />cout<<a[i]<<" ";<br />cout<<endl;<br />}</p><p>void Merge(int a[],int c[],int left,int m,int right)<br />{<br />int i = left;<br />int j = m+1;<br />int k = left;<br />while(i<=m && j<=right)<br />{<br />if(a[i] <= a[j])<br />c[k++] = a[i++];<br />else<br />c[k++] = a[j++];<br />}</p><p>while(i<=m)<br />c[k++] = a[i++];<br />while(j<=right)<br />c[k++] = a[j++];</p><p>}</p><p>void MergeSort(int a[],int c[],int left,int right)<br />{<br />if(left < right)<br />{<br />int i = left+(right-left)/2; //取二路分割點</p><p>MergeSort(a,c,left,i);<br />MergeSort(a,c,i+1,right);<br />//將a中已經排好序的兩半(以i為分割點)合并到數組b中<br />Merge(a,c,left,i,right); </p><p>//將數組c中排好序的部分複製到原始數組a中(這裡就說明了空間複雜度是O(n))<br />for(int i = left;i<=right;i++)<br />a[i] = c[i];<br />}<br />}</p><p>int main()<br />{<br />int N;<br />cout<<"Input Count:";<br />cin>>N;<br />int *a = new int[N];<br />int *b = new int[N];<br />srand((unsigned)time(NULL));<br />for(int i=0;i<N;i++)<br />{<br />a[i] = rand();<br />cout<<a[i]<<" ";<br />}<br />cout<<endl;</p><p>//插入排序<br />InsertSort(a,b,N);<br />InsertSortBin(b,a,N);<br />ShellSort(a,b,N);<br />//交換排序<br />BubbleSort(b,a,N);<br />BubbleSortAdv(a,b,N);</p><p>ArrayCopy(b,a,N);<br />QuickSort(a,0,N-1);<br />cout<<"Quick Sort:"<<endl;<br />for(int i=0;i<N;i++)<br />cout<<a[i]<<" ";<br />cout<<endl;</p><p>//選擇排序<br />SelectSort(a,b,N);<br />HeapSort(b,a,N);</p><p>//歸併排序<br />int* c = new int[N];<br />ArrayCopy(a,b,N);<br />MergeSort(a,c,0,N-1);<br />cout<<"Merge Sort:"<<endl;<br />for(int i=0;i<N;i++)<br />cout<<a[i]<<" ";<br />cout<<endl;<br />delete []c;</p><p>return 0;<br />}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.