From 47093451ac4e08c25198529fa152d282cf5093ff Mon Sep 17 00:00:00 2001 From: lq Date: Thu, 30 Aug 2018 01:34:59 +0800 Subject: [PATCH 1/2] add Test for InsertionSort --- src/main/java/com/sorts/InsertionSort.java | 27 +++++++++++++++++++ .../java/com/sorts/InsertionSortTest.java | 27 +++++++++++++++++++ 2 files changed, 54 insertions(+) create mode 100644 src/main/java/com/sorts/InsertionSort.java create mode 100644 src/test/java/com/sorts/InsertionSortTest.java diff --git a/src/main/java/com/sorts/InsertionSort.java b/src/main/java/com/sorts/InsertionSort.java new file mode 100644 index 00000000..e7deb897 --- /dev/null +++ b/src/main/java/com/sorts/InsertionSort.java @@ -0,0 +1,27 @@ +package src.main.java.com.sorts; + +public class InsertionSort { + + /** + * This method implements the Generic Insertion Sort + * Sorts the array in increasing order + * + * @param array The array to be sorted + **/ + public > T[] sort(T[] array) { + for (int j = 1; j < array.length; j++) { + + // Picking up the key(Card) + T key = array[j]; + int i = j - 1; + + while (i >= 0 && SortUtils.less(key, array[i])) { + array[i + 1] = array[i]; + i--; + } + // Placing the key (Card) at its correct position in the sorted subarray + array[i + 1] = key; + } + return array; + } +} diff --git a/src/test/java/com/sorts/InsertionSortTest.java b/src/test/java/com/sorts/InsertionSortTest.java new file mode 100644 index 00000000..e579c664 --- /dev/null +++ b/src/test/java/com/sorts/InsertionSortTest.java @@ -0,0 +1,27 @@ +package src.test.java.com.sorts; + + +import org.junit.Assert; +import org.junit.Test; +import src.main.java.com.sorts.InsertionSort; + +import java.util.Arrays; + +public class InsertionSortTest { + + @Test + public void insertionSortTest() { + InsertionSort insertionSort = new InsertionSort(); + Integer[] unsortedInt = new Integer[]{0, 5, 9, 2, 1, 3, 4, 8, 6, 7}; + Integer[] sortedInt = new Integer[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; + System.out.println(Arrays.toString(insertionSort.sort(unsortedInt))); + + Assert.assertArrayEquals(sortedInt, insertionSort.sort(unsortedInt)); + + Character[] unsortedChar = new Character[]{'f', 'h', 'c', 'a', 'b', 'd', 'g', 'e'}; + Character[] sortedChar = new Character[]{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'}; + System.out.println(Arrays.toString(insertionSort.sort(unsortedChar))); + + Assert.assertArrayEquals(sortedChar, insertionSort.sort(unsortedChar)); + } +} From 6de87051fd4f86713d931710f5f7afe0fd296843 Mon Sep 17 00:00:00 2001 From: lq Date: Thu, 30 Aug 2018 07:37:23 +0800 Subject: [PATCH 2/2] add Test for InsertionSort; delete print statements from Test files. --- src/test/java/com/sorts/BubbleSortTest.java | 7 +------ src/test/java/com/sorts/HeapSortTest.java | 7 +------ src/test/java/com/sorts/InsertionSortTest.java | 7 +------ src/test/java/com/sorts/QuickSortTest.java | 7 +------ src/test/java/com/sorts/SelectionSortTest.java | 7 +------ src/test/java/com/sorts/ShellSortTest.java | 7 +------ 6 files changed, 6 insertions(+), 36 deletions(-) diff --git a/src/test/java/com/sorts/BubbleSortTest.java b/src/test/java/com/sorts/BubbleSortTest.java index d30e58f2..e58d086a 100644 --- a/src/test/java/com/sorts/BubbleSortTest.java +++ b/src/test/java/com/sorts/BubbleSortTest.java @@ -5,23 +5,18 @@ import org.junit.Assert; import org.junit.Test; import src.main.java.com.sorts.BubbleSort; -import java.util.Arrays; - public class BubbleSortTest { @Test public void bubbleSortTest() { BubbleSort bubbleSort = new BubbleSort(); + Integer[] unsortedInt = new Integer[]{0, 5, 9, 2, 1, 3, 4, 8, 6, 7}; Integer[] sortedInt = new Integer[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; - System.out.println(Arrays.toString(bubbleSort.sort(unsortedInt))); - Assert.assertArrayEquals(sortedInt, bubbleSort.sort(unsortedInt)); Character[] unsortedChar = new Character[]{'f', 'h', 'c', 'a', 'b', 'd', 'g', 'e'}; Character[] sortedChar = new Character[]{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'}; - System.out.println(Arrays.toString(bubbleSort.sort(unsortedChar))); - Assert.assertArrayEquals(sortedChar, bubbleSort.sort(unsortedChar)); } diff --git a/src/test/java/com/sorts/HeapSortTest.java b/src/test/java/com/sorts/HeapSortTest.java index 6044efbe..9d7cd936 100644 --- a/src/test/java/com/sorts/HeapSortTest.java +++ b/src/test/java/com/sorts/HeapSortTest.java @@ -5,23 +5,18 @@ import org.junit.Assert; import org.junit.Test; import src.main.java.com.sorts.HeapSort; -import java.util.Arrays; - public class HeapSortTest { @Test public void heapSortTest() { HeapSort heapSort = new HeapSort(); + Integer[] unsortedInt = new Integer[]{0, 5, 9, 2, 1, 3, 4, 8, 6, 7}; Integer[] sortedInt = new Integer[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; - System.out.println(Arrays.toString(heapSort.sort(unsortedInt))); - Assert.assertArrayEquals(sortedInt, heapSort.sort(unsortedInt)); Character[] unsortedChar = new Character[]{'f', 'h', 'c', 'a', 'b', 'd', 'g', 'e'}; Character[] sortedChar = new Character[]{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'}; - System.out.println(Arrays.toString(heapSort.sort(unsortedChar))); - Assert.assertArrayEquals(sortedChar, heapSort.sort(unsortedChar)); } } diff --git a/src/test/java/com/sorts/InsertionSortTest.java b/src/test/java/com/sorts/InsertionSortTest.java index e579c664..0344c2b1 100644 --- a/src/test/java/com/sorts/InsertionSortTest.java +++ b/src/test/java/com/sorts/InsertionSortTest.java @@ -5,23 +5,18 @@ import org.junit.Assert; import org.junit.Test; import src.main.java.com.sorts.InsertionSort; -import java.util.Arrays; - public class InsertionSortTest { @Test public void insertionSortTest() { InsertionSort insertionSort = new InsertionSort(); + Integer[] unsortedInt = new Integer[]{0, 5, 9, 2, 1, 3, 4, 8, 6, 7}; Integer[] sortedInt = new Integer[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; - System.out.println(Arrays.toString(insertionSort.sort(unsortedInt))); - Assert.assertArrayEquals(sortedInt, insertionSort.sort(unsortedInt)); Character[] unsortedChar = new Character[]{'f', 'h', 'c', 'a', 'b', 'd', 'g', 'e'}; Character[] sortedChar = new Character[]{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'}; - System.out.println(Arrays.toString(insertionSort.sort(unsortedChar))); - Assert.assertArrayEquals(sortedChar, insertionSort.sort(unsortedChar)); } } diff --git a/src/test/java/com/sorts/QuickSortTest.java b/src/test/java/com/sorts/QuickSortTest.java index c61b924c..00154c1f 100644 --- a/src/test/java/com/sorts/QuickSortTest.java +++ b/src/test/java/com/sorts/QuickSortTest.java @@ -5,23 +5,18 @@ import org.junit.Assert; import org.junit.Test; import src.main.java.com.sorts.QuickSort; -import java.util.Arrays; - public class QuickSortTest { @Test public void quickSortTest() { QuickSort quickSort = new QuickSort(); + Integer[] unsortedInt = new Integer[]{0, 5, 9, 2, 1, 3, 4, 8, 6, 7}; Integer[] sortedInt = new Integer[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; - System.out.println(Arrays.toString(quickSort.sort(unsortedInt))); - Assert.assertArrayEquals(sortedInt, quickSort.sort(unsortedInt)); Character[] unsortedChar = new Character[]{'f', 'h', 'c', 'a', 'b', 'd', 'g', 'e'}; Character[] sortedChar = new Character[]{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'}; - System.out.println(Arrays.toString(quickSort.sort(unsortedChar))); - Assert.assertArrayEquals(sortedChar, quickSort.sort(unsortedChar)); } } diff --git a/src/test/java/com/sorts/SelectionSortTest.java b/src/test/java/com/sorts/SelectionSortTest.java index 7bfbe638..cb9411c7 100644 --- a/src/test/java/com/sorts/SelectionSortTest.java +++ b/src/test/java/com/sorts/SelectionSortTest.java @@ -4,23 +4,18 @@ import org.junit.Assert; import org.junit.Test; import src.main.java.com.sorts.SelectionSort; -import java.util.Arrays; - public class SelectionSortTest { @Test public void selectionSortTest() { SelectionSort selectionSort = new SelectionSort(); + Integer[] unsortedInt = new Integer[]{0, 5, 9, 2, 1, 3, 4, 8, 6, 7}; Integer[] sortedInt = new Integer[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; - System.out.println(Arrays.toString(selectionSort.sort(unsortedInt))); - Assert.assertArrayEquals(sortedInt, selectionSort.sort(unsortedInt)); Character[] unsortedChar = new Character[]{'f', 'h', 'c', 'a', 'b', 'd', 'g', 'e'}; Character[] sortedChar = new Character[]{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'}; - System.out.println(Arrays.toString(selectionSort.sort(unsortedChar))); - Assert.assertArrayEquals(sortedChar, selectionSort.sort(unsortedChar)); } } diff --git a/src/test/java/com/sorts/ShellSortTest.java b/src/test/java/com/sorts/ShellSortTest.java index e057e346..a473de69 100644 --- a/src/test/java/com/sorts/ShellSortTest.java +++ b/src/test/java/com/sorts/ShellSortTest.java @@ -4,23 +4,18 @@ import org.junit.Assert; import org.junit.Test; import src.main.java.com.sorts.ShellSort; -import java.util.Arrays; - public class ShellSortTest { @Test public void shellSortTest() { ShellSort shellSort = new ShellSort(); + Integer[] unsortedInt = new Integer[]{0, 5, 9, 2, 1, 3, 4, 8, 6, 7}; Integer[] sortedInt = new Integer[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; - System.out.println(Arrays.toString(shellSort.sort(unsortedInt))); - Assert.assertArrayEquals(sortedInt, shellSort.sort(unsortedInt)); Character[] unsortedChar = new Character[]{'f', 'h', 'c', 'a', 'b', 'd', 'g', 'e'}; Character[] sortedChar = new Character[]{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'}; - System.out.println(Arrays.toString(shellSort.sort(unsortedChar))); - Assert.assertArrayEquals(sortedChar, shellSort.sort(unsortedChar)); } }