Skip to content

Commit c8d3c30

Browse files
realDuYuanChaogithub-actions
andauthored
add sort utils (examplehub#142)
* add sort utils * Formatted with Google Java Formatter Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
1 parent cc300f3 commit c8d3c30

6 files changed

Lines changed: 54 additions & 27 deletions

File tree

src/main/java/com/examplehub/sorts/BubbleSort.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.examplehub.sorts;
22

3+
import com.examplehub.utils.SortUtils;
4+
35
public class BubbleSort implements Sort {
46

57
/**
@@ -12,9 +14,7 @@ public void sort(int[] numbers) {
1214
boolean swapped = false;
1315
for (int j = 0; j < numbers.length - 1 - i; ++j) {
1416
if (numbers[j] > numbers[j + 1]) {
15-
int temp = numbers[j];
16-
numbers[j] = numbers[j + 1];
17-
numbers[j + 1] = temp;
17+
SortUtils.swap(numbers, j, j + 1);
1818
swapped = true;
1919
}
2020
}
@@ -35,9 +35,7 @@ public <T extends Comparable<T>> void sort(T[] array) {
3535
boolean swapped = false;
3636
for (int j = 0; j < array.length - 1 - i; ++j) {
3737
if (array[j].compareTo(array[j + 1]) > 0) {
38-
T temp = array[j];
39-
array[j] = array[j + 1];
40-
array[j + 1] = temp;
38+
SortUtils.swap(array, j, j + 1);
4139
swapped = true;
4240
}
4341
}

src/main/java/com/examplehub/sorts/BubbleSortRecursion.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.examplehub.sorts;
22

3+
import com.examplehub.utils.SortUtils;
4+
35
public class BubbleSortRecursion implements Sort {
46
@Override
57
public void sort(int[] numbers) {
@@ -16,9 +18,7 @@ public void sortRecursion(int[] numbers, int length) {
1618
boolean swapped = false;
1719
for (int i = 0; i < length - 1; ++i) {
1820
if (numbers[i] > numbers[i + 1]) {
19-
int temp = numbers[i];
20-
numbers[i] = numbers[i + 1];
21-
numbers[i + 1] = temp;
21+
SortUtils.swap(numbers, i, i + 1);
2222
swapped = true;
2323
}
2424
}
@@ -43,9 +43,7 @@ public <T extends Comparable<T>> void sortRecursion(T[] array, int length) {
4343
boolean swapped = false;
4444
for (int i = 0; i < length - 1; ++i) {
4545
if (array[i].compareTo(array[i + 1]) > 0) {
46-
T temp = array[i];
47-
array[i] = array[i + 1];
48-
array[i + 1] = temp;
46+
SortUtils.swap(array, i, i + 1);
4947
swapped = true;
5048
}
5149
}

src/main/java/com/examplehub/sorts/QuickSort.java

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.examplehub.sorts;
22

3+
import com.examplehub.utils.SortUtils;
4+
35
public class QuickSort implements Sort {
46

57
@Override
@@ -13,12 +15,14 @@ private int partition(int[] number, int left, int right) {
1315
while (left < right && number[right] >= pivot) {
1416
right--;
1517
}
16-
number[left] = number[right];
17-
1818
while (left < right && number[left] <= pivot) {
1919
left++;
2020
}
21-
number[right] = number[left];
21+
if (left < right) {
22+
SortUtils.swap(number, left, right);
23+
left++;
24+
right--;
25+
}
2226
}
2327
number[left] = pivot;
2428
return left;
@@ -54,12 +58,14 @@ private static <T extends Comparable<T>> int partition(T[] array, int left, int
5458
while (left < right && array[right].compareTo(pivot) >= 0) {
5559
right--;
5660
}
57-
array[left] = array[right];
58-
5961
while (left < right && array[left].compareTo(pivot) <= 0) {
6062
left++;
6163
}
62-
array[right] = array[left];
64+
if (left < right) {
65+
SortUtils.swap(array, left, right);
66+
left++;
67+
right--;
68+
}
6369
}
6470
array[left] = pivot;
6571
return left;

src/main/java/com/examplehub/sorts/SelectionSort.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.examplehub.sorts;
22

3+
import com.examplehub.utils.SortUtils;
4+
35
public class SelectionSort implements Sort {
46

57
@Override
@@ -12,9 +14,7 @@ public void sort(int[] numbers) {
1214
}
1315
}
1416
if (minIndex != i) {
15-
int temp = numbers[i];
16-
numbers[i] = numbers[minIndex];
17-
numbers[minIndex] = temp;
17+
SortUtils.swap(numbers, i, minIndex);
1818
}
1919
}
2020
}

src/main/java/com/examplehub/sorts/SelectionSortRecursion.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.examplehub.sorts;
22

3+
import com.examplehub.utils.SortUtils;
4+
35
public class SelectionSortRecursion implements Sort {
46

57
@Override
@@ -22,9 +24,7 @@ private void selectionSort(int[] numbers, int len) {
2224
}
2325
}
2426
if (maxIndex != len - 1) {
25-
int temp = numbers[maxIndex];
26-
numbers[maxIndex] = numbers[len - 1];
27-
numbers[len - 1] = temp;
27+
SortUtils.swap(numbers, len - 1, maxIndex);
2828
}
2929
selectionSort(numbers, len - 1);
3030
}
@@ -51,9 +51,7 @@ private <T extends Comparable<T>> void selectionSort(T[] numbers, int len) {
5151
}
5252
}
5353
if (maxIndex != len - 1) {
54-
T temp = numbers[maxIndex];
55-
numbers[maxIndex] = numbers[len - 1];
56-
numbers[len - 1] = temp;
54+
SortUtils.swap(numbers, len - 1, maxIndex);
5755
}
5856
selectionSort(numbers, len - 1);
5957
}

src/main/java/com/examplehub/utils/SortUtils.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,31 @@ public static <T extends Comparable<T>> boolean isSorted(T[] array) {
3232
}
3333
return true;
3434
}
35+
36+
/**
37+
* Swap two elements of array.
38+
*
39+
* @param array the array contains elements
40+
* @param i the first index
41+
* @param j the second index
42+
*/
43+
public static void swap(int[] array, int i, int j) {
44+
int temp = array[i];
45+
array[i] = array[j];
46+
array[j] = temp;
47+
}
48+
49+
/**
50+
* Swap two elements of array.
51+
*
52+
* @param array the array contains elements
53+
* @param i the first index
54+
* @param j the second index
55+
* @param <T> the class of the objects in the array.
56+
*/
57+
public static <T extends Comparable<T>> void swap(T[] array, int i, int j) {
58+
T temp = array[i];
59+
array[i] = array[j];
60+
array[j] = temp;
61+
}
3562
}

0 commit comments

Comments
 (0)