Sorting algorithms/Sleep sort: Difference between revisions
Content added Content deleted
m (→{{header|Scala}}: Did some formatting) |
|||
Line 114: | Line 114: | ||
=={{header|C++}}== |
=={{header|C++}}== |
||
{{works with|C++11}} |
|||
<lang cpp> |
<lang cpp> |
||
⚫ | |||
#include <iostream> |
#include <iostream> |
||
#include <thread> |
#include <thread> |
||
#include <vector> |
#include <vector> |
||
⚫ | |||
int main(int argc, char* argv[]) { |
|||
using namespace std; |
|||
⚫ | |||
⚫ | |||
void sortThread( int x ) |
|||
threads.emplace_back([i, &argv]() { |
|||
{ |
|||
int arg = std::stoi(argv[i]); |
|||
std::this_thread::sleep_for(std::chrono::seconds(arg)); |
|||
cout << |
std::cout << argv[i] << std::endl; |
||
} |
|||
⚫ | |||
⚫ | |||
for (auto& thread : threads) { |
|||
int main() |
|||
⚫ | |||
{ |
|||
} |
|||
⚫ | |||
srand( ( unsigned )time( NULL ) ); |
|||
cout << "unsorted:" << endl; |
|||
⚫ | |||
{ |
|||
int r = rand() % 20 + 5; |
|||
cout << r << " "; |
|||
thread* t = new thread( sortThread, r ); |
|||
threads.push_back( t ); |
|||
⚫ | |||
cout << endl << endl << "sorted:" << endl; |
|||
for( vector<thread*>::iterator t = threads.begin(); t != threads.end(); t++ ) |
|||
{ |
|||
⚫ | |||
delete ( *t ); |
|||
⚫ | |||
cout << endl << endl; |
|||
return 0; |
|||
} |
} |
||
</lang> |
</lang> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
⚫ | |||
unsorted: |
|||
6 |
|||
⚫ | |||
8 |
|||
9 |
|||
sorted: |
|||
14 |
|||
6 8 9 14 15 16 17 19 19 20 21 23 23 24 24 |
|||
15 |
|||
16 |
|||
17 |
|||
19 |
|||
19 |
|||
20 |
|||
21 |
|||
23 |
|||
23 |
|||
24 |
|||
24 |
|||
</pre> |
</pre> |
||