Introduction to algorithm 2.4 The algorithm of inverse order number __ in merging sort

Source: Internet
Author: User
Tags arrays sort what array

2-4 in reverse order

Set A[1...N] is an array that contains n different numbers. If, in the case of I<j, there is a[i]>a[j], then (I,J) is called an inverse pair in a.

(1) List The five reverse order of the array {2,3,8,6,1}.

(2) If the elements of the array are taken from {1,2...,n}, then what array contains the most inverse pairs.

(3) What is the relationship between the time the insertion is sorted and the number of reverse pairs in the input array.

(4) An algorithm that can use the worst run time of θ (n㏒n) to determine the number of reverse pairs in any permutation of n elements. (Hint: Modify merge Sort)

Answer:

(1) slightly

(2) Reverse array

(3) The time to insert a sort is linearly positive relative to the number of reverse pairs in the input array. By observing the algorithm pseudo-code of inserting sort, the running steps of the algorithm mainly depend on the number of elements moving in the inner loop, and each move means that the inverse number of the group is reduced by one, and the reverse number is zero when the sort ends.

(4) According to the Division of the law, if we decompose the array into two sub-sequences, two sub-sequences of the reverse order, and then find out the number of elements between two sub-sequences, you can get the whole array of reverse order number. The following considerations can be made:

Decomposition: Divide the problem into two arrays of size N/2

Solution: Solve the respective inverse logarithm respectively. If the size of the sub-problem is 2 or 1, it can be solved directly.

Merge: While knowing the inverse logarithm of the two subsequence sequences, the logarithmic logarithm between the two sub-sequences cannot be easily learned, and if 22 comparisons are made, the time complexity of the merging operation is N2, and the division method is meaningless.

Again considering the "merging" problem, if the two subsequence is ordered at this point, then by modifying the Merg process of the merge sort, you can get the number of reverse order between the subsequence: when Merg selects between the first element of two subsequence, the number of reverse orders is the same if the first element of the previous sequence is selected. The element does not form the inverse of the remaining elements in the latter sequence, and if the first element of the second sequence is selected, the number of reverse order increases "the number of elements left in the first sequence"-the element and each of the remaining elements in the previous sequence are reversed, and the reverse is eliminated after Merg. According to this idea of the split-treatment algorithm redesign as follows:

Decomposition: Divide the problem into two arrays of size N/2

Workaround: Recursively merge the sorting separately, and record the inverse logarithm that the additive sort eliminates. If the size of the sub-problem is 2 or 1, it can be solved directly.

Merge: Merge sorted by Merg, and add the reverse number in the Merg process as described above.

PS: The role of sequencing is not so obvious in the initial consideration of the problem (4) by the Division method, but through the analysis of "merging", it is required to produce "sort" side-effects for solving the problem of sub-problems. This "side effect" in the division of the law is worth noting.


Code:


Import Java.util.Arrays;

	public class Test {public static int count = 0;
		public static void Main (string[] args) {int arr[] = {6, 5, 4, 3, 2, 1};
		Int[] result = Sort_and_count (arr);
		SYSTEM.OUT.PRINTLN ("Reverse order Number:" +count);
		for (int i=0;i<result.length;i++) {System.out.print (result[i]+ "");
		}} private static int[] Sort_and_count (int[] arr) {if (arr.length = = 1) {return arr;
		} int length = Arr.length;
		int alength = LENGTH/2;
		int a[] = arrays.copyofrange (arr, 0, alength);
		int b[] = Arrays.copyofrange (arr, alength, length);
		A = Sort_and_count (a);
		b = Sort_and_count (b);
		arr = Merge_and_count (A, B);
	return arr;
		}//This function has two functions://(1) Merging in merge sort//(2) calculating inverse number private static int[] Merge_and_count (int[] A, int[] b) {int i = 0;
		int j = 0;
		int result[] = new Int[a.length+b.length];
		int current = 0;
				while (I < a.length && J < b.length) {if (A[i]<b[j]) {result[current++] = A[i];
			i++; } if (A[i]>b[j]) {result[current++] = B[j];
				Count + = (a.length-i);
			j + +; 
			}} if (I==a.length) {for (; j<b.length;j++) {result[current++] = b[j]; 
			}} if (J==b.length) {for (; i<a.length;i++) {result[current++] = A[i];
	}} return result;
 }
}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.