forked from yangyiRunning/DataStructureAlgorithmsJava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSelectionSort.java
More file actions
60 lines (57 loc) · 1.56 KB
/
SelectionSort.java
File metadata and controls
60 lines (57 loc) · 1.56 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
package ds;
/**
* 选择排序
* <p>
* 含义:
* 1. 整个排序的数据分为两个部分:已排序部分和未排序部分
* 2. 遍历未排序部分,找出最小的元素
* 3. 放在已排序部分的末尾
* <p>
* <p>
* 核心:
* 最好时间复杂度 O(n^2)
* 最坏时间复杂度 O(n^2)
* 平均时间复杂度 O(n^2)
* 空间复杂度 O(1)
* 不稳定排序
*
* @author yangyi 2018年12月04日23:47:37
*/
public class SelectionSort {
public void selectionSort(int[] ints) {
if (ints.length == 1) {
return;
}
//已排序部分
for (int i = 0; i < ints.length; i++) {
int minIndex = i;
//未排序部分
for (int j = i + 1; j < ints.length; j++) {
if (ints[j] < ints[minIndex]) {
minIndex = j;
}
}
int temp = ints[i];
ints[i] = ints[minIndex];
ints[minIndex] = temp;
}
}
public static void main(String[] args) {
//准备30个0~100的随机数
int[] nums = new int[300];
for (int i = 0; i < 300; i++) {
nums[i] = (int) (Math.random() * 100);
}
System.out.println("排序前:");
for (int num : nums) {
System.out.println(num);
}
SelectionSort bubbleSort = new SelectionSort();
bubbleSort.selectionSort(nums);
System.out.println();
System.out.println("排序后:");
for (int num : nums) {
System.out.println(num);
}
}
}