-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSelectionSort.java
More file actions
29 lines (27 loc) · 904 Bytes
/
SelectionSort.java
File metadata and controls
29 lines (27 loc) · 904 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
package SortPractice;
/**
* @auther: wilson
* @version: v1.0 创建时间: 2019-12-16:2019
* Des:选择排序 每次固定把最大或者最小的数选出来放在最后
*/
public class SelectionSort {
public static Integer[] selectionSort(Integer[] dataSources) {
for (int i = 0; i < dataSources.length -1 ; i++) {
//初始化最小指针的index
int minIndex = i;
int temp;
for (int j = i+1; j < dataSources.length ; j++) {
if (dataSources[j] < dataSources[minIndex]){
minIndex = j;
}
}
//将当前i的指针和minIndex互换
if (minIndex != i){
temp = dataSources[i];
dataSources[i] = dataSources[minIndex];
dataSources[minIndex] = temp;
}
}
return dataSources;
}
}