Login [Register]
Don't have an account? Register now to chat, post, use our tools, and much more.
So I found this thing online that sorted integers by putting each one in a thread and sleeping for an amount of time based on that number, so I tried it out, and it worked!

Here's the (C#) code:

Code:
using System;
using System.Collections.Generic;
using System.Threading;

namespace SleepSort {
   class SleepSort {
      static void Main(string[] args) {
         List<int> nums = new List<int>();
         foreach (string s in args) {
            ParameterizedThreadStart pts = new ParameterizedThreadStart(Sort);
            Thread t = new Thread(pts);
            t.Start(int.Parse(s));
         }
      }

      static void Sort(object i) {
         int a = (int)i;
         Thread.Sleep(10 * a);
         Console.WriteLine(a);
      }
   }
}

And here's the download:
http://merthsoft.com/SleepSort.exe
It works pretty well. I guess the complexity is O(kn) where k is the biggest number? I'd like to compare it to some other sorts and graph the results. If I get around to that, I'll post. Meanwhile, if you want to do the same, or show your implementations, or just talk about cupcakes, let's do that Smile
Haha, that's silly. Here's a fun example of why that's not a good idea: what if your computer is loaded such that some of the threads with smaller numbers sleep longer, or some of the threads with larger numbers sleep shorter? Sleep has weird guarantees; I think on most platform the given sleep value guarantees a minimum, with an unbounded maximum. Cf. livelock and deadlock. Smile

Edit: To be sure, by "weird" I don't mean "inexplicable"; by "weird" I mean "not intuitive if you haven't dealt with it before". And also non-deterministic, as Elfprince says below.
Isn't this nondeterministic since sleep is a "please sleep me for about this long" request in many languages?
Yeah, it's not really a good idea. It is cute, though. You could increase the chances of it working by increasing the amount of time per thing. So, change it to 1000*a or something.
merthsoft wrote:
Yeah, it's not really a good idea. It is cute, though. You could increase the chances of it working by increasing the amount of time per thing. So, change it to 1000*a or something.
True, but then you just get into the long tail of asymptotically-decreasing non-determinism that's still non-deterministic. Very Happy This would actually be an interesting problem to show CS101/102-type students to ask them why it's a bad idea.
BASH Implementation:


Code:
#!/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
I think that's where I first saw it, in fact. My co-worker showed it to me last week. I realize it's bad, but it's still cute.
merthsoft wrote:
I think that's where I first saw it, in fact. My co-worker showed it to me last week. I realize it's bad, but it's still cute.
For sure. Ultimate Dev'r, I was about to complement you on quite tightly-written code until I clicked though. Very Happy
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» All times are GMT - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum