-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathselection-sort.cpp
More file actions
65 lines (52 loc) · 1.34 KB
/
Copy pathselection-sort.cpp
File metadata and controls
65 lines (52 loc) · 1.34 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
56
57
58
59
60
61
62
63
64
65
#include <iostream>
using namespace std;
void pr(int arr[], int len) {
int i = 0;
for (i = 0; i < len; i++) {
cout << arr[i] << "\t";
}
cout << endl;
}
void pr_h(int arr[], int len, int highlight_1, int highlight_2) {
int i = 0;
for (i = 0; i < len; i++) {
if (i == highlight_1 || i == highlight_2)
cout << "(" << arr[i] << ")" << "\t";
else
cout << arr[i] << "\t";
}
cout << endl;
}
void swap(int arr[], int x, int y) {
int temp = arr[x];
arr[x] = arr[y];
arr[y] = temp;
}
void selection_sort(int arr[], int len) {
int i, j, sel, min;
cout << "Input Array : " << endl;
pr(arr, len);
for (i = 0; i < len; i++) {
min = arr[i];
sel = i;
for (j = i + 1; j < len; j++) {
if (min > arr[j]) {
sel = j;
min = arr[j];
}
}
if (sel != i)
swap(arr, i, sel);
cout << endl << "Pass " << i + 1 << " : ";
pr_h(arr, len, i, sel);
}
cout << "Sorted Array : " << endl;
pr(arr, len);
}
int main() {
// int arr[] = {10, 3, 5, 12, 8};
int arr[] = {1000, 100, 99, 98, 50, 233, 12, 90, 112, 97, 95};
int len = sizeof(arr) / sizeof(int);
selection_sort(arr, len);
return 1;
}