ultra-quicksort--[merge sort, divide and conquer to seek reverse order

Source: Internet
Author: User

Description

In this problem, you has to analyze a particular sorting algorithm. The algorithm processes a sequence of n distinct integers by swapping, adjacent sequence elements until the sequence is Sorted in ascending order. For the input sequence
9 1 0 5 4,
Ultra-quicksort produces the output
0 1 4 5 9.
Your task is to determine what many swap operations Ultra-quicksort needs to perform in order to sort a given input sequenc E.

Input

The input contains several test cases. Every test case begins with a line this contains a single integer n < 500,000-the length of the input sequence. Each of the following n lines contains a single integer 0≤a[i]≤999,999,999, the i-th input sequence element. Input is terminated by a sequence of length n = 0. This sequence must is processed.

Output

For every input sequence, your program prints a single line containing an integer number OP, the minimum number of swap op Erations necessary to sort the given input sequence.

Sample Input

59105431230

Sample Output

60
Problem Solving Ideas:
The subject reveals the nature of the sort--elimination of reverse order. In reverse order, it refers to the A[i]>a[j (A[i],a[j]) that satisfies when i<j. Can prove that any two of the switching sequences
Adjacent elements, in reverse order to increase or decrease one. Then for a given sequence, the method of exchanging a pair of adjacent elements one at a time, the minimum number of times to be exchanged equals the number of reverse pairs in this sequence .
Then the question becomes how to find the inverse pair of a given sequence. Can be divided into a method of treatment:
Inverse logarithm of entire sequence = left half sequence reverse logarithm + right half sequence reverse logarithm + previous element in left half sequence, followed by inverse logarithm of one element in right half sequence.
Further consideration can be found that this process can be done at the same time as the merge sort, with the complexity of O (Nlogn).

Note : You can simply calculate that n elements are arranged in reverse order with a maximum of n (n-1)/2, which requires a long long.

The code is as follows:
1#include <iostream>2#include <cstdio>3#include <cstring>4#include <ctime>5 using namespacestd;6 #definePrint_time_ printf ("Time:%f\n", double (Clock ())/clocks_per_sec)7 #defineMAXN 5000008 inta[maxn+5];9 TentypedefLong LongLL; OneLL DC (intAintN) { A     intmid=a+n/2-1; -     intb=a+n-1; -     if(n==1) the         return 0; -      -LL X1=dc (A, n/2); -LL X2=DC (mid+1, B-mid); +LL x3=0; -     int*b=New int[N]; +     intI=a,j=mid+1, p=0; A      for(; i<=mid&&j<=b&&p<n;p++){ at         if(a[i]<=A[j]) { -b[p]=a[i++]; -         } -         Else { -b[p]=a[j++]; -x3+=mid-i+1; in         } -     } to      while(I<=mid) {b[p++]=a[i++];} +      while(j<=b) {b[p++]=a[j++];} -memcpy (A+a, B, nsizeof(int)); the     Delete[] B; *     returnx1+x2+X3; $ }Panax Notoginseng intMain () { -     intN; the      while(SCANF ("%d", &n) = =1&&N) { +          for(intI=0; i<n;i++) Ascanf"%d",&a[i]); theprintf"%lld\n", DC (0, N)); +     } -     //print_time_; $     return 0; $}



ultra-quicksort--[merge sort, divide and conquer to seek reverse order

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.