-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSelection Sort.cpp
45 lines (34 loc) · 895 Bytes
/
Selection Sort.cpp
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
/***********************************
* Time Complexity
* Worst complexity: O(n^2)
* Average complexity: O(n^2)
* Best complexity: O(n^2)
* Space complexity: O(1)
**********************************/
#include<bits/stdc++.h>
using namespace std;
void printArray(int arr[], int st, int en) {
for(int i = st; i < en; ++i)
cout << arr[i] << " ";
cout << endl;
}
void selectionSort(int arr[], int st, int en) {
for(int i = st; i < en - 1; ++i) {
int pos = i;
for(int j = i+1; j < en; ++j) {
if(arr[j] < arr[pos])
pos = j;
}
swap(arr[i], arr[pos]);
printArray(arr, 0, en);
}
}
int main() {
int n, arr[100];
cin >> n;
for(int i = 0; i < n; ++i)
cin >> arr[i];
selectionSort(arr, 0, n);
printArray(arr, 0, n);
return 0;
}