-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathselectionsort.cpp
More file actions
55 lines (38 loc) · 1.15 KB
/
selectionsort.cpp
File metadata and controls
55 lines (38 loc) · 1.15 KB
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
#include <iostream>
using namespace std;
int selectSort(int [], int);
void selectShow(const int [], int);
int counter = 0;
int main() {
const int SIZE = 10;
int values[SIZE] = {10, 1, 9, 2, 8, 3, 7, 4, 6, 5};
cout << "This is the unsorted arry:" << endl;
selectShow(values, SIZE);
selectSort(values, SIZE);
cout << "This is the sorted array:" << endl;
selectShow(values, SIZE);
cout << "The loop ran " << counter << " times.";
return 0;
}
int selectSort(int array[], int SIZE) {
int startScan, minIdex, minValue;
for (startScan = 0; startScan < (SIZE - 1); ++startScan) {
minIdex = startScan;
minValue = array[startScan];
for(int index = startScan + 1; index < SIZE; index++) {
if (array[index] < minValue) {
minValue = array[index];
minIdex = index;
}counter++;
}
array[minIdex] = array[startScan];
array[startScan] = minValue;
return counter;
}
}
void selectShow(const int array[], int size)
{
for (int count = 0; count < size; count++)
cout << array[count] << " ";
cout << endl;
}