-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSelectSort.java
More file actions
47 lines (43 loc) · 993 Bytes
/
Copy pathSelectSort.java
File metadata and controls
47 lines (43 loc) · 993 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
35
36
37
38
39
40
41
42
43
44
45
46
47
package com.albert.algorithm.sort;
import java.util.Arrays;
/**
* 简单选择排序法
*
* @author Albert
* @see http://baike.baidu.com/view/1575807.htm
*/
public class SelectSort {
public static void main(String[] args) {
int[] array = {6, 5, 3, 9, 0, 2, 7, 6, 4, 12, 11};
sort(array);
}
/**
* 简单选择排序
*
* @param array
*/
public static void sort(int[] array) {
printArray(array);
int index = 0;
int tmp = array[0];
int j;
for (int i = 0; i < array.length - 1; i++) {
index = 0;
tmp = array[0];
for (j = 1; j < array.length - i; j++) {
if(tmp < array[j]) {
index = j;
tmp = array[j];
}
}
j--;
array[index] = array[j];
array[j] = tmp;
printArray(array);
}
}
private static void printArray(int[] array) {
System.out.print(Arrays.toString(array));
System.out.println();
}
}