Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions Insertion sort algorithm
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
a[] is an array of size N
begin InsertionSort(a[])

for i = 1 to N
key = a[ i ]
j = i - 1
while ( j >= 0 and a[ j ] > key0
a[ j+1 ] = x[ j ]
j = j - 1
end while
a[ j+1 ] = key
end for

end InsertionSort
40 changes: 40 additions & 0 deletions Sleep sort algorithm
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import java.util.concurrent.CountDownLatch;

public class SleepSort {
public static void sleepSortAndPrint(int[] nums) {
final CountDownLatch doneSignal = new CountDownLatch(nums.length);
for (final int num : nums) {
new Thread(new Runnable() {
public void run() {
doneSignal.countDown();
try {
doneSignal.await();

//using straight milliseconds produces unpredictable
//results with small numbers
//using 1000 here gives a nifty demonstration
Thread.sleep(num * 500);
System.out.println(num);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
}
}
public static void main(String[] args) {
int[] nums ={7, 3, 2, 1, 0, 5};
for (int i = 0; i < args.length; i++)
nums[i] = Integer.parseInt(args[i]);
sleepSortAndPrint(nums);
}
}