简单选择排序,selectSort

// 简单选择排序,selectSort
// O(n^2),不稳定
#include <cstdio>

void selectSort(int *arr, int length)
{
    for(int i = 0; i < length-1; i++)
    {
        int index = i;
        for(int j = i+1; j < length; j++)
        {
            if(arr[index] > arr[j])
                index = j;
        }
        if(index != i)
        {
            int temp = arr[index];
            arr[index] = arr[i];
            arr[i] = temp;
        }
    }
}

int main()
{
    int arr[] = {9,3,8,4,0,2,6,7,5,1};
    int n = sizeof(arr)/sizeof(arr[0]);

    selectSort(arr, n);
    for(int i = 0; i < n; i++)
        printf("%d ",arr[i]);

    return 0;
}
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页