统计阵列中的逆序数


阵列的逆序表示:将阵列转换为排序形式需要多少次改变。当阵列已经排序时,它需要 0 次逆序,而在另一种情况下,如果阵列是逆序的,则逆序数将是最大值。

为解决此问题,我们将遵循合并排序法来降低时间复杂度,并将其制成分治算法。

输入和输出

Input:
A sequence of numbers. (1, 5, 6, 4, 20).
Output:
The number of inversions required to arrange the numbers into ascending order.
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(intarr[], 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;
}

intmergeSort(intarr[], 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;
}

intarrInversion(intarr[], int n) {
   int temp[n];
   return mergeSort(arr, temp, 0, n - 1);
}

int main() {
   intarr[] = {1, 5, 6, 4, 20};
   int n = 5;
   cout<< "Number of inversions are "<<arrInversion(arr, n);
}

输出

Number of inversions are 2

更新于:16-Jun-2020

429 浏览

开启你的职业生涯

完成该课程以获得认证

开始
广告