现在的位置: 首页 > 综合 > 正文

选择排序

2018年09月17日 ⁄ 综合 ⁄ 共 563字 ⁄ 字号 评论关闭

void xf_Swap(int& x, int& y)
{
    int temp = x;
    x = y;
    y = temp;
}

/*
* 简单选择排序
*/

#include<iostream>
using namespace std;
int main()
{
    int num[10] = {9,8,10,3,5,6,4,7,2,1};
    cout<<"排序前:"<<endl;
    for (int m = 0;m < 10;m++)
    {
        cout<<num[m]<<" ";
    }
    for (int i = 0;i < 9;i++)
    {
        int pos = i;
        for (int j = i+1;j < 10;j++)
        {
            if (num[pos] > num[j])
            {
                pos = j;
            }
        }
        int tem;
        tem = num[pos];
        num[pos] = num[i];
        num[i] = tem;
    }
    cout<<endl<<"排序后:"<<endl;
    for (int n = 0;n < 10;n++)
    {
        cout<<num[n]<<" ";
    }
    return 0;
}

【上篇】
【下篇】

抱歉!评论已关闭.