Uva10327-flip sort

Source: Internet
Author: User

Flip sort

Sorting in computer science is an important part. almost every problem can be solved already iently if sorted data are found. there are some excellent sorting algorithm which has already acheived the lower bound nlgn. in this problem we will also discuss about a new sorting approach. in this approach only one operation (FLIP) is available and that is you can exchange two adjacent terms. if you think a while, you will see that it is always possible to sort a set of numbers in this way.

The Problem

A set of integers will be given. now using the above approach we want to sort the numbers in ascending order. you have to find out the minimum number of flips required. such as to sort "1 2 3" we need no flip operation whether to sort "2 3 1" we need at least 2 flip operations.

The input

The input will start with a positive integer n (n <= 1000). In next few lines there will be n integers. input will be terminated by EOF.

The output

For each data set print "minimum exchange operations: M" Where m is the minimum flip operations required to perform sorting. Use a seperate line for each case.

Sample Input
3 1 2 332 3 1
Sample output
Minimum exchange operations : 0Minimum exchange operations : 2

 

 

The minimum number of interchange times for adjacent interactive sorting is reverse order.

The number of exchanges in Bubble Sorting is a reverse order pair.

 

# Include <cstdio> # include <cstring> # include <iostream> # include <string> # include <algorithm> using namespace STD; const int maxn = 1001; int A [maxn]; int N; // calculate the enumerated values in two repeating cycles in the reverse order of int solve () {int CNT = 0; For (INT I = 0; I <n-1; I ++) for (Int J = I + 1; j <n; j ++) if (a [I]> A [J]) CNT ++; return CNT;} int main () {# ifndef online_judge freopen (". /uva10327.in "," r ", stdin); # endif while (scanf (" % d ", & N )! = EOF) {for (INT I = 0; I <n; I ++) {scanf ("% d", A + I) ;}printf ("minimum exchange operations: % d \ n ", solve ();} return 0 ;}

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.