Interview question 29: Reverse Order pairs in the array

Source: Internet
Author: User

Brute Force Method: Scan each number in the array to compare the size of the number one by one with the number following it. If the number following it is smaller than its size, it is a reverse order.
Improvement Method: Divide the array into sub-arrays. First, calculate the reverse logarithm of the sub-array, and then calculate the reverse logarithm of the two adjacent sub-arrays. In the process of counting Reverse Order pairs, the array needs to be sorted. Obviously, the sorting process is Merge Sorting. The time complexity is reduced from O (N ^ 2) to O (nlogn), but the space complexity of O (n) is required.

Code:

# Include "stdafx. H "# include <iostream> # include <assert. h> using namespace STD; // combine two sorted arrays narr [nstart, nmid] And narr [nmid + 1, nend] void merge (INT narr [], int nstart, int nmid, int nend, int * ptemp, Int & ncount) {// set the two cursors to point to the elements in the two subarrays. The initial State points to the last element int ncur1 = nmid; int ncur2 = nend; int ntempindex = nend; // traverses two subarrays at the same time and puts them in the temporary array in sequence while (ncur1> = nstart) & (ncur2> = nmid + 1) {If (narr [ncur1]> narr [ncur2]) {ncount + = Ncur2-nmid; // note the code ptemp [ntempindex --] = narr [ncur1 --];} else {ptemp [ntempindex --] = narr [ncur2 --];}. while (ncur1> = nstart) {ptemp [ntempindex --] = narr [ncur1 --];} while (ncur2> = nmid + 1) {ptemp [ntempindex --] = narr [ncur2 --];} // assign the elements in the temporary array to the original array for (INT I = nstart; I <= nend; I ++) {narr [I] = ptemp [I] ;}// merge the sorted void reverseordercount (INT narr [], int nstart, int nend, int * ptemp, int & ncount) {assert (narr! = NULL & nstart> = 0 & nend> = 0); If (nstart <nend) {int nmid = (nstart + nend)> 1; reverseordercount (narr, nstart, nmid, ptemp, ncount); // sort the First Half of the array by merging and reverseordercount (narr, nmid + 1, nend, ptemp, ncount ); // merge and sort the second half of the array in Merge (narr, nstart, nmid, nend, ptemp, ncount ); // merge two sorted arrays} // calculate the reverse order of int reverseordercount (INT narr [], int nlength) in the array) {If (narr = NULL | nlength <0) {return 0;} // apply for a temporary array of the same length * ntemp = new int [nlength]; int ncount = 0; reverseordercount (narr, 0, nLength-1, ntemp, ncount); Delete [] ntemp; ntemp = NULL; return ncount ;} // print the reverse logarithm of the array and the void printmessage (INT narr [], int nlength) {cout <"Number of Reverse Order pairs:" <reverseordercount (narr, nlength) <Endl; cout <"sorted array:"; for (INT I = 0; I <nlength; I ++) {cout <narr [I] <"" ;}cout <Endl ;}int _ tmain (INT argc, _ tchar * argv []) {// array int narr1 [5] = {7, 5, 6, 4, 8}; printmessage (narr1, 5 ); // incrementing array int narr2 [5] = {4, 5, 6, 7, 8}; printmessage (narr2, 5); // decreasing array int narr3 [5] = {8, 7, 6, 5, 4}; printmessage (narr3, 5); // array with repeated numbers int narr4 [5] = {7, 4, 8, 8}; printmessage (narr4, 5 ); // only the digits int narr5 [2] = {7, 5}; printmessage (narr5, 2); int narr6 [1] = {4}; printmessage (narr6, 1); System ("pause"); Return 0 ;}

Running 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.