编辑代码

#include <stdlib.h>
#include <iostream>

using std::cout;
using std::endl;

#define N 10
#define M 100
#define SWAP(x,y) {int tmp=x; x=y; y=tmp;}

void print(int *a)
{
    for(int i=0; i<N; ++i)
    {
        printf("%3d", a[i]);
    }
    printf("\n");
}
void selectSort(int *a)
{
    for(int i=0; i<N-1; ++i)
    {
        //i从i号元素找到N-1号元素
        int minPos = i;
        for(int j=i+1; j<N; ++j)
        {
            //如果当前的值小于之前被认为的最小值
            if(a[minPos] > a[j])
            {
                minPos = j;
            }
        }
        //交换最小值和最前的位置
        SWAP(a[minPos], a[i]);
    }
}
int main()
{
    srand(time(NULL));
    int *a = (int*)malloc(sizeof(int)*N);
    
    for(int i=0; i<N; ++i)
    {
        a[i] = rand() % M;
    }
    print(a);
    
    selectSort(a);
    
    print(a);
    
    free(a);
    return 0;
}