8000 refactor(sort): Update test utility · HuboArch/algorithms-in-java@d09ccbb · GitHub
[go: up one dir, main page]

Skip to content

Commit d09ccbb

Browse files
author
agribeau
committed
refactor(sort): Update test utility
1 parent 72abbdb commit d09ccbb

File tree

3 files changed

+42
-4
lines changed

3 files changed

+42
-4
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,5 @@ hs_err_pid*
2727
node_modules
2828
.idea/
2929
out
30-
placeholder.txt
30+
placeholder.txt
31+
TestDemo.java

src/TestDemo.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* Created by lyn
3+
*/
4+
@SuppressWarnings("unchecked")
5+
public class TestDemo {
6+
public static void main(String[] args) {
7+
8+
}
9+
10+
public void selectionSort(Comparable[] arr) {
11+
int len = arr.length;
12+
13+
for (int i = 0; i < len; i++) {
14+
15+
int minIdx = i;
16+
Comparable curItem = arr[minIdx];
17+
for (int j = 1; j < len; j++) {
18+
if (arr[j].compareTo(curItem) < 0) {
19+
minIdx = j;
20+
}
21+
}
22+
23+
if (minIdx != i) {
24+
swap(arr, i, minIdx);
25+
}
26+
}
27+
28+
}
29+
30+
private void swap(Comparable[] arr, int a, int b) {
31+
Comparable tmp = arr[a];
32+
33+
arr[a] = arr[b];
34+
arr[b] = tmp;
35+
}
36+
}

src/algorithm/sorting/TestHelper.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
*
88
* @author dean
99
*/
10+
@SuppressWarnings("unchecked")
1011
public class TestHelper {
1112

1213
/* 不允许产生实例对象 */
@@ -46,8 +47,8 @@ public static Integer[] generateRandomArray(int n, int rangeL, int rangeR) {
4647
* @param arr 数组
4748
*/
4849
public static void printArray(Object[] arr) {
49-
for (int i = 0; i < arr.length; i++) {
50-
System.out.print(arr[i]);
50+
for (Object anArr : arr) {
51+
System.out.print(anArr);
5152
System.out.print(" ");
5253
}
5354
}
@@ -58,7 +59,7 @@ public static void printArray(Object[] arr) {
5859
* @param arr 数组
5960
* @return boolean
6061
*/
61-
public static boolean isSorted(Comparable[] arr) {
62+
private static boolean isSorted(Comparable[] arr) {
6263
for (int i = 0; i < arr.length - 2; i++) {
6364
if (arr[i].compareTo(arr[i + 1]) > 0) {
6465
return false;

0 commit comments

Comments
 (0)
0