-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathselection_sort.h
More file actions
41 lines (40 loc) · 1.64 KB
/
selection_sort.h
File metadata and controls
41 lines (40 loc) · 1.64 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
#include <iostream>
#include <stdint.h>
template<typename T>
void selection_sort(T* data, size_t num)
{
std::cout << "choose selection sort ." << std::endl;
// 每次循环结束,令 [current_loop] 的值为本次循环最小值
for (size_t current_loop = 0; current_loop < num - 1; current_loop++) {
#ifdef DEBUG
std::cout << "-------------- loop time " << current_loop + 1 << " start "
<< "---------------" << std::endl;
show_data(data, num);
#endif
size_t smaller_index = current_loop;
T* target_data = data + current_loop;
T* smaller_data = data + current_loop;
// 每次循环结束,找到本次最小值,记录在 smaller_data 中
for (size_t current_index = current_loop + 1; current_index < num - 1; current_index++) {
T* loop_data = data + current_index;
if (*loop_data < *smaller_data) {
smaller_data = loop_data;
}
}
#ifdef DEBUG
std::cout << "target data : " << *target_data << std::endl;
std::cout << "smaller data : " << *smaller_data << std::endl;
#endif
// target_data <==> smaller_data
T temp = *target_data;
*target_data = *smaller_data;
*smaller_data = temp;
#ifdef DEBUG
std::cout << "-------------- loop time " << current_loop + 1 << " result "
<< "--------------" << std::endl;
show_data(data, num);
std::cout << "-------------- loop time " << current_loop + 1 << " end "
<< "-----------------" << std::endl << std::endl;
#endif
}
}