← Back to context

Comment by Sukotto

15 years ago

Would someone please summarize the article?

There's no way I'm clicking a 4chan link from here at work.

It is currently SFW (it seems to have images disabled).

But, the post that started it all is:

Man, am I a genius. Check out this sorting algorithm I just invented.

  #!/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

  • That's actually a very clever hack. I'm not sure how I'll put it to use, but I'll definitely keep it in my mental store.

    • Well, I would hope you wouldn't use it to actually sort integers...but, yes, bash is surprisingly good at parallel jobs (or, at least, it is surprisingly easy to create parallel jobs in bash), and that's one way to go about it. One could bash together a cheap and cheerful queueing system with something like this...but only if order isn't all that important (race conditions are almost guaranteed), timing isn't all that important (Linux has high precision timers but bash/sleep don't use them, as far as I know; and you'd need RT patches to get near realtime performance from the kernel, anyway), and only if you know with reasonable certainty the bounds on how many things will be queued before-hand.

      So...probably not very useful, after all. But, a fun thought experiment, regardless. I feel awake after reading the whole thread. Honestly, I wish there were more posts like this on HN.

Someone posted an "algorithm" for sorting integers in bash by calling a function f, which sleeps for n seconds and then prints n. Smaller numbers will cause f to wake up and print earlier than larger numbers, so the result is that you have a sorted list print back.

Unless you have small numbers, or negative numbers, or very closely grouped numbers, or...

It was a joke, I think. (I hope)