From f3f2661d353050b4c6153bafdb1c399abda9a896 Mon Sep 17 00:00:00 2001 From: MK Date: Tue, 17 Mar 2020 14:35:13 +0800 Subject: [PATCH 1/2] Solve the Optimize method flip in SortUtils #1246 --- src/main/java/com/sorts/SortUtils.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/sorts/SortUtils.java b/src/main/java/com/sorts/SortUtils.java index cbede545..23bf0336 100644 --- a/src/main/java/com/sorts/SortUtils.java +++ b/src/main/java/com/sorts/SortUtils.java @@ -38,7 +38,7 @@ final class SortUtils { * @param right is a right flip border of the array */ static > void flip(T[] array, int left, int right) { - while (left <= right) { + while (left < right) { swap(array, left++, right--); } } From 7b1c328594d460b9a9bb28b87da1ae4615c4ff23 Mon Sep 17 00:00:00 2001 From: MK Date: Wed, 18 Mar 2020 18:19:16 +0800 Subject: [PATCH 2/2] fix a bug #1250 --- src/main/java/com/sorts/CycleSort.java | 2 +- src/test/java/com/dataStructures/StackTest.java | 4 ++-- src/test/java/com/sorts/CycleSortTest.java | 3 +++ 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/sorts/CycleSort.java b/src/main/java/com/sorts/CycleSort.java index a1e5a5bf..6c0436c5 100644 --- a/src/main/java/com/sorts/CycleSort.java +++ b/src/main/java/com/sorts/CycleSort.java @@ -15,7 +15,7 @@ public class CycleSort { int count = 0; // Traverse array and put the elements on their respective right places - for (int i = 0; i < n - 2; i++) { + for (int i = 0; i < n - 1; i++) { // Initialize item as the starting point T item = arr[i]; diff --git a/src/test/java/com/dataStructures/StackTest.java b/src/test/java/com/dataStructures/StackTest.java index 6bf4868e..c6b80f15 100644 --- a/src/test/java/com/dataStructures/StackTest.java +++ b/src/test/java/com/dataStructures/StackTest.java @@ -36,7 +36,7 @@ class StackTest { myStack.push(30); myStack.push(40); - Assertions.assertEquals(40, myStack.peek()); + Assertions.assertEquals(40, (int) myStack.peek()); } @Test @@ -57,7 +57,7 @@ class StackTest { myStack.push(40); myStack.push(50); - Assertions.assertEquals(50, myStack.pop()); + Assertions.assertEquals(50, (int) myStack.pop()); } diff --git a/src/test/java/com/sorts/CycleSortTest.java b/src/test/java/com/sorts/CycleSortTest.java index 720e5e5f..594efec6 100644 --- a/src/test/java/com/sorts/CycleSortTest.java +++ b/src/test/java/com/sorts/CycleSortTest.java @@ -30,5 +30,8 @@ class CycleSortTest { String[] sortedStr = new String[]{"Alan", "David", "Dennis", "Edward", "Ken", "Linus", "Robert"}; Assertions.assertArrayEquals(sortedStr, cycleSort.sort(unsortedStr)); + Integer[] unsortedShortInt = new Integer[]{29, 11}; + Integer[] sortedShortInt = new Integer[]{11, 29}; + Assertions.assertArrayEquals(sortedShortInt, cycleSort.sort(unsortedShortInt)); } }