forked from fnplus/Algorithms-Hacktoberfest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcounting_sort.cpp
54 lines (37 loc) · 1.12 KB
/
counting_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
46
47
48
49
50
51
52
53
54
/* Created by SubCoder1 on 31-May-18.
Sorting Technique -- Counting Sort
Input -- A sequence of n numbers
Output -- Reordered into a definite sequence {Ascending(HERE)}
Time Complexity :-
Worst Case - O(N^2)
Best Case - O(N + RANGE)
*/
#include <iostream>
#include <vector>
#include <algorithm>
#include <chrono>
using namespace std::chrono;
using namespace std;
int main(){
int inpt;
vector<int> store;
vector<int>:: iterator it;
cout << "Unsorted Input -- ";
while(cin.peek() != '\n'){
cin >> inpt;
store.push_back(inpt);
}
auto start = high_resolution_clock::now();
it = max_element(store.begin(), store.end());
int range = *it;
vector<int> aux(range, 0);
for(auto i: store){ aux[i-1]+=1; }
cout << "Sorted Output -- ";
for(int i = 0; i < range; i++){
for(int j = 1; j <= aux[i]; j++){
cout << i+1 << " ";
}
}
auto end = high_resolution_clock::now();
cout << "Time Elapsed -- " << duration_cast<microseconds>(end - start).count() << " microS.\n";
}