Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the time complexity of the sleep sort?

Given this sort algorithm how do you express its time complexity?

Originally presented here (partial archive).

#!/bin/bash function f() { sleep "$1" echo "$1" } while [ -n "$1" ] do     f "$1" &     shift done wait  example usage: ./sleepsort.bash 5 3 6 3 6 3 1 4 7 
like image 357
justinhj Avatar asked Jun 24 '11 22:06

justinhj


People also ask

Does sleep sort work?

In general, sleep sort works by starting a separate task for each item to be sorted, where each task sleeps for an interval corresponding to the item's sort key, then emits the item. Items are then collected sequentially in time.

What is the time complexity of sort ()?

sort(Object[]) is based on the TimSort algorithm, giving us a time complexity of O(n log(n)).

What is the fastest sorting algorithm?

But since it has the upper hand in the average cases for most inputs, Quicksort is generally considered the “fastest” sorting algorithm.


2 Answers

O(max(input)+n)

The complexity just appears awkward to express because most sorting algorithms are data agnostic. Their time scales with the amount of data, not the data itself.

FWIW, as pointed out here, this is not a reliable algorithm for sorting data.

like image 129
blahdiblah Avatar answered Sep 30 '22 17:09

blahdiblah


One point which nobody seems to have addressed is how those sleeps are implemented. Ultimately they end up in a scheduler somewhere, and the operational complexity will depend on the scheduling algorithm used. For example, if the sleeps are put as events in a priority queue you will likely end up with something equivalent to heapsort, with complexity O(n log n). A naive scheduling algorithm might result in O(n^2).

like image 31
hammar Avatar answered Sep 30 '22 16:09

hammar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!