-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSelectionSort.cpp
More file actions
39 lines (38 loc) · 1 KB
/
SelectionSort.cpp
File metadata and controls
39 lines (38 loc) · 1 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
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
// Selection sort has minimum swaps (n-1)
class SelectionSort
{
public:
void sort(vector<int> &arr) // time complexity is O(n^2)
{
for (size_t i = 0; i < arr.size() - 1; i++)
{
size_t minIndex = i;
for (size_t j = i + 1; j < arr.size(); j++)
{
if (arr.at(j) < arr.at(minIndex))
minIndex = j;
}
swap(arr.at(minIndex), arr.at(i));
}
}
};
// Disadvantages:
// 1. It's non adaptive, means it don't give any advantage when array is already sorted, would still takes O(n^2)
// 2. It's not stable , means it don't maintain the order of output as it was in input
int main()
{
vector<int> arr{5, 6, 8, 8, 8, 2};
SelectionSort sorting;
sorting.sort(arr);
cout << "[";
for (auto i : arr)
{
cout << i << " ";
}
cout << "]" << endl;
return 0;
}