算法导论Problems 2-4: Inversions
来源:互联网 发布:安卓翻墙软件 编辑:程序博客网 时间:2024/05/17 09:16
算法导论上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.
-
List the five inversions of the array 〈2, 3, 8, 6, 1〉.
-
What array with elements from the set {1, 2, . . . , n} has the most inversions? How many does it have?
-
What is the relationship between the running time of insertion sort and the number of inversions in the input array? Justify your answer.
-
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: }
- 算法导论Problems 2-4: Inversions
- 算法导论习题2-4 Inversions 逆序数
- 算法导论【思考题Problems 12-2: Radix trees 】
- 算法导论(Problems 6-2) d-ary heap(多叉树实现堆)
- 算法导论 2-4
- 算法导论 4-2
- 【算法导论学习-011】数组中的逆序对个数(Counting inversions in an array)
- 算法导论(problems 6-3 Young tableaus)
- 算法导论思考题2-4
- 算法导论2-1-4
- 算法导论 思考题 2-4
- 算法导论 思考题 2-4
- Inversions
- 算法导论之2-4思考题
- 算法导论(Exercise 2-4)
- 算法导论 4-4
- 算法导论 2-2
- 算法导论14.3-4
- 初学者的个人的一些经验,希望大家不要见笑 (2)
- JNI学习笔记2——本地代码访问Java代码
- 从“大厨”开发走向“麦当劳”开发模式---之系统工程方法引进与实施
- sae架构
- DistanceJoint
- 算法导论Problems 2-4: Inversions
- Android弹出拨号界面和拨打电话实现
- Android弹出拨号界面和拨打电话实现
- Android弹出拨号界面和拨打电话实现
- b2DebugDraw
- 析构函数是虚函数的优点(转)
- asp.net项目中通过Web.config配置文件及文件夹的访问权限!
- 如何更改Ubuntu开机等待时间
- day29 Servlet Http协议四个阶段 Servlet的编程步骤 WEB应用程序常见错误