#include <QCoreApplication>
#include <iostream>
using namespace std;
//选择排序,大循环每次选择一个数与后面元素逐个比较
void mysort(char* src,int len){
int tem =0;
int exchanged = 1;//交换标志位
for(int i = 0; i < len && exchanged; i++){
//每轮比较之前重置交换标志位
exchanged = 0;
for(int j= i + 1; j < len; j++){
//从小到大>
//从大到小<
//src[i]依次与它后面元素比较
if(src[i] > src[j]){
//需要交换,置位标志位
exchanged = 1;
tem = src[i];
src[i] = src[j];
src[j] = tem;
}
}
}
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
char b2[]={11,5,4,7,3,0,9,10,22};
mysort(b2, 9);
cout<<b2<<endl;
for (auto j : b2) {
printf("从大到小 mysort out buff2: %d\n", j);
}
return a.exec();
}
选择排序
最新推荐文章于 2024-07-17 06:30:00 发布