How bubble sort in C language?

Man of few words said, the code is as follows:

. 1 #include <stdio.h>
 2  
. 3  int main ()
 . 4  {
 . 5      int A [ 100 ], I, J, T, n-;
 . 6      the printf ( " Please enter the number of values to sort: " );
 . 7      scanf_s ( " % D " , n &);          // input a number n, indicating that the following have the number n 
. 8      for (I = . 1 ; I <= n; I ++ )
 . 9      {
 10          // loop to read the number n of the array in a 
. 11          the printf ( " Please enter the number to be sorted: \ n- " );
 12 is          scanf_s (" % D " , A & [I]);
 13 is      }
 14      // core bubble sorting section 
15      for (I = . 1 ; I <= n-; I ++) // n-number of sorting, only n-1 times 
16      {
 . 17          for (J = . 1 ; J <= n-- I; J ++)   // starting from the first one until the last comparison has not been normalized bit number 
18 is          {
 . 19              IF (a [J] <a [+ J . 1 ])   // size comparison and exchange 
20 is              {
 21 is                  T = A [J];
 22 is                  A [J] = A [+ J . 1 ];
 23 is                 a[j + 1] = t;
24             }
25         }
26     }
27     for (i = 1; i <= n; i++)
28     {
29         printf(" %d", a[i]);
30     }
31     getchar();
32     getchar();
33     return 0;
34 }

 Results are as follows:

The high and low scores to be sorted names, use a structure, as follows:

#define _CRT_SECURE_NO_DEPRECATE
#include<stdio.h>

// create a structure to store the names and scores 
struct MyStruct
{
    char name[21];
    char score;
};

int main ()
{
    struct MyStruct a[100], t;
    int i, j, n;
    Scanf ( " % D " , n &); // input a number n, the number to be sorted
     // cycle and read into n fractions Personal name 
    for (I = . 1 ; I <= n; I ++ )
    {
        scanf("%s %d",&a[i].name,&a[i].score);
    }
    // bubble sort by score in descending sort 
    for (I = . 1 ; I <= n-; I ++ )
    {
        for (J = . 1 ; J <n-; J ++)    // for comparison scores 
        {
             IF (A [J] .score <A [+ J . 1 ] .score)
            {
                t = a[j];
                a[j] = a[j + 1];
                a[j + 1] = t;
            }
        }
    }
    // output name 
    for (I = . 1 ; I <= n-; I ++ )
    {
        printf("%s\n", a[i].name);
    }
    getchar();
    getchar();
    return 0;
}

Results are as follows

 

Guess you like

Origin www.cnblogs.com/YanQing1998/p/11991743.html