Number of reverse pairs in the statistical sequence C Language

Source: Internet
Author: User

<AlgorithmIntroduction> A Question in it. What I saw this morning.

For example, if a [0]> A [1], a [0] And a [1] are a reverse order. haha. as mentioned in the book, Merge Sorting is used. I implemented it.

The idea is clear and you are enjoying the process of studying algorithms.

/* 2-4-11-04-06-13.16.c -- Chapter 2, Topic 4 */<br/> # include <stdio. h> <br/> # include <stdlib. h> </P> <p> # define size (27) </P> <p> int main (void); <br/> int mergesort (int * const array, const int left, const int right); <br/> int Merge (int * const array, const int left, const int middle, const int right ); <br/> void printresult (const int * const array, const int size); </P> <p> int main (void) <br/> {<br/> int array [size] = {4, 2, 9, 7, 6, 3, 1, 5, 7, 54, 12, 87, 14, 62, 15, 0, 54, 33, 41, 87, 56, 43, 76, 53, 21, 46, 88}; <br/> int size = size; </P> <p> printf ("% d/N", mergesort (array, 0, size-1); <br/> printresult (array, size ); </P> <p> return 0; <br/>}</P> <p> int mergesort (int * const array, const int left, const int right) <br/>{< br/> static int COUNT = 0; <br/> int middle; </P> <p> If (left <right) <br/>{< br/> middle = (left + right)/2; <br/> mergesort (array, left, middle); <br/> mergesort (array, middle + 1, right); <br/> count + = Merge (array, left, middle, right); <br/> return count; <br/>}< br/> else <br/> return 0; <br/>}</P> <p> int Merge (int * const array, const int left, const int middle, const int right) <br/>{< br/> int * First, * Second; <br/> int flenth, slenth; <br/> int I, j, k; <br/> int count; </P> <p> flenth = middle-left + 1; <br/> slenth = right-middle; <br/> first = (int *) malloc (sizeof (INT) * flenth); <br/> second = (int *) malloc (sizeof (INT) * slenth); <br/> for (I = 0; I <flenth; I ++) <br/> first [I] = array [left + I]; <br/> for (j = 0; j <slenth; j ++) <br/> second [J] = array [Middle + J + 1]; <br/> COUNT = 0; <br/> I = J = 0; <br/> for (k = left; k <= right; k ++) <br/> {<br/> If (first [I] <= Second [J]) <br/> array [k] = first [I ++]; <br/> else <br/> {<br/> array [k] = Second [J ++]; <br/> count + = flenth-I; <br/>}< br/> if (I = flenth) <br/>{< br/> K ++; <br/> while (k <= right) <br/> array [k ++] = Second [J ++]; <br/> break; <br/>}< br/> else if (j = slenth) <br/>{< br/> K ++; <br/> while (k <= right) <br/> array [k ++] = first [I ++]; <br/> break; <br/>}< br/> free (first); <br/> free (second); </P> <p> return count; <br/>}</P> <p> void printresult (const int * const array, const int size) <br/>{< br/> int I; </P> <p> for (I = 0; I <size; I ++) <br/> printf ("%-3D", array [I]); <br/> putchar ('/N'); <br/>}

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.