-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathSelectionSort.java
More file actions
78 lines (55 loc) · 1.59 KB
/
SelectionSort.java
File metadata and controls
78 lines (55 loc) · 1.59 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package sort;
/**
* Created by ozc on 2018/3/14.
*
* @author ozc
* @version 1.0
*/
public class SelectionSort {
public void sort() {
int[] arrays = {2, 3, 1, 4, 3, 5, 1, 6, 1, 2, 3, 7, 2, 3};
/*
//假定max是最大的
int max = 0;
for (int i = 0; i < arrays.length ; i++) {
if (arrays[i] > max) {
max = arrays[i];
}
}
//使用临时变量,让两个数互换
int temp;
temp = arrays[11];
arrays[11] = arrays[13];
arrays[13] = temp;
int max2 = 0;
for (int i = 0; i < (arrays.length - 1); i++) {
if (arrays[i] > max2) {
max2 = arrays[i];
}
}
temp = arrays[7];
arrays[7] = arrays[12];
arrays[12] = temp;
*/
//记录当前趟数的最大值的角标
int pos ;
//交换的变量
int temp;
//外层循环控制需要排序的趟数
for (int i = 0; i < arrays.length - 1; i++) {
//新的趟数、将角标重新赋值为0
pos = 0;
//内层循环控制遍历数组的个数并得到最大数的角标
for (int j = 0; j < arrays.length - i; j++) {
if (arrays[j] > arrays[pos]) {
pos = j;
}
}
//交换
temp = arrays[pos];
arrays[pos] = arrays[arrays.length - 1 - i];
arrays[arrays.length - 1 - i] = temp;
}
System.out.println("公众号Java3y" + arrays);
}
}