选择排序算法C语言实现

选择排序C语言实现

int select_sort(int *list, int len)
{
    int i = 0;
    int j = 0;
    int tmp = 0;
    int smallest_loc = 0;

    if((NULL == list) || (0 == len))
    {
        return 1;
    }

    for(i=0; i<len; i++)
    {
        smallest_loc = i;
        for(j=i+1; j<len; j++)
        {
            if(list[smallest_loc] > list[j])
            {
                smallest_loc = j;
            }
        }
        if(smallest_loc != i)
        {
            printf("swap %d, %d --> list[%d], list[%d]", list[i], list[smallest_loc], i, smallest_loc);
            tmp = list[i];
            list[i] = list[smallest_loc];
            list[smallest_loc] = tmp;
            getchar();
        }
    }

    return 0;
}

 

posted @ 2021-02-08 20:10  sinodragon21  阅读(100)  评论(0编辑  收藏  举报