-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathselection_sort.cpp
52 lines (45 loc) · 1.15 KB
/
selection_sort.cpp
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
/* Summary: 选择排序
* Author: Amusi
* Date: 208-06-22
*
* Reference:
* https://en.wikipedia.org/wiki/Selection_sort
* https://github.com/xtaci/algorithms/blob/master/include/selection_sort.h
* 选择排序说明:首先在未排序序列中找到最小(大)元素,存放到排序序列的起始位置,然后,再从剩余未排序元素中继续寻找最小(大)元素,然后放到已排序序列的末尾。以此类推,直到所有元素均排序完毕。
*
*/
#include <iostream>
// 选择排序函数
namespace alg{
template<typename T>
static void SelectionSort(T list[], int length)
{
// 外循环: length-1次,因为当length-1个元素排序好后,第length个元素位置不再变化
for (int i = 0; i < length-1; ++i)
{
int minIndex = i;
// 从i的位置,进行遍历,因为前i-1个元素已经排序好
for (int j = i; j < length; ++j)
{
// 每次从未排序的数组中选出最小的值放入已排序的数组中,即从小到大排序
if (list[j] < list[minIndex])
{
minIndex = j;
}
}
int temp = list[minIndex];
list[minIndex] = list[i];
list[i] = temp;
}
}
}
using namespace std;
using namespace alg;
int main()
{
int a[8] = { 5, 2, 5, 7, 1, -3, 99, 56 };
SelectionSort<int>(a, 8);
for (auto e : a)
std::cout << e << " ";
return 0;
}