使用隨機化的快速排序C++程式


快速排序技術透過將列表分成兩部分來完成。最初,樞軸元素由分割槽演算法選擇。樞軸的左側包含小於樞軸的值,右側包含大於樞軸的值。分割槽後,每個單獨的列表都使用相同的過程進行分割槽。

在本例中,我們隨機選擇樞軸元素。選擇樞軸後,我們進行分割槽,然後遞迴地排序陣列。

快速排序技術的複雜度

  • 時間複雜度 - 最佳情況和平均情況為 O(n log n),最壞情況為 O(n2)。

  • 空間複雜度 - O(log n)

輸入 - 未排序列表:90 45 22 11 22 50
輸出 - 排序後陣列:11 22 22 45 50 90

演算法

partition(array, lower, upper)

輸入 - 資料集陣列、下界和上界

輸出 - 樞軸在正確的位置

Begin
   index := lower
   pivot := higher
   for i in range lower to higher, do
      if array[i] < array[pivot], then
         exchange the values of array[i] and array[index]
         index := index + 1
   done
   exchange the values of array[pivot] and array[index]
End

random_pivot_partition(array, lower, upper)

輸入 - 資料集陣列、下界和上界

輸出 - 隨機選擇的樞軸的最終索引

Begin
   n := a random number
   pvt := lower + n mod (upper – lower + 1)
   exchange the values of array[pivot] and array[upper]
   index := Partition(array, lower, upper)
   return index
End

quickSort(array, left, right)

輸入 - 一個數據陣列,以及陣列的下界和上界

輸出 - 已排序的陣列

Begin
   if lower < right then
      q = random_pivot_partition(array, left, right).
      quickSort(array, left, q-1)
      quickSort(array, q+1, right)
End

示例程式碼

#include<iostream>
#include<cstdlib>
#include<ctime>
#define MAX 100
using namespace std;
void random_shuffle(int arr[]) {
   //function to shuffle the array elements into random positions
   srand(time(NULL));
   for (int i = MAX - 1; i > 0; i--) {
      int j = rand()%(i + 1);
      int temp = arr[i];
      arr[i] = arr[j];
      arr[j] = temp;
   }
}
// Partitioning the array on the basis of values at high as pivot value.
int Partition(int a[], int low, int high) {
   int pivot, index, i;
   index = low;
   pivot = high;
   for(i=low; i < high; i++) {
      // finding index of pivot.
      if(a[i] < a[pivot]) {
         swap(a[i], a[index]);
         index++;
      }
   }
   swap(a[pivot], a[index]);
   return index;
}
int RandomPivotPartition(int a[], int low, int high){
   // Random selection of pivot.
   int pvt, n, temp;
   n = rand();
   pvt = low + n%(high-low+1); // Randomizing the pivot value from sub-array.
   swap(a[high], a[pvt]);
   return Partition(a, low, high);
}
void quick_sort(int arr[], int p, int q) {
   //recursively sort the list
   int pindex;
   if(p < q) {
      pindex = RandomPivotPartition(arr, p, q); //randomly choose pivot
      // Recursively implementing QuickSort.
      quick_sort(arr, p, pindex-1);
    quick_sort(arr, pindex+1, q);
}
}
int main() {
int i;
int arr[MAX];
for (i = 0;i < MAX; i++)
arr[i] = i + 1;
random_shuffle(arr); //To randomize the array
quick_sort(arr, 0, MAX - 1); //sort the elements of array
for (i = 0; i < MAX; i++)
cout << arr[i] << " ";
cout << endl;
return 0;
}

輸出

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100

更新於:2019年7月30日

3K+ 次瀏覽

開啟你的職業生涯

完成課程獲得認證

開始學習
廣告
© . All rights reserved.