-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathselection.cpp
More file actions
57 lines (48 loc) · 975 Bytes
/
selection.cpp
File metadata and controls
57 lines (48 loc) · 975 Bytes
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
/*
Selection sort
for i = 1:n,
k = i
for j = i+1:n, if a[j] < a[k], k = j
→ invariant: a[k] smallest of a[i..n]
swap a[i,k]
→ invariant: a[1..i] in final position
end
*/
#include <iostream>
typedef unsigned int UINT;
void selectionSort(int array[], UINT size)
{
// first iteration to find the lowest value
for (UINT i = 0; i < size; i++)
{
if (array[0] > array[i])
{
int temp = array[0];
array[0] = array[i];
array[i] = temp;
}
}
for (UINT i = 1; i < size; i++)
{
for (UINT j = i+1; j < size; j++)
{
if (array[i] > array[j])
{
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
}
}
int main()
{
int test[11] = {3, 6, 1, 4, 0, 8, 2, 9, 10, 5, 7};
UINT usize = sizeof(test) / sizeof(test[0]);
selectionSort(test, usize);
for (UINT i = 0; i < usize; ++i)
{
std::cout << test[i] << std::endl;
}
return 0;
}