Skip to content

Commit 4690efb

Browse files
authored
Merge pull request TheAlgorithms#1247 from BanQiaoGeXia/Development
Optimize method flip in SortUtils TheAlgorithms#1246
2 parents 6111fb7 + 7b1c328 commit 4690efb

File tree

4 files changed

+7
-4
lines changed

4 files changed

+7
-4
lines changed

src/main/java/com/sorts/CycleSort.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public <T extends Comparable<T>> T[] sort(T[] arr) {
1515
int count = 0;
1616

1717
// Traverse array and put the elements on their respective right places
18-
for (int i = 0; i < n - 2; i++) {
18+
for (int i = 0; i < n - 1; i++) {
1919

2020
// Initialize item as the starting point
2121
T item = arr[i];

src/main/java/com/sorts/SortUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ static <T extends Comparable<T>> boolean less(T v, T w) {
3838
* @param right is a right flip border of the array
3939
*/
4040
static <T extends Comparable<T>> void flip(T[] array, int left, int right) {
41-
while (left <= right) {
41+
while (left < right) {
4242
swap(array, left++, right--);
4343
}
4444
}

src/test/java/com/dataStructures/StackTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ void testPeekWithElements() {
3636
myStack.push(30);
3737
myStack.push(40);
3838

39-
Assertions.assertEquals(40, myStack.peek());
39+
Assertions.assertEquals(40, (int) myStack.peek());
4040
}
4141

4242
@Test
@@ -57,7 +57,7 @@ void testPopWithElements() {
5757
myStack.push(40);
5858
myStack.push(50);
5959

60-
Assertions.assertEquals(50, myStack.pop());
60+
Assertions.assertEquals(50, (int) myStack.pop());
6161

6262
}
6363

src/test/java/com/sorts/CycleSortTest.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,8 @@ void cycleSortIntegerTest() {
3030
String[] sortedStr = new String[]{"Alan", "David", "Dennis", "Edward", "Ken", "Linus", "Robert"};
3131
Assertions.assertArrayEquals(sortedStr, cycleSort.sort(unsortedStr));
3232

33+
Integer[] unsortedShortInt = new Integer[]{29, 11};
34+
Integer[] sortedShortInt = new Integer[]{11, 29};
35+
Assertions.assertArrayEquals(sortedShortInt, cycleSort.sort(unsortedShortInt));
3336
}
3437
}

0 commit comments

Comments
 (0)