forked from mandliya/algorithms_and_data_structures
-
Notifications
You must be signed in to change notification settings - Fork 0
/
selectionSort.h
48 lines (43 loc) · 1.94 KB
/
selectionSort.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
/***
* _ __ ___ _
* | |/ /___ ___ _ __ / __|__ _| |_ __
* | ' </ -_) -_) '_ \ | (__/ _` | | ' \
* |_|\_\___\___| .__/ \___\__,_|_|_|_|_|
* |_|
* _
* __ _ _ _ __| |
* / _` | ' \/ _` |
* \__,_|_||_\__,_|
*
* _ _ _ _ _ _
* | | ___ __ _ _ _ _ _ /_\ | |__ _ ___ _ _(_) |_| |_ _ __ ___
* | |__/ -_) _` | '_| ' \ / _ \| / _` / _ \ '_| | _| ' \| ' \(_-<
* |____\___\__,_|_| |_||_| /_/ \_\_\__, \___/_| |_|\__|_||_|_|_|_/__/
* |___/
* Yeah! Ravi let's do it!
*/
#ifndef SELECTION_SORT_H
#define SELECTION_SORT_H
#include <cassert>
namespace algo {
template <typename T>
static void selectionSort( T list[ ], int start, int end )
{
assert( start <= end );
int i, j, minIndex;
for ( i = start; i <= end - 1; ++i ) {
//let us assume element at start index is smallest
minIndex = i;
for ( j = start + 1; j <= end; ++j ) {
if ( list[j] < list[minIndex] ) {
minIndex = j;
}
}
//swap the min element with the element at current position
if ( minIndex != i ) {
swap( list[i], list[minIndex] );
}
}
}
} //end of namespace algo
#endif