选择排序(从大到小排序): 从数组中随机选择一个数,和数组中的其他成员挨个比较,如果这个数比数组中的数小就交换数据。

    技术2026-01-10  9

    #include <stdio.h> #define N 5

    int a[N];

    void show_data() {     int i;     for(i=0; i<N; i++){         printf("%d\t",a[i]);     }     puts("");

    }

    void input_data() {     int i;

        for(i=0; i<N; i++){         printf("please input %d number > ",i+1);         scanf("%d",&a[i]);     } }

    void selection_sort() {     int i,j,temp;

        for(i=0; i<N-1; i++){         for(j=i+1; j<=N-1; j++){             if(a[i] < a[j]){                 temp = a[i];                 a[i] = a[j];                 a[j] = temp;             }         }     } }

    int main(int argc, const char *argv[]) {     input_data();

        printf("selection sort before\n");     show_data();

        selection_sort();

        printf("selection sort after\n");     show_data();

        return 0; }

     

    Processed: 0.029, SQL: 9