-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
sleep_sort.cpp
53 lines (48 loc) · 991 Bytes
/
sleep_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
#include<iostream>
#include<thread>
#include<chrono>
#include<vector>
std::vector <int> Sorted;
void CountDown(int Sec)
{
std::this_thread::sleep_for(std::chrono::seconds(Sec));
Sorted.push_back(Sec);
}
void Multithread(int NumList[], int n){
std::vector <std::thread> Threads;
for (int i = 0; i <= n; i++)
{
Threads.push_back(std::thread(CountDown, NumList[i]));
}
for (auto& thread : Threads)
{
thread.join();
}
}
int main()
{
int n,i;
std::cin >> n;
int NumList[n];
for(i = 0; i < n; i++)
{
std::cin >> NumList[i];
}
Multithread(NumList, n);
for (auto& Num : Sorted)
{
std::cout << Num<<std::endl;
}
return 0;
}
/*
Sample Input : n = 3
89
12
26
Sample Output: 12
26
89
Time Complexity : O(NlogN + max(input)) where input is maximum of input array elements.
Space Complexity : All the things are done by the internal priority queue of the OS. Hence auxiliary space is ignored
*/