From 191b27a8c5d038b30b1febc17619183f352e80a7 Mon Sep 17 00:00:00 2001 From: Vaibhav Sahay Date: Sat, 29 Sep 2018 15:22:22 +0530 Subject: [PATCH 1/7] initial commit --- .../algorithms/mergesort/MergeSort.java | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 algorithms/src/main/java/com/baeldung/algorithms/mergesort/MergeSort.java diff --git a/algorithms/src/main/java/com/baeldung/algorithms/mergesort/MergeSort.java b/algorithms/src/main/java/com/baeldung/algorithms/mergesort/MergeSort.java new file mode 100644 index 0000000000..0deb48b6a0 --- /dev/null +++ b/algorithms/src/main/java/com/baeldung/algorithms/mergesort/MergeSort.java @@ -0,0 +1,50 @@ +package com.baeldung.algorithms.mergesort; + +public class MergeSort { + + public static void main(String[] args) { + int[] a = { 5, 1, 6, 2, 3, 4 }; + mergeSort(a, a.length); + for (int i = 0; i < a.length; i++) + System.out.println(a[i]); + } + + public static void mergeSort(int[] a, int n) { + if (n < 2) + return; + int mid = n / 2; + int[] l = new int[mid]; + int[] r = new int[n - mid]; + + for (int i = 0; i < mid; i++) { + l[i] = a[i]; + } + for (int i = mid; i < n; i++) { + r[i - mid] = a[i]; + } + mergeSort(l, mid); + mergeSort(r, n - mid); + + merge(a, l, r, mid, n - mid); + } + + public static void merge(int[] a, int[] l, int[] r, int left, int right) { + + int i = 0, j = 0, k = 0; + + while (i < left && j < right) { + + if (l[i] < r[j]) + a[k++] = l[i++]; + else + a[k++] = r[j++]; + + } + + while (i < left) + a[k++] = l[i++]; + + while (j < right) + a[k++] = r[j++]; + } +} From 5db1d97f8f0c8d0f72eced10d672bf77a095ce6a Mon Sep 17 00:00:00 2001 From: Vaibhav Sahay Date: Sat, 29 Sep 2018 15:24:09 +0530 Subject: [PATCH 2/7] initial commit --- .../algorithms/mergesort/MergeSortTest.java | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 algorithms/src/test/java/com/baeldung/algorithms/mergesort/MergeSortTest.java diff --git a/algorithms/src/test/java/com/baeldung/algorithms/mergesort/MergeSortTest.java b/algorithms/src/test/java/com/baeldung/algorithms/mergesort/MergeSortTest.java new file mode 100644 index 0000000000..64916fdc08 --- /dev/null +++ b/algorithms/src/test/java/com/baeldung/algorithms/mergesort/MergeSortTest.java @@ -0,0 +1,26 @@ +package com.baeldung.algorithms.mergesort; + +import static org.junit.jupiter.api.Assertions.*; + +import org.junit.jupiter.api.Test; + + +class MergeSortTest { + + @Test + void positiveTest() { + int[] input = { 5, 1, 6, 2, 3, 4 }; + int[] expected = { 1, 2, 3, 4, 5, 6 }; + MergeSort.mergeSort(input, input.length); + assertArrayEquals(expected, input); + } + + @Test + void negativeTest() { + int[] input = { 5, 1, 6, 2, 3, 4 }; + int[] expected = { 1, 2, 3, 4, 6 ,5}; + MergeSort.mergeSort(input, input.length); + assertArrayEquals(expected, input); + } + +} From d01ab8e3eb3969a3165063467ff008f98c7d2028 Mon Sep 17 00:00:00 2001 From: Vaibhav Sahay Date: Sat, 29 Sep 2018 15:37:34 +0530 Subject: [PATCH 3/7] removed negative test --- .../com/baeldung/algorithms/mergesort/MergeSortTest.java | 8 -------- 1 file changed, 8 deletions(-) diff --git a/algorithms/src/test/java/com/baeldung/algorithms/mergesort/MergeSortTest.java b/algorithms/src/test/java/com/baeldung/algorithms/mergesort/MergeSortTest.java index 64916fdc08..a367c8eeee 100644 --- a/algorithms/src/test/java/com/baeldung/algorithms/mergesort/MergeSortTest.java +++ b/algorithms/src/test/java/com/baeldung/algorithms/mergesort/MergeSortTest.java @@ -15,12 +15,4 @@ class MergeSortTest { assertArrayEquals(expected, input); } - @Test - void negativeTest() { - int[] input = { 5, 1, 6, 2, 3, 4 }; - int[] expected = { 1, 2, 3, 4, 6 ,5}; - MergeSort.mergeSort(input, input.length); - assertArrayEquals(expected, input); - } - } From 681815d97f4051ec7467d570c8d3cbd4742d6e6b Mon Sep 17 00:00:00 2001 From: Vaibhav Sahay Date: Sat, 29 Sep 2018 15:44:14 +0530 Subject: [PATCH 4/7] Deleted MergeSortTest.java --- .../algorithms/mergesort/MergeSortTest.java | 18 ------------------ 1 file changed, 18 deletions(-) delete mode 100644 algorithms/src/test/java/com/baeldung/algorithms/mergesort/MergeSortTest.java diff --git a/algorithms/src/test/java/com/baeldung/algorithms/mergesort/MergeSortTest.java b/algorithms/src/test/java/com/baeldung/algorithms/mergesort/MergeSortTest.java deleted file mode 100644 index a367c8eeee..0000000000 --- a/algorithms/src/test/java/com/baeldung/algorithms/mergesort/MergeSortTest.java +++ /dev/null @@ -1,18 +0,0 @@ -package com.baeldung.algorithms.mergesort; - -import static org.junit.jupiter.api.Assertions.*; - -import org.junit.jupiter.api.Test; - - -class MergeSortTest { - - @Test - void positiveTest() { - int[] input = { 5, 1, 6, 2, 3, 4 }; - int[] expected = { 1, 2, 3, 4, 5, 6 }; - MergeSort.mergeSort(input, input.length); - assertArrayEquals(expected, input); - } - -} From 6a3eec15e55541876a951543a0e6584f18eb83cc Mon Sep 17 00:00:00 2001 From: Vaibhav Sahay Date: Sat, 29 Sep 2018 16:06:30 +0530 Subject: [PATCH 5/7] Unit test for MergeSort --- .../algorithms/mergesort/MergeSortTest.java | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 algorithms/src/test/java/com/baeldung/algorithms/mergesort/MergeSortTest.java diff --git a/algorithms/src/test/java/com/baeldung/algorithms/mergesort/MergeSortTest.java b/algorithms/src/test/java/com/baeldung/algorithms/mergesort/MergeSortTest.java new file mode 100644 index 0000000000..5c304ba7fe --- /dev/null +++ b/algorithms/src/test/java/com/baeldung/algorithms/mergesort/MergeSortTest.java @@ -0,0 +1,25 @@ +package com.baeldung.algorithms.mergesort; + +import static org.junit.jupiter.api.Assertions.*; + +import org.junit.jupiter.api.Test; + +class MergeSortTest { + + @Test + void positiveTest() { + int[] input = { 5, 1, 6, 2, 3, 4 }; + int[] expected = { 1, 2, 3, 4, 5, 6 }; + MergeSort.mergeSort(input, input.length); + assertArrayEquals(expected, input); + } + + @Test + void negativeTest() { + int[] input = { 5, 1, 6, 2, 3, 4 }; + int[] expected = { 1, 2, 3, 4, 6, 5 }; + MergeSort.mergeSort(input, input.length); + assertArrayEquals(expected, input); + } + +} From 575dcc38409110819d7af51a8189d8bab475c506 Mon Sep 17 00:00:00 2001 From: Vaibhav Sahay Date: Sat, 29 Sep 2018 16:15:54 +0530 Subject: [PATCH 6/7] Updated the imports --- .../algorithms/mergesort/MergeSortTest.java | 25 ------------------ .../mergesort/MergeSortUnitTest.java | 26 +++++++++++++++++++ 2 files changed, 26 insertions(+), 25 deletions(-) delete mode 100644 algorithms/src/test/java/com/baeldung/algorithms/mergesort/MergeSortTest.java create mode 100644 algorithms/src/test/java/com/baeldung/algorithms/mergesort/MergeSortUnitTest.java diff --git a/algorithms/src/test/java/com/baeldung/algorithms/mergesort/MergeSortTest.java b/algorithms/src/test/java/com/baeldung/algorithms/mergesort/MergeSortTest.java deleted file mode 100644 index 5c304ba7fe..0000000000 --- a/algorithms/src/test/java/com/baeldung/algorithms/mergesort/MergeSortTest.java +++ /dev/null @@ -1,25 +0,0 @@ -package com.baeldung.algorithms.mergesort; - -import static org.junit.jupiter.api.Assertions.*; - -import org.junit.jupiter.api.Test; - -class MergeSortTest { - - @Test - void positiveTest() { - int[] input = { 5, 1, 6, 2, 3, 4 }; - int[] expected = { 1, 2, 3, 4, 5, 6 }; - MergeSort.mergeSort(input, input.length); - assertArrayEquals(expected, input); - } - - @Test - void negativeTest() { - int[] input = { 5, 1, 6, 2, 3, 4 }; - int[] expected = { 1, 2, 3, 4, 6, 5 }; - MergeSort.mergeSort(input, input.length); - assertArrayEquals(expected, input); - } - -} diff --git a/algorithms/src/test/java/com/baeldung/algorithms/mergesort/MergeSortUnitTest.java b/algorithms/src/test/java/com/baeldung/algorithms/mergesort/MergeSortUnitTest.java new file mode 100644 index 0000000000..c46bac041f --- /dev/null +++ b/algorithms/src/test/java/com/baeldung/algorithms/mergesort/MergeSortUnitTest.java @@ -0,0 +1,26 @@ +package com.baeldung.algorithms.mergesort; + +import org.junit.Assert; + +import org.junit.Test; + +public class MergeSortUnitTest { + + @Test + public void positiveTest() { + int[] actual = { 5, 1, 6, 2, 3, 4 }; + int[] expected = { 1, 2, 3, 4, 5, 6 }; + MergeSort.mergeSort(actual, actual.length); + Assert.assertArrayEquals(expected, actual); + } + + @Test + public void negativeTest() { + int[] actual = { 5, 1, 6, 2, 3, 4 }; + int[] expected = { 1, 2, 3, 4, 6, 5 }; + MergeSort.mergeSort(actual, actual.length); + Assert.assertArrayEquals(expected, actual); + + } + +} From ea4b5c8636e58a3960d25107dec22b4b0c2fbd92 Mon Sep 17 00:00:00 2001 From: Vaibhav Sahay Date: Sat, 29 Sep 2018 16:19:45 +0530 Subject: [PATCH 7/7] removed negative test --- .../baeldung/algorithms/mergesort/MergeSortUnitTest.java | 9 --------- 1 file changed, 9 deletions(-) diff --git a/algorithms/src/test/java/com/baeldung/algorithms/mergesort/MergeSortUnitTest.java b/algorithms/src/test/java/com/baeldung/algorithms/mergesort/MergeSortUnitTest.java index c46bac041f..5cd14b7bd0 100644 --- a/algorithms/src/test/java/com/baeldung/algorithms/mergesort/MergeSortUnitTest.java +++ b/algorithms/src/test/java/com/baeldung/algorithms/mergesort/MergeSortUnitTest.java @@ -14,13 +14,4 @@ public class MergeSortUnitTest { Assert.assertArrayEquals(expected, actual); } - @Test - public void negativeTest() { - int[] actual = { 5, 1, 6, 2, 3, 4 }; - int[] expected = { 1, 2, 3, 4, 6, 5 }; - MergeSort.mergeSort(actual, actual.length); - Assert.assertArrayEquals(expected, actual); - - } - }