Sleep Sort

Zabawny algorytm sortujący:

public class SleepSort {

	public static void main(String[] args) {
		for (final String arg : args) {
			new Thread() {
				public void run() {
					try {
						Thread.sleep(Integer.parseInt(arg));
					} catch (Exception e) {
					}
					System.out.print(arg + " ");
				}
			}.start();
		}
	}
}
<barista@javaczyherbata.pl> java SleepSort 23 2 45 9 15 10 87 21 100 34 45 18
2 9 10 15 18 21 23 34 45 45 87 100

2 komentarze to “Sleep Sort”

  • comrad says:

    I love this! Here’s the version in scala:

    object SleepSort {
    
      class Sleep(duration: Int) extends Thread {
        override def run() = {
          Thread.sleep(duration)
          print(duration + " ")
        }
      }
      
      implicit def stringToInteger(s: String) = Integer.parseInt(s)
      
      def go(duration: String) = new Sleep(duration).start()
      
      def main(args: Array[String]) = { 
        for (arg <- args) { go(arg) }
      }  
    }
    

Leave a Reply for Barista