From b2a5a6b83c736019b1dfdd62c5713481fe34af44 Mon Sep 17 00:00:00 2001 From: Hrishivite <53490298+Hrishivite@users.noreply.github.com> Date: Fri, 16 Oct 2020 23:28:10 +0530 Subject: [PATCH 1/2] Create Insertion sort algorithm --- Insertion sort algorithm | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 Insertion sort algorithm diff --git a/Insertion sort algorithm b/Insertion sort algorithm new file mode 100644 index 000000000000..243ba61f8e33 --- /dev/null +++ b/Insertion sort algorithm @@ -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 From a49a19b7cfda069478b3784e2217aae920d7cbb5 Mon Sep 17 00:00:00 2001 From: Hrishivite <53490298+Hrishivite@users.noreply.github.com> Date: Fri, 16 Oct 2020 23:37:51 +0530 Subject: [PATCH 2/2] Create Sleep sort algorithm --- Sleep sort algorithm | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 Sleep sort algorithm diff --git a/Sleep sort algorithm b/Sleep sort algorithm new file mode 100644 index 000000000000..7de11c37910f --- /dev/null +++ b/Sleep sort algorithm @@ -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); + } +} + + + + + + + + +