Recalling the bubble sort

//
//  main.c
// Array - Bubble Sort
//
//  Created by LongMa on 2019/6/26.
//  Copyright © 2019. All rights reserved.
//

#include <stdio.h>

int main(int argc, const char * argv[]) {
    int a[10];
    for (int i = 0; i < 10; i++) {
        printf ( "% d Please enter integer, a total of 10:", i + 1);
        scanf("%d", &a[i]);
    }
    
    int temp = 0;
    
    // bubble: first, the biggest routed to the last; the second time (the last one without contrast), the second largest of the discharged second to last ...
    (Int i = 0; i <= 10 - 1 - 1; i ++) {// total number of 3 to 2 cycles => for the number n, n-1 times the cycle
        for (int j = 0; j <= 10 - 1 - 1 - i; j ++) {// i is the time 0, j + 1 maximum == 9, j == maximum 8
            if (a[j] > a[j + 1]) {
                temp = a[j];
                a[j] = a[j + 1];
                a[j + 1] = temp;
            }
        }
    }
    
    for (int k = 0; k < 10; k++) {
        printf("%d\t",a[k]);
    }
    return 0;
}

 log:

Please enter an integer, a total of 10: 9
Please enter the second integer, a total of 10: 8
Please enter integers, a total of 10: 7
Please enter the first four integers, a total of 10: 66
Please enter the first five integers, a total of 10: 55
Please enter the first six integers, a total of 10: 444
Please enter an integer of 7, a total of 10: 3
Please enter an integer of 8, a total of 10: 2
Please enter an integer of 9, a total of 10: 0
Please enter the first 10 integers, a total of 10: 111111
0	2	3	7	8	9	55	66	444	111111	Program ended with exit code: 0

 

Guess you like

Origin www.cnblogs.com/Dast1/p/11094547.html