首頁  >  文章  >  後端開發  >  使用歸併排序演算法編寫的C/C++程序,用於計算數組中的逆序數

使用歸併排序演算法編寫的C/C++程序,用於計算數組中的逆序數

PHPz
PHPz轉載
2023-08-25 19:33:281067瀏覽

使用歸併排序演算法編寫的C/C++程序,用於計算數組中的逆序數

陣列的反轉表示;需要進行多少次變更才能將陣列轉換為其排序形式。當陣列已經排序時,需要 0 次反轉,而在其他情況下,如果陣列反轉,反轉次數將達到最大。

為了解決這個問題,我們將遵循歸併排序方法降低時間複雜度,採用分治演算法。

輸入

A sequence of numbers. (1, 5, 6, 4, 20).

輸出

將數字升序排列所需的反轉次數。

Here the number of inversions are 2.
First inversion: (1, 5, 4, 6, 20)
Second inversion: (1, 4, 5, 6, 20)

演算法

merge(array, tempArray, left, mid, right)

輸入 - 兩個數組,誰已經合併,左,右和中間索引。

輸出-依排序順序合併的陣列。

Begin
   i := left, j := mid, k := right
   count := 0
   while i <= mid -1 and j <= right, do
      if array[i] <= array[j], then
         tempArray[k] := array[i]
         increase i and k by 1
      else
         tempArray[k] := array[j]
         increase j and k by 1
         count := count + (mid - i)
   done
   while left part of the array has some extra element, do
      tempArray[k] := array[i]
      increase i and k by 1
   done
   while right part of the array has some extra element, do
      tempArray[k] := array[j]
      increase j and k by 1
   done
   return count
End

mergeSort(array, tempArray, left, right)

輸入 - 給定陣列和暫存數組,數組的左右索引。

輸出 - 排序後的逆序對數量。

Begin
   count := 0
   if right > left, then
      mid := (right + left)/2
      count := mergeSort(array, tempArray, left, mid)
      count := count + mergeSort(array, tempArray, mid+1, right)
      count := count + merge(array, tempArray, left, mid+1, right)
   return count
End

範例

 即時示範

#include <iostream>
using namespace std;
int merge(int arr[], int temp[], int left, int mid, int right) {
   int i, j, k;
   int count = 0;
   i = left; //i to locate first array location
   j = mid; //i to locate second array location
   k = left; //i to locate merged array location
   while ((i <= mid - 1) && (j <= right)) {
      if (arr[i] <= arr[j]){ //when left item is less than right item
      temp[k++] = arr[i++];
      } else {
         temp[k++] = arr[j++];
         count += (mid - i); //find how many convertion is performed
      }
   }
   while (i <= mid - 1) //if first list has remaining item, add them in the list
      temp[k++] = arr[i++];
   while (j <= right) //if second list has remaining item, add them in the list
      temp[k++] = arr[j++];
   for (i=left; i <= right; i++)
      arr[i] = temp[i]; //store temp Array to main array
   return count;
}
int mergeSort(int arr[], int temp[], int left, int right){
   int mid, count = 0;
   if (right > left) {
      mid = (right + left)/2; //find mid index of the array
      count = mergeSort(arr, temp, left, mid); //merge sort left sub array
      count += mergeSort(arr, temp, mid+1, right); //merge sort right sub array
      count += merge(arr, temp, left, mid+1, right); //merge two sub arrays
   }
   return count;
}
int arrInversion(int arr[], int n) {
   int temp[n];
   return mergeSort(arr, temp, 0, n - 1);
}
int main() {
   int arr[] = {1, 5, 6, 4, 20};
   int n = 5;
   cout << "Number of inversions are "<< arrInversion(arr, n);
}

輸出

Number of inversions are 2

以上是使用歸併排序演算法編寫的C/C++程序,用於計算數組中的逆序數的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:tutorialspoint.com。如有侵權,請聯絡admin@php.cn刪除