选择排序
#include <iostream>
#include <string>
#include <string.h>
#include <vector>
#include <map>
#include <set>
using namespace std;
void print(const vector<int>& vt_num)
{
for (const int& i : vt_num)
{
cout << i << ' ';
}
cout << '\n';
}
void deal_input(vector<int>& vt_num)
{
char str_pin[1024]{ 0 };
fgets(str_pin, 1024, stdin);
int str_pin_len = strlen(str_pin);
int loop_end = str_pin_len - 1;
int loop_flag = 0;
while (loop_flag < loop_end)
{
if (str_pin[loop_flag] != ' ')
{
vt_num.push_back(str_pin[loop_flag] - '0');
}
loop_flag++;
}
#ifdef _DEBUG
print(vt_num);
#endif
}
int main(int argc, char* argv[])
{
vector<int> vt_num;
deal_input(vt_num);
int vt_num_len = vt_num.size();
int loop_end = 0, vt_min_flag = 0;
while (loop_end < vt_num_len)
{
vt_min_flag = loop_end;
for (int i = loop_end;i < vt_num_len;i++)
{
if (vt_num[i] < vt_num[vt_min_flag])
{
vt_min_flag = i;
}
}
if (vt_min_flag != loop_end)
{
vt_num[vt_min_flag] += vt_num[loop_end];
vt_num[loop_end] = vt_num[vt_min_flag] - vt_num[loop_end];
vt_num[vt_min_flag] = vt_num[vt_min_flag] - vt_num[loop_end];
}
loop_end++;
}
print(vt_num);
}
/*
选择排序解释
把第一小的放第一个
把第二小的放第二个
把第三小的放第三个
···············
把第n小的放第n个
或者反过来,从最大的搞
*/