Created
November 19, 2015 22:52
-
-
Save anonymous/1d39e3d308ba410f50c7 to your computer and use it in GitHub Desktop.
The great sleepSort, now in java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
class Number implements Runnable | |
{ | |
private Thread t; | |
private int n; | |
public Number(int n) | |
{ | |
this.n = n; | |
t = new Thread(this); | |
} | |
public void run() | |
{ | |
// Wait until all input is complete to begin counting | |
while(!SleepSort.done) { | |
try { | |
Thread.sleep(1); | |
} catch (InterruptedException e) { | |
System.out.println("Thread interrupted."); | |
} | |
} | |
try Thread.sleep(n*100); | |
catch (InterruptedException e) System.out.println("Thread interrupted."); | |
System.out.printf("%d ", n); | |
} | |
public void start () | |
{ | |
t.start(); | |
} | |
} | |
public class SleepSort | |
{ | |
private static Scanner in = new Scanner(System.in); | |
public static boolean done; | |
public static int main() | |
{ | |
System.out.printf("How many numbers? "); | |
int n = in.nextInt(); | |
done = false; | |
for(int i=0; i<n; i++) { | |
// Comment out either part A or part B | |
// A to produce n random numbers, B to actually use it | |
// Part A begin | |
int a = (int)(Math.random()*100+1); | |
System.out.printf("%d ", a); | |
Number x = new Number(a); | |
// Part A end | |
// Part B begin | |
//Number x = new Number(in.nextInt()); | |
// Part B end | |
x.start(); | |
} System.out.println(); | |
done = true; | |
return 0; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment