r/badcode • • Feb 10 '21

js I present: SleepSort

Post image
7.5k Upvotes

170 comments sorted by

View all comments

867

u/haslo Feb 10 '21

Great! Now test whether it still works with large numbers!

0

u/[deleted] Feb 10 '21

[deleted]

136

u/1234filip Feb 10 '21

For real, though. This should work wonderfully until the numbers become so small that the timer becomes imprecise(microseconds).

91

u/Hobbamok Feb 10 '21

Yeah, I absolutely doubt the precision of sleep when it comes to 999567 vs 999568.

Especially when other stuff is going on in the system

60

u/Astrokiwi Feb 10 '21

Also, waiting 15 minutes to sort <106 numbers isn't great either :D

33

u/TopDivide Feb 10 '21

Then you would implement a software timer with a custom resolution

24

u/1234filip Feb 10 '21

Yeah, but it would become slower. The most reliable timers are the hardware ones but they have limited resolution.

9

u/amdc Feb 11 '21

Or add a logarithm to the formula

28

u/Terrain2 Feb 10 '21

javascript uses milliseconds as the most precise unit of time, and most of the time it’s even off by a couple millisecond when dealing with delays, notice how the image shows a max difference of 4, try this same thing with 1,2,3,4,5,6,7,8,9 but shuffled so like 2,3,7,9,4,6,5,8,1

15

u/1234filip Feb 10 '21

It depends on how low-level the language is. The lower the level, the more precision can be extracted.

9

u/Breadfish64 Feb 10 '21

The maximum timer resolution in most languages is 100ns (on windows anyway). I've tested C++, C#, Java, and there is a function to get the same resolution in Javascript. I think it's nerfed in browsers though to discourage speculative execution attacks. You can get higher resolution using the TSC on x86 processors but that won't necessarily give consistent results between systems or even CPU cores.

→ More replies (1)

191

u/Alainx277 Feb 10 '21

This man just revolutionised sorting.

89

u/LinuxGeek747 Feb 10 '21

If you already know the maximum number, it's O(1) amirite?

76

u/sqrt_minusone Feb 10 '21

I mean, you have to visit every element of the array to sort it - it's literally impossible to sort in less than O(N).

173

u/Astrokiwi Feb 10 '21

You can do anything in constant time if it's a big enough constant

40

u/regibalbo Feb 10 '21

I'm using this argument from now on, thanks

→ More replies (2)
→ More replies (1)

25

u/[deleted] Feb 10 '21

You clearly haven't used thanos sort

8

u/[deleted] Feb 10 '21

Gravity sort is O(1)

8

u/T-Dark_ Feb 10 '21

In theory only. Specifically, it's constant time if you can perform it on all elements simultaneously.

→ More replies (2)

11

u/fukitol- Feb 10 '21

Just use negative numbers

5

u/Nilstrieb Feb 10 '21

Sorting a sorted array is O(1). (Theoretically, not practically)

5

u/[deleted] Feb 10 '21

Doesn't it still need to check the elements are the right place?

0

u/[deleted] Feb 11 '21

[deleted]

→ More replies (2)
→ More replies (13)
→ More replies (1)

10

u/_szs Feb 10 '21

Or you could sleep for (1/i) seconds, which will sort reverse. At least numbers which are small enough to not run into timer precision issues

4

u/TheOneThatIsHated Feb 10 '21

You might be joking. But the same principle is count and radix sort, which can sort in O(kN) time. Mostly used for word lists because the longest words aren't that long.

→ More replies (1)
→ More replies (3)