Ranking algorithm and ranking algorithm in C language and ranking algorithm in C Language
# Include "stdio. h"
2 int search_second_max (int array [], int n, int m)
3 {
4 int max1;
5 int I, num;
6 num = 1; // The first place by default
7 if (m> n) return 0;
8 max1 = array [m];
9 for (I = 0; I <n; I ++)
10 {
11 if (array [I]> max1)
12 {
13 num ++;
14}
15
16}
17 return num;
18}
19
20
21 void sort (int * a, int l) // a is the array address, and l is the array length.
22 {
23 int I, j;
24 int v;
25 // sort the subject
26 for (I = 0; I <l-1; I ++)
27 for (j = I + 1; j <l; j ++)
28 {
29 if (a [I]> a [j]) // if the preceding value is greater than the following value, the result is exchanged.
30 {
31 v = a [I];
32 a [I] = a [j];
33 a [j] = v;
34}
35}
36}
37
38 // ranking
39 void PaiMing (int * a, int * B, int * c, int N)
40 {
41 int I, j;
42 int v;
43 int arrayindex [8] [2];
44
45 for (I = 0; I <8; I ++)
46 {
47 arrayindex [I] [0] = a [I];
48 arrayindex [I] [1] = I;
49}
50 // sort subjects from small to large
51 for (I = 0; I <N-1; I ++)
52 for (j = I + 1; j <N; j ++)
53 {
54 if (arrayindex [I] [0]> arrayindex [j] [0]) // if the preceding value is greater than the preceding value, the result is switched.
55 {
56 v = arrayindex [I] [0];
57 arrayindex [I] [0] = arrayindex [j] [0];
58 arrayindex [j] [0] = v;
59
60 v = arrayindex [I] [1];
61 arrayindex [I] [1] = arrayindex [j] [1];
62 arrayindex [j] [1] = v;
63
64}
65}
66
67 // ranking
68 for (I = 0, j = 1; I
69 {
70
71 if (arrayindex [I] [0]! = Arrayindex [I + 1] [0])
72 {
73 B [I] = j;
74 j ++;
75 B [I + 1] = j;
76} else
77 {
78 B [I] = j;
79 B [I + 1] = j;
80}
81}
82
83 for (I = 0; I
84 {
85 a [I] = arrayindex [I] [0];
86 c [I] = arrayindex [I] [1];
87}
88
89}
90
91
92 int main (void)
93 {
94 int a [8] = };
95 int num;
96 int I;
97 int B [8];
98 int c [8];
99
100 printf ("--------------------------------------- \ n ");
101
102 PaiMing (a, B, c, 8 );
103 for (I = 0; I <8; I ++)
104 {
105 printf ("a [% d] = % d B [% d] = % d c [% d] = % d \ n", I, a [I], i, B [I], I, c [I]);
106}
107 printf ("\ n ");
108 printf ("---------------------------------------- \ n ");
109
110
111
112
113}
Result output
---------------------------------------
A [0] = 1 B [0] = 1 c [0] = 6
A [1] = 2 B [1] = 2 c [1] = 1
A [2] = 3 B [2] = 3 c [2] = 4
A [3] = 3 B [3] = 3 c [3] = 3
A [4] = 6 B [4] = 4 c [4] = 2
A [5] = 7 B [5] = 5 c [5] = 5
A [6] = 7 B [6] = 5 c [6] = 0
A [7] = 8 B [7] = 6 c [7] = 7
----------------------------------------
--------------------------------
Process exited after 0.8098 seconds with return value 0
Press any key to continue...