Skip to content

JeromeBau/MultiprocessedSleepSort

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 

Repository files navigation

MultiprocessedSleepSort

The idea is simple and yet quite nice:

Sort a sequence of natural numbers by letting each number sleep for as many seconds as it is high and then add it to a list.

For example:
Sequence: [7, 2, 4]
First number sleeps 7 seconds and then adds to the result list.
Second number sleeps 2 seconds and then adds to the result list.
Third number sleeps 4 seconds and then adds to the result list.
Result: [2,4,7]

Credit: I did not come up with this idea. It seems to have originated from a 4chan board.

Implementation in Python using Multiprocessing

I use this little exercise to apply the basics of multiprocessing. It is important here to use pool.apply_async such that the workers don't wait for each other to finish.

To start sorting:

start_sorting(speed=0.1, to_be_sorted=[7, 3, 65, 2, 98])

where speed is the factor by which the sleep time is divided. If the sleep time is too short, the algorithm won't work due to the time it takes to assign the work and the time a worker needs to start the work.

Result:

[2, 3, 7, 65, 98]

About

A more creative way to sort natural numbers using sleep and multiprocessing

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages