-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathselectionSort.cpp
More file actions
62 lines (50 loc) · 932 Bytes
/
Copy pathselectionSort.cpp
File metadata and controls
62 lines (50 loc) · 932 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
58
59
60
61
62
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
const int SIZE = 32;
void print(int* input)
{
for(int i = 0; i < SIZE; i++)
cout << input[i] << " ";
cout << endl;
}
void selectionSort(int arr[])
{
int i, j, min, temp;
int count = 0;
for(i = 0; i < SIZE; i++)
{
min = i;
for(j = i+1; j < SIZE; j++)
{
if(arr[j] < arr[min])
min = j;
count++;
}
if(min != i)
{
temp = arr[i];
arr[i] = arr[min];
arr[min] = temp;
count++;
}
}
cout << "Counter is: " << count << endl;
}
int main() {
srand(time(NULL));
cout << "The array consists of " << SIZE << " elements: " << endl;
int input[SIZE] = { 0 };
for(int i = 0; i < SIZE; i++)
{
int j = rand() % 100+1;
input[i] = j;
}
cout << "Input: " << endl;
print(input);
selectionSort(input);
cout << "Output: " << endl;
print(input);
return 0;
}