-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSelectSort.java
More file actions
34 lines (28 loc) · 925 Bytes
/
Copy pathSelectSort.java
File metadata and controls
34 lines (28 loc) · 925 Bytes
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
package cx.Sort;
import java.util.Arrays;
/**
* 选择排序,每次遍历找出未排序数中的最小数
*/
public class SelectSort {
public static void main(String[] args) {
int[] arr = new int[]{3, 5, 2, 7, 8, 1, 2, 0, 4, 7, 4, 3, 8};
selectSort(arr);
System.out.println(Arrays.toString(arr));
}
public static void selectSort(int[] arr) {
//遍历每一个数字
for (int i = 0; i < arr.length - 1; i++) {
//记录未排序数字中最小数的下标
int index = i;
for (int j = i + 1; j < arr.length; j++) {
if (arr[j] < arr[index]) index = j;
}
if (index != i) {
//将当前未排序数字中的最小数交换到当前位置
int temp = arr[i];
arr[i] = arr[index];
arr[index] = temp;
}
}
}
}