Skip to content

Commit e4827bd

Browse files
authored
Merge pull request TheAlgorithms#500 from lq920320/Development
add Test for InsertionSort
2 parents 0adfcac + 6de8705 commit e4827bd

File tree

7 files changed

+54
-30
lines changed

7 files changed

+54
-30
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package src.main.java.com.sorts;
2+
3+
public class InsertionSort {
4+
5+
/**
6+
* This method implements the Generic Insertion Sort
7+
* Sorts the array in increasing order
8+
*
9+
* @param array The array to be sorted
10+
**/
11+
public <T extends Comparable<T>> T[] sort(T[] array) {
12+
for (int j = 1; j < array.length; j++) {
13+
14+
// Picking up the key(Card)
15+
T key = array[j];
16+
int i = j - 1;
17+
18+
while (i >= 0 && SortUtils.less(key, array[i])) {
19+
array[i + 1] = array[i];
20+
i--;
21+
}
22+
// Placing the key (Card) at its correct position in the sorted subarray
23+
array[i + 1] = key;
24+
}
25+
return array;
26+
}
27+
}

src/test/java/com/sorts/BubbleSortTest.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,18 @@
55
import org.junit.Test;
66
import src.main.java.com.sorts.BubbleSort;
77

8-
import java.util.Arrays;
9-
108
public class BubbleSortTest {
119

1210
@Test
1311
public void bubbleSortTest() {
1412
BubbleSort bubbleSort = new BubbleSort();
13+
1514
Integer[] unsortedInt = new Integer[]{0, 5, 9, 2, 1, 3, 4, 8, 6, 7};
1615
Integer[] sortedInt = new Integer[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
17-
System.out.println(Arrays.toString(bubbleSort.sort(unsortedInt)));
18-
1916
Assert.assertArrayEquals(sortedInt, bubbleSort.sort(unsortedInt));
2017

2118
Character[] unsortedChar = new Character[]{'f', 'h', 'c', 'a', 'b', 'd', 'g', 'e'};
2219
Character[] sortedChar = new Character[]{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'};
23-
System.out.println(Arrays.toString(bubbleSort.sort(unsortedChar)));
24-
2520
Assert.assertArrayEquals(sortedChar, bubbleSort.sort(unsortedChar));
2621

2722
}

src/test/java/com/sorts/HeapSortTest.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,18 @@
55
import org.junit.Test;
66
import src.main.java.com.sorts.HeapSort;
77

8-
import java.util.Arrays;
9-
108
public class HeapSortTest {
119

1210
@Test
1311
public void heapSortTest() {
1412
HeapSort heapSort = new HeapSort();
13+
1514
Integer[] unsortedInt = new Integer[]{0, 5, 9, 2, 1, 3, 4, 8, 6, 7};
1615
Integer[] sortedInt = new Integer[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
17-
System.out.println(Arrays.toString(heapSort.sort(unsortedInt)));
18-
1916
Assert.assertArrayEquals(sortedInt, heapSort.sort(unsortedInt));
2017

2118
Character[] unsortedChar = new Character[]{'f', 'h', 'c', 'a', 'b', 'd', 'g', 'e'};
2219
Character[] sortedChar = new Character[]{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'};
23-
System.out.println(Arrays.toString(heapSort.sort(unsortedChar)));
24-
2520
Assert.assertArrayEquals(sortedChar, heapSort.sort(unsortedChar));
2621
}
2722
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package src.test.java.com.sorts;
2+
3+
4+
import org.junit.Assert;
5+
import org.junit.Test;
6+
import src.main.java.com.sorts.InsertionSort;
7+
8+
public class InsertionSortTest {
9+
10+
@Test
11+
public void insertionSortTest() {
12+
InsertionSort insertionSort = new InsertionSort();
13+
14+
Integer[] unsortedInt = new Integer[]{0, 5, 9, 2, 1, 3, 4, 8, 6, 7};
15+
Integer[] sortedInt = new Integer[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
16+
Assert.assertArrayEquals(sortedInt, insertionSort.sort(unsortedInt));
17+
18+
Character[] unsortedChar = new Character[]{'f', 'h', 'c', 'a', 'b', 'd', 'g', 'e'};
19+
Character[] sortedChar = new Character[]{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'};
20+
Assert.assertArrayEquals(sortedChar, insertionSort.sort(unsortedChar));
21+
}
22+
}

src/test/java/com/sorts/QuickSortTest.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,18 @@
55
import org.junit.Test;
66
import src.main.java.com.sorts.QuickSort;
77

8-
import java.util.Arrays;
9-
108
public class QuickSortTest {
119

1210
@Test
1311
public void quickSortTest() {
1412
QuickSort quickSort = new QuickSort();
13+
1514
Integer[] unsortedInt = new Integer[]{0, 5, 9, 2, 1, 3, 4, 8, 6, 7};
1615
Integer[] sortedInt = new Integer[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
17-
System.out.println(Arrays.toString(quickSort.sort(unsortedInt)));
18-
1916
Assert.assertArrayEquals(sortedInt, quickSort.sort(unsortedInt));
2017

2118
Character[] unsortedChar = new Character[]{'f', 'h', 'c', 'a', 'b', 'd', 'g', 'e'};
2219
Character[] sortedChar = new Character[]{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'};
23-
System.out.println(Arrays.toString(quickSort.sort(unsortedChar)));
24-
2520
Assert.assertArrayEquals(sortedChar, quickSort.sort(unsortedChar));
2621
}
2722
}

src/test/java/com/sorts/SelectionSortTest.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,18 @@
44
import org.junit.Test;
55
import src.main.java.com.sorts.SelectionSort;
66

7-
import java.util.Arrays;
8-
97
public class SelectionSortTest {
108

119
@Test
1210
public void selectionSortTest() {
1311
SelectionSort selectionSort = new SelectionSort();
12+
1413
Integer[] unsortedInt = new Integer[]{0, 5, 9, 2, 1, 3, 4, 8, 6, 7};
1514
Integer[] sortedInt = new Integer[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
16-
System.out.println(Arrays.toString(selectionSort.sort(unsortedInt)));
17-
1815
Assert.assertArrayEquals(sortedInt, selectionSort.sort(unsortedInt));
1916

2017
Character[] unsortedChar = new Character[]{'f', 'h', 'c', 'a', 'b', 'd', 'g', 'e'};
2118
Character[] sortedChar = new Character[]{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'};
22-
System.out.println(Arrays.toString(selectionSort.sort(unsortedChar)));
23-
2419
Assert.assertArrayEquals(sortedChar, selectionSort.sort(unsortedChar));
2520
}
2621
}

src/test/java/com/sorts/ShellSortTest.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,18 @@
44
import org.junit.Test;
55
import src.main.java.com.sorts.ShellSort;
66

7-
import java.util.Arrays;
8-
97
public class ShellSortTest {
108

119
@Test
1210
public void shellSortTest() {
1311
ShellSort shellSort = new ShellSort();
12+
1413
Integer[] unsortedInt = new Integer[]{0, 5, 9, 2, 1, 3, 4, 8, 6, 7};
1514
Integer[] sortedInt = new Integer[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
16-
System.out.println(Arrays.toString(shellSort.sort(unsortedInt)));
17-
1815
Assert.assertArrayEquals(sortedInt, shellSort.sort(unsortedInt));
1916

2017
Character[] unsortedChar = new Character[]{'f', 'h', 'c', 'a', 'b', 'd', 'g', 'e'};
2118
Character[] sortedChar = new Character[]{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'};
22-
System.out.println(Arrays.toString(shellSort.sort(unsortedChar)));
23-
2419
Assert.assertArrayEquals(sortedChar, shellSort.sort(unsortedChar));
2520
}
2621
}

0 commit comments

Comments
 (0)