C language ranking algorithm and ranking algorithm, C Language Algorithm ranking

Source: Internet
Author: User

C language ranking algorithm and ranking algorithm, C Language Algorithm ranking

1 #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; // default first place
  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.
 twenty two {
 23 int i, j;
 24 int v;
 25 // sort body
 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 front is larger than the back, exchange.
 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 front is larger than the back, then swap.
 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 <N-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 <N; 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] = {7,2,6,3,3,7,1,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] =% db [% d] =% dc [% 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...


Related Article

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.