选择排序_C语言_数组

选择排序_C语言_数组


#include <stdio.h>

void select_sort(int *);

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

    //初始化数组
    int a[10] = {1, 6, 8, 9, 3, 2, 4, 5, 7, 0};

    //乱序
    printf("乱序\n");
    for (int i = 0; i < 10; i ++ ) {
        printf("%d ",a[i]);
    }
    printf("\n\n");

    select_sort(a);

    //顺序
    printf("顺序\n");
    for (int i = 0; i < 10; i ++ ) {
        printf("%d ",a[i]);
    }
    printf("\n\n");

    return 0;
}

void select_sort(int * a) {
    int i = 0;
    int j;
    int k;
    int temp;

    for (i; i < 10; i ++) {
        temp = a[i];
        k = i;

        for (j = i + 1; j < 10; j++) {
            if (temp > a[j]) {
                temp = a[j];
                k = j;
            }
        }

        if (k != i) {
            temp = a[i];
            a[i] = a[k];
            a[k] = temp;
        }
    }
}
posted @ 2017-02-18 20:27  旭宝爱吃鱼  阅读(1635)  评论(0编辑  收藏  举报