-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChoiceTest.java
More file actions
55 lines (43 loc) · 1.36 KB
/
ChoiceTest.java
File metadata and controls
55 lines (43 loc) · 1.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package com.chen.test;
/**
* @author : chen weijie
* @Date: 2020-05-25 10:10
*/
public class ChoiceTest {
public static void choiceTest(int[] array) {
for (int i = 0; i < array.length - 1; i++) {
int min = i;
for (int j = i + 1; j < array.length; j++) {
if (array[j] < array[min]) {
min = j;
}
}
if (min != i) {
int temp = array[i];
array[i] = array[min];
array[min] = temp;
}
//第 i轮排序的结果为
System.out.print("第" + (i + 1) + "轮排序后的结果为:");
display(array);
}
}
//遍历显示数组
public static void display(int[] array) {
for (int i = 0; i < array.length; i++) {
System.out.print(array[i] + " ");
}
System.out.println();
}
public static void main(String[] args) {
int[] array = {4, 2, 8, 9, 5, 7, 6, 1, 3};
//未排序数组顺序为
System.out.println("未排序数组顺序为:");
display(array);
System.out.println("-----------------------");
choiceTest(array);
System.out.println("-----------------------");
System.out.println("经过选择排序后的数组顺序为:");
display(array);
}
}