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 ;}