快速排序

来源:互联网 发布:vnr翻译软件 编辑:程序博客网 时间:2024/06/05 11:46

代码一.霍尔快排:

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
#include <iostream>using namespace std; int num; void swap(int &a, int &b){int temp = a;a = b;b = temp;} void PrintArray(int *arr){for(int i=1; i<=num; ++i)cout << arr[i] << " ";cout << endl;} int Partition1(int *arr, int beg, int end){int low = beg, high = end;int sentinel = arr[beg];while(low < high){while(low<high && arr[high]>=sentinel)--high;arr[low] = arr[high];while(low<high && arr[low]<=sentinel)++low;arr[high] = arr[low];}arr[low] = sentinel; cout << "排序过程:";PrintArray(arr);return low;} void QuickSort(int *arr, int beg, int end){if(beg < end){int pivot = Partition1(arr, beg, end);QuickSort(arr, beg, pivot-1);QuickSort(arr, pivot+1, end);}} int main(){int arr[100];cout << "Input the num of the elements:\n";cin >> num;cout << "Input the elements:\n";for(int i=1; i<=num; ++i)cin >> arr[i];QuickSort(arr, 1, num);cout << "最后结果:";PrintArray(arr);return 0;}

代码二.《算法导论》里讲的快排:

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
#include <iostream>using namespace std; int num; void swap(int &a, int &b){int temp = a;a = b;b = temp;} void PrintArray(int *arr){for(int i=1; i<=num; ++i)cout << arr[i] << " ";cout << endl;} int Partition2(int *arr, int beg, int end){int sentinel = arr[end];int i = beg-1;for(int j=beg; j<=end-1; ++j){if(arr[j] <= sentinel){i++;swap(arr[i], arr[j]);}}swap(arr[i+1], arr[end]); cout << "排序过程:";PrintArray(arr);return i+1;} void QuickSort(int *arr, int beg, int end){if(beg < end){int pivot = Partition2(arr, beg, end);QuickSort(arr, beg, pivot-1);QuickSort(arr, pivot+1, end);}} int main(){int arr[100];cout << "Input the num of the elements:\n";cin >> num;cout << "Input the elements:\n";for(int i=1; i<=num; ++i)cin >> arr[i];QuickSort(arr, 1, num);cout << "最后结果:";PrintArray(arr);return 0;}

代码三.随机快排:

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  #include <iostream>#include <cstdlib>using namespace std; int num; void swap(int &a, int &b){int temp = a;a = b;b = temp;} void PrintArray(int *arr){for(int i=1; i<=num; ++i)cout << arr[i] << " ";cout << endl;} int Partition3(int *arr, int beg, int end){int sentinel = arr[end];int i = beg-1;for(int j=beg; j<=end-1; ++j){if(arr[j] <= sentinel){i++;swap(arr[i], arr[j]);}}swap(arr[i+1], arr[end]); cout << "排序过程:";PrintArray(arr);return i+1;} int RandomPartition(int *arr, int beg, int end){int i = beg + rand() % (end-beg+1);swap(arr[i], arr[end]);return Partition3(arr, beg, end);}  void RandomQuickSort(int *arr, int beg, int end){if(beg < end){int pivot = RandomPartition(arr, beg, end);RandomQuickSort(arr, beg, pivot-1);RandomQuickSort(arr, pivot+1, end);}} int main(){int arr[100];cout << "Input the num of the elements:\n";cin >> num;cout << "Input the elements:\n";for(int i=1; i<=num; ++i)cin >> arr[i];RandomQuickSort(arr, 1, num);cout << "最后结果:";PrintArray(arr);return 0;}

最后,我想说下,随机化的快排一般适用于待排序的数据之间相差较大的情况下。

原创粉丝点击