-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathselect_sort.cpp
More file actions
77 lines (63 loc) · 1.69 KB
/
select_sort.cpp
File metadata and controls
77 lines (63 loc) · 1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# include <iostream>
using namespace std;
// 使用简单选择算法排序
int main(){
int input_data[20];
// 输入部分
{
for(int i = 0; i < 20; i++)
{
/* code */
try
{
cout << "please input the value of " << i << endl;
cin >> input_data[i];
/* code */
}
catch(const std::exception& e)
{
std::cerr << e.what() << '\n';
}catch(io_errc e){
cout << e;
}
}
cout << endl << "input complete!" << endl;
cout << "input result:";
for(int i = 0; i < 20; i++)
cout << input_data[i] << ", ";
cout << endl;
}
// 算法部分
int flag;
for(int i = 0; i < 19; i++)
{
flag = i;
for(int j = i +1; j < 20; j++)
{
/* code */
if (input_data[j] < input_data[flag]) {
/* code */
flag = j;
}
}
//交换部分
if (flag != i) {
int p = input_data[i];
input_data[i] = input_data[flag];
input_data[flag] = p;
cout << "the sort of " << i + 1 << ":";
for(int i = 0; i < 20; i++)
cout << input_data[i] << ", ";
cout << "swap:" << input_data[i] << "<-->" << input_data[flag] << endl;
}
}
// 输出部分
{
cout << "sort completed!" << endl;
for(int i = 0; i < 20; i++)
{
cout << "the value of " << i << ": "<< input_data[i] << endl;
}
}
return 0;
}