-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSelectionSort.cpp
More file actions
37 lines (31 loc) · 925 Bytes
/
SelectionSort.cpp
File metadata and controls
37 lines (31 loc) · 925 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
#include "SelectionSort.h"
SelectionSort::SelectionSort(int aArrayOfNumbers[], unsigned int aArraySize) : ArraySorter( aArrayOfNumbers, aArraySize )
{}
//sorts the array with the "selection sort" algorithm
void SelectionSort::sort(std::ostream& aOStream)
{
//variable to store the minimum element
int min;
for (int i = 0; i < ( getRange() - 1 ); i++)
{
//sets first value to be minimun as it is has not been sorted
min = i;
//we compare the values within elements after i
for (int j = (i + 1); j < ( getRange() ); j++)
{
//if value is lower than min, the current index is recorded to store the new minimun
if (at(j) < at(min))
{
min = j;
}
}
//swaps the value in i with min, once the selection finishes
if ( at(min) != at(i) )
{
swapElements(i, min);
}
//outputs the current iteration
stepCompleted(aOStream);
aOStream << "\n";
}
}