Skip to content

Commit

Permalink
Create SleepSort.cpp
Browse files Browse the repository at this point in the history
Implementation of sleep sort in c++.
  • Loading branch information
navdeepkk authored Jul 16, 2019
1 parent a711788 commit cc4d899
Showing 1 changed file with 51 additions and 0 deletions.
51 changes: 51 additions & 0 deletions sort_search_problems/SleepSort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
//This is the most simple implementation of sleeping sort in c++//
#include<iostream>
#include<thread>
#include<vector>
#include <chrono>
using namespace std;

void sleepSort(int*, int);
void sleep(int);

int main()
{
//initialize an array of integers to be sorted
// Doesn't work for negative numbers
int arr[] = { 3,2,1 };
int n = sizeof(arr) / sizeof(arr[0]);

//find the max element in arr
//Why to ding max? Read below
int max = arr[0];
for (int i = 0; i < n; i++) {
if (arr[i] > max) max = arr[i];
}
max++;

//call the function SLEEPSORT()
sleepSort(arr, n);

//make the main thread to go to sleep for 1 second more then
//the largest number in the list.
//this ensures all the threads are over before main finishes execution.
std::this_thread::sleep_for(std::chrono::seconds(max));


return(0);
}

void sleep( int i) {
//make the thread to sleep for I seconds.
std::this_thread::sleep_for(std::chrono::seconds(i));
cout << i << " ";
}

void sleepSort(int* arr, int n) {
thread* threads;
threads = new thread[n];
for (int i = 0; i < n; i++) {
//a new thread is created for each element of an array, which goes to sleep for arr[i] seconds.
threads[i] = (thread(sleep, arr[i]));
}
}

0 comments on commit cc4d899

Please sign in to comment.