/home/toolbox/public_html/solutions/103/10327/a.c
1 #include <stdio.h>
2 #include <string.h>
3 #include <sys/types.h>
4 #include <sys/stat.h>
5 #include <fcntl.h>
6 #include <stdint.h>
7 #include <math.h>
8 #include <stdlib.h>
9
10 #define TRUE (1 == 1)
11 #define FALSE (1 != 1)
12
13 #define DEBUG if (FALSE)
14
15 #define MAX_LINE 1004
16
17 /*
18 * Author: Isaac Traxler
19 * Date: 2016-08-31
20 * Purpose: fun
21 * Problem: 10327 - Flip Sort
22 */
23
24 /*
25 * This template reads lines of data at a time until end of file.
26 */
27
28 int cnt;
29 int a[MAX_LINE];
30
31 void init()
32 {
33 /* FUNCTION init */
34 } /* FUNCTION init */
35
36 void dump()
37 {
38 /* FUNCTION dump */
39 } /* FUNCTION dump */
40
41 int getInput()
42 {
43 /* FUNCTION getInput */
44 int dataReadFlag;
45 int tmp;
46 int i;
47
48 tmp = scanf(" %d ", &cnt);
49 dataReadFlag = 0 <= tmp;
50
51 if (dataReadFlag)
52 {
53 /* load array */
54 for (i=0; i<cnt; i++)
55 {
56 /* for */
57 scanf(" %d ", &a[i]);
58 } /* for */
59 } /* load array */
60
61 return (dataReadFlag);
62 } /* FUNCTION getInput */
63
64 void process()
65 {
66 /* FUNCTION process */
67 int flips = 0;
68 int prevFlips = -1;
69 int i;
70 int top;
71 int tmp;
72
73 top = cnt - 1;
74 while (flips != prevFlips)
75 {
76 /* while */
77 prevFlips = flips;
78 for (i=0; i<top; i++)
79 {
80 /* for */
81 if (a[i] > a[i+1])
82 {
83 /* flip */
84 flips++;
85 tmp = a[i];
86 a[i] = a[i+1];
87 a[i+1] = tmp;
88 } /* flip */
89 } /* for */
90 } /* while */
91 printf("Minimum exchange operations : %d\n", flips);
92 } /* FUNCTION process */
93
94 int main()
95 {
96 /* main */
97 int moreToDo;
98
99 init();
100 moreToDo = getInput();
101 while (moreToDo)
102 {
103 /* while */
104 process();
105 moreToDo = getInput();
106 } /* while */
107
108 return EXIT_SUCCESS;
109 } /* main */
110