-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPassSort.java
More file actions
58 lines (54 loc) · 1.75 KB
/
Copy pathPassSort.java
File metadata and controls
58 lines (54 loc) · 1.75 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
package com.albert.algorithm.sort;
import java.util.Arrays;
/**
* 传递排序法
*
* @author Albert
* @see http://baike.baidu.com/view/9975641.htm
*/
public class PassSort {
public static void main(String[] args) {
int[] array = {6, 5, 3, 9, 0, 2, 7, 6, 4, 12, 11};
sort(array);
}
/**
* 升序排列
* 设有n个数据,首先,数据位置序号模2相等的数进行排序,其次,数据位置序号各加1再模2相等的数进行排序,如此反复,直到n次比较排序完成。
* 初始关键字 [49 38 65 97 76 27 20 11]
* 第一趟排序后 [38 49][ 65 97] [27 76] [11 20]
* 第二趟排序后 38 [49 65][ 27 97][ 11 76] 20
* 第三趟排序后 [38 49][ 27 65][ 11 97][ 20 76]
* 第四趟排序后 38[ 27 49][ 11 65][ 20 97] 76
* 第五趟排序后 [27 38][ 11 49][ 20 65][ 76 97]
* 第六趟排序后 27[ 11 38][ 20 49][ 65 76 ]97
* 第七趟排序后 [11 27][ 20 38 ][49 65 ][76 97]
* 最后一趟排序后 11 [20 27][ 38 49][ 65 76] 97
*
* @param array
*/
public static void sort(int[] array) {
printArray(array);
int a = 0;
boolean finished = true;
for (int i = 0; i < array.length; i++) {
finished = true;
for (int j = i % 2; j < array.length - 1 - i % 2; j += 2) {
if(array[j] > array[j + 1]) {
a = array[j + 1];
array[j + 1] = array[j];
array[j] = a;
finished = false;
}
}
if(finished) {
break;
}
printArray(array);
}
printArray(array);
}
private static void printArray(int[] array) {
System.out.print(Arrays.toString(array));
System.out.println();
}
}