#include <iostream>
#include <algorithm>
using namespace std;
const int N = 100010;
int Arr[N];
void MySort(int *p, int nLen);
int main(int argc, char** argv)
{
int nLen;
scanf("%d",&nLen);
for(int i = 0; i < nLen; i++)
{
scanf("%d",&Arr[i]);
}
MySort(Arr,nLen);
for(int i = 0; i < nLen; i++)
{
printf("%d ",Arr[i]);
}
printf("\n");
return 0;
}
void MySort(int *p, int nLen)
{
int nMaxPos;//记录最大值的下标
//从数组最末尾开始
for(int i = nLen-1; i > 0; i--)
{
nMaxPos = i;
//找到最大的数的下标
for(int j = 0; j < i; j++)
{
if(Arr[j] > Arr[nMaxPos])
{
nMaxPos = j;
}
}
//最大数 和 当前位置i上的数交换
swap(Arr[i],Arr[nMaxPos]);
}
}
选择排序
最新推荐文章于 2024-07-24 20:44:05 发布