简单选择排序,selectSort

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

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;
}

你可能感兴趣的:(算法,数据结构)