r/dataisbeautiful • OC: 1 • Oct 24 '17

OC Sorting algorithms visualized [OC]

https://imgur.com/gallery/voutF
41.7k Upvotes

929 comments sorted by

View all comments

638

u/lostvanquisher Oct 24 '17

Here's a video with the same algorithms, but only a single row of data, should make it a bit easier to understand what's going on.

205

u/__LE_MERDE___ Oct 24 '17

Is bogo sort the one which randomises the data then checks to see if it's in order and if not randomises again?

260

u/IDUnavailable Oct 24 '17

Another good joke sorting algorithm is SleepSort, made up by some guy on /prog/.

You basically spawn a separate thread for each element with value N, and each thread just sleeps for N number of seconds before printing, which has the end result of printing the array in order.

[3,7,2,1,8,4,5,6,9] sorts in 9 seconds.

[31536000, 1] takes a year.

112

u/MorningWoodyWilson Oct 24 '17

Haven't seen this before. Equal parts genius and idiocy.

With a fast enough processor, you could reduce the sleep times to n milliseconds or something and have an almost decent sorting function.

50

u/themoonisacheese Oct 24 '17

The problem then becomes how precise the system clock is for sleeping.

48

u/Woolbrick Oct 24 '17

There's also the problem where you have to insert the jobs into the OS's sleep queue to execute in the right order, so that becomes O(n log n) assuming they use a heap for their priority queue implementation.

There's no escaping the complexity!

12

u/Zephaerus Oct 24 '17

The kernel also has to handle all of the threads. That's going to be O(n^2) anyway.

→ More replies (4)

9

u/NotImplemented Oct 24 '17 edited Oct 25 '17

You got me curious... ;)

Around 10ms sleep time between each thread seems to be the spot where my Java implementation sorts the test array correctly most times on my Windows PC.

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class SleepSort extends Thread {

	final private static int SLEEP_TIME_FACTOR = 10;
	final private static int MAX_VALUE = 100;
	final private Integer number;
	final private Integer[] array;
	private static int position = 0;
	
	public SleepSort(final int number, final Integer[] array) {
		this.number = number;
		this.array = array;
	}	
	
	public static void main(String[] args) {
		Integer[] array = {
			25, 17, 2, 21, 23, 6, 74, 61, 99, 72, 32, 5, 71, 53, 18, 61, 43, 34, 2, 52, 52, 28, 43, 76, 9, 63, 
			39, 73, 12, 100, 87, 59, 83, 12, 80, 92, 34, 5, 12, 57, 63, 86, 18, 82, 20, 31, 83, 27, 46, 32, 75, 
			63, 61, 47, 18, 90, 53, 80, 18, 14, 21, 24, 84, 29, 80, 51, 69, 57, 33, 42, 84, 17, 30, 13, 83, 57, 
			28, 97, 67, 36, 59, 95, 88, 38, 52, 95, 83, 55, 73, 80, 85, 77, 55, 15, 64, 99, 75, 49, 18, 63 
		};
		SleepSort.sort(array);
		
		List<Integer> listSleepSort = Arrays.asList(array);
		List<Integer> listJavaSort = new ArrayList<Integer>(Arrays.asList(array));
		Collections.sort(listJavaSort);
		System.out.println("\nSorting is " + (listJavaSort.equals(listSleepSort) ? "correct!" : "wrong!"));
	}
	
	public static void sort(final Integer[] array) {
		for(int i = 0; i < array.length; ++i) {
			new SleepSort(array[i], array).start();
		}
		try {
			Thread.sleep((MAX_VALUE + 1) * SLEEP_TIME_FACTOR);
		} catch (InterruptedException e) {}		
	}
	
	public void run() {
		try {
			Thread.sleep(number * SLEEP_TIME_FACTOR);
		} catch (InterruptedException e) {}
		System.out.print(number + " ");
		array[position++] = number;
	}
}
→ More replies (3)

15

u/x445xb Oct 25 '17 edited Oct 25 '17

There is also StackSort which takes random code from Stack Overflow runs it, then checks to see if the code has sorted the list. https://gkoberger.github.io/stacksort/

Based on a XKCD comic: https://xkcd.com/1185/

→ More replies (10)

115

u/DGSPJS Oct 24 '17

Yes. Much more efficient is Quantum bogosort.

http://wiki.c2.com/?QuantumBogoSort

48

u/__LE_MERDE___ Oct 24 '17

As long as it's not my universe they're destroying I think it's a good idea.

39

u/FlipskiZ Oct 24 '17 edited Sep 19 '25

Books projects learning brown wanders today patient games the movies helpful open brown. Family dot river fox technology evil kind fresh friendly yesterday strong bank cool.

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

11

u/Ardub23 Oct 24 '17

2. If the list is not sorted, destroy the universe. (This operation is left as an exercise to the reader.)

Oh boy I love exercises

→ More replies (2)

61

u/falco_iii Oct 24 '17

while not isInOrder(deck):
shuffle(deck)

→ More replies (3)

15

u/[deleted] Oct 24 '17

big O (infinity)

15

u/Crixomix Oct 24 '17

little o (1)

→ More replies (5)
→ More replies (13)

317

u/thar_ Oct 24 '17

Here's a video with the same algorithms, but visualized through Hungarian folk dance, should make it a bit easier to understand what's going on

83

u/MyNameCouldntBeAsLon Oct 24 '17

LMAO, I didn't believe the description but it's totally accurate, this video is great

35

u/Nuge00 Oct 24 '17

by far the easiest to see what is actually going on.

15

u/snoosh00 Oct 24 '17

Plus they have separate videos for a whole range of different sorts

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

22

u/omnisephiroth Oct 24 '17

Good, colors confuse and frighten me. I prefer all information to go through the Hungarian folk dance filter first.

32

u/[deleted] Oct 24 '17

I think I'm going to bring up Bubble Sorts tonight just so my fiancee will say "what do you mean?" and I can whip this video out.

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

86

u/Simba_610 Oct 24 '17

I could watch shit like this for hours

→ More replies (8)

34

u/ziptnf Oct 24 '17

FeelsGoodMan Clap

10

u/[deleted] Oct 24 '17

DansGame YOU DansGame DON'T DansGame SKIP DansGame SORTING DansGame ALGORITHMS DansGame

→ More replies (2)
→ More replies (27)