-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathselection_sort.h
52 lines (45 loc) · 1.48 KB
/
selection_sort.h
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
/*******************************************************************************
* ALGORITHM IMPLEMENTAIONS
*
* /\ | _ _ ._ o _|_ |_ ._ _ _
* /--\ | (_| (_) | | |_ | | | | | _>
* _|
*
* SELECTION SORT
*
* In computer science, selection sort is a sorting algorithm, specifically an
* in-place comparison sort. It has O(n2) time complexity, making it inefficient
* on large lists, and generally performs worse than the similar insertion sort.
* Selection sort is noted for its simplicity, and it has performance advantages
* over more complicated algorithms in certain situations, particularly where
* auxiliary memory is limited.
*
* http://en.wikipedia.org/wiki/Selection_sort
******************************************************************************/
#ifndef ALGO_SELECTION_SORT_H__
#define ALGO_SELECTION_SORT_H__
#include <assert.h>
#include <generic.h>
namespace alg {
template<typename T>
static void SelectionSort(T list[], int start, int end) {
int i,j;
int iMin;
assert(start <= end);
for(j = start; j <= end-1; j++) {
// assume the min is the first element */
iMin = j;
// test against elements after i to find the smallest
for(i = j+1; i <= end; i++) {
if (list[i] < list[iMin]) {
iMin = i;
}
}
// iMin is the index of the minimum element. Swap it with the current position
if (iMin != j) {
swap(list[j], list[iMin]);
}
}
}
}
#endif //ALGO_SELECTION_SORT_H__