算法导论Problems 2-4: Inversions

本文介绍了一种基于归并排序算法修改的方法来高效计算数组中的逆序数个数。通过调整归并排序过程,可以在O(n log n)的时间复杂度内完成计算,适用于大数据集的逆序数统计。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

算法导论上Problems 2-4是一道求逆序数个数的问题,原题目是这样的:

Let A[1 ‥ n] be an array of n distinct numbers. If i < j and A[i] > A[j], then the pair (i, j) is called an inversion of A.

  1. List the five inversions of the array 〈2, 3, 8, 6, 1〉.

  2. What array with elements from the set {1, 2, . . . , n} has the most inversions? How many does it have?

  3. What is the relationship between the running time of insertion sort and the number of inversions in the input array? Justify your answer.

  4. Give an algorithm that determines the number of inversions in any permutation on n elements in Θ(n lg n) worst-case time. (Hint: Modify merge sort.)

下面是根据题目提示,修改归并排序后的代码

   1: #include <iostream>
   2: #include <vector>
   3: using namespace std;
   4:  
   5: template<typename T>
   6: int mergeInversions(vector<T>& array,vector<T>& tempArray,int left,int mid,int right)
   7: {
   8:     int l = left,r = mid+1;
   9:     int i =l;
  10:     int count=0;
  11:  
  12:     while(l<=mid&&r<=right)
  13:     {
  14:         //if a inversion found
  15:         if(array[l]>array[r])
  16:         {
  17:             //In left section ,for all the elements right to array[l], counter will count.    
  18:             count+=mid-l+1;    
  19:         }
  20:  
  21:         if(array[l]<=array[r])
  22:             tempArray[i++] = array[l++];
  23:         else
  24:             tempArray[i++] = array[r++];
  25:     }
  26:  
  27:     //copy the remaining elements in L and R to tempArray. 
  28:     while(l<=mid)
  29:         tempArray[i++]= array[l++];
  30:     while (r<=right)
  31:         tempArray[i++] = array[r++];
  32:     //copy the sorted elements back to the original array.
  33:     for(i=left;i<=right;i++)
  34:         array[i] = tempArray[i];
  35:     return count;
  36: }
  37:  
  38: template<typename T>
  39: int countInversions(vector<T>& array,vector<T>& tempArray,int left,int right)
  40: {
  41:     int count = 0;
  42:     if(left < right)
  43:     {
  44:         int mid = (left + right)/2;
  45:         count += countInversions(array,tempArray,left,mid);
  46:         count += countInversions(array,tempArray,mid+1,right);
  47:         count += mergeInversions(array,tempArray,left,mid,right);
  48:     }
  49:     return count;
  50: }
  51:  
  52: template<typename T>
  53: int countInversions(vector<T>& array)
  54: {
  55:     vector<T> tempArray(array.size());//A auxiliary vector will be used in merge function.
  56:     return countInversions(array,tempArray,0,array.size()-1);
  57: }
  58:  
  59: int main()
  60: {
  61:     vector<int> array(5);
  62:     array[0]= 5;
  63:     array[1]= 4;
  64:     array[2]= 3;
  65:     array[3]= 2;
  66:     array[4]= 1;
  67:     cout<<"Number of inversions:";
  68:     cout<<countInversions<int>(array)<<endl;
  69: }

留意着农场之外的长期职业生涯的可能性,奶牛Bessie开始在不同的在线编程网站上学习算法。 她到目前为止最喜欢的算法是“冒泡排序”。这是Bessie最初的对长度为 𝑁 N的数组 𝐴 A进行排序的奶牛码实现。 sorted = false while (not sorted): sorted = true moo for i = 0 to N-2: if A[i+1] < A[i]: swap A[i], A[i+1] sorted = false 显然,奶牛码中的“moo”指令的作用只是输出“moo”。奇怪的是,Bessie看上去执着于在她的代码中的不同位置使用这个语句。 在用若干个数组测试了她的代码之后,Bessie得到一个有趣的观察现象:大的元素很快就会被拉到数组末尾,然而小的元素需要很长时间“冒泡”到数组的开头(她怀疑这就是为什么这个算法得名的原因)。为了实验和缓解这一问题,Bessie试着修改了她的代码,使代码在每次循环中向前再向后各扫描一次,从而无论是大的元素还是小的元素在每一次循环中都有机会被拉较长的一段距离。她的代码现在是这样的: sorted = false while (not sorted): sorted = true moo for i = 0 to N-2: if A[i+1] < A[i]: swap A[i], A[i+1] for i = N-2 downto 0: if A[i+1] < A[i]: swap A[i], A[i+1] for i = 0 to N-2: if A[i+1] < A[i]: sorted = false 给定一个输入数组,请预测Bessie修改后的代码会输出多少次“moo”。 输入格式 输入的第一行包含 𝑁 N( 1 ≤ 𝑁 ≤ 100 , 000 1≤N≤100,000)。接下来 𝑁 N行描述了 𝐴 [ 0 ] … 𝐴 [ 𝑁 − 1 ] A[0]…A[N−1],每个数都是一个范围为 0 … 1 0 9 0…10 9 的整数。输入数据不保证各不相同。 输出格式 输出“moo”被输出的次数。 样例 #1 样例输入 #1 5 1 8 5 3 2 样例输出 #1 2
最新发布
03-16
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值