-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChooseSort.java
More file actions
47 lines (42 loc) · 1.3 KB
/
ChooseSort.java
File metadata and controls
47 lines (42 loc) · 1.3 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
import java.util.Arrays;
import java.util.Random;
/**
* 选择排序
* 依次选择最小(大)的值放到对应的位置
* 算法复杂度为O(n^2)
*/
@SuppressWarnings("unused")
public class ChooseSort implements AlgorithmInGraph {
@SuppressWarnings("Duplicates")
public void showAlgorithm() {
int [] array = new int[10];
Random random = new Random();
for(int i = 0;i<10;i++){
array[i] = random.nextInt(100);
}
System.out.println("排序前生成的数组的顺序是: \t" + Arrays.toString(array));
sort(array);
System.out.println("排序后的数组的顺序是: \t" + Arrays.toString(array));
}
/**
* @param array 待排序的数组
* 因为传进来的是数组,所以不需要返回,将数组排序好
*/
private void sort(int [] array){
for(int i = 0;i<array.length;i++){
int min = array[i];
int index = i;
for(int j = i;j<array.length;j++){
if(min > array[j]){
min = array[j];
index = j;
}
}
if(i != index){
int temp = array[i];
array[i] = array[index];
array[index] = temp;
}
}
}
}