直接选择排序

直接选择排序:

直接选择排序实质是一种交换排序.每次从待排序序列中选取关键字最小的元素,与当前元素交换,直到全部排序.

时间复杂度: O(n^2)-->O(n^2)

空间复杂度:O(1)

是否稳定排序:不稳定

void selectSort(int *array, int n)
{
    for (int i = 0; i < n; ++i)
    {
        int smallIndex = i;
        for (int j = i+1; j < n; ++j)
        {
            if (array[smallIndex] > array[j])
            {
                smallIndex = j;
            }
        }
        
        if (i != smallIndex)
        {
            int temp = array[i];
            array[i] = array[smallIndex];
            array[smallIndex] = temp;        
        }
    }
}

 

posted @ 2016-07-11 11:01  715776681  阅读(116)  评论(0编辑  收藏  举报