#include <iostream>
#include <string>
#include <stdlib.h>
using namespace std;
void swap(int &a,int &b);
int main()
{
const int num = 5;
int str[num];
cout<<"原数组:";
for(int i = 0;i<num;i++)
{
str[i] = num - i;
cout<<str[i]<<" ";
}
cout<<endl;
for(int i = 0;i<num-1;i++)
{
for(int j = i+1;j <num;j++)
{
if(str[i]>str[j])
swap(str[i],str[j]);
}
cout<<"第"<<i+1<<"次排序后:";
for(int k = 0;k<num;k++)
{
cout<<str[k]<<" ";
}
cout<<endl;
}
cout<<"最终结果为:";
for(int i = 0;i<num;i++)
{
cout<<str[i]<<" ";
}
cout<<endl;
system("pause");
return 0;
}
void swap(int &a,int &b)
{
int tmp;
tmp = a;
a = b;
b = tmp;
}
简单选择排序
最新推荐文章于 2024-10-21 22:48:57 发布