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
| public static void _排序(Comparable[] a) { _临时数组 = new Comparable[a.length];
_拆分(a,0,a.length-1); }
public static void _拆分(Comparable[] a,int _开始, int _结束) {
if(_结束 <= _开始){ return; }
int _中位数 = (_开始 + _结束) / 2;
_拆分(a, _开始, _中位数);
_拆分(a, _中位数+1, _结束);
_比较并合并(a, _开始, _中位数, _结束); }
public static void _比较并合并(Comparable[] a, int _开始, int _中位数, int _结束) {
int _插入临时数组索引 = _开始;
int _左边组遍历索引 = _开始; int _右边组遍历索引 = _中位数+1;
while (_左边组遍历索引<=_中位数 && _右边组遍历索引<=_结束) {
if(_是否小于(a[_左边组遍历索引], a[_右边组遍历索引])){ _临时数组[_插入临时数组索引] = a[_左边组遍历索引]; _左边组遍历索引++; }else { _临时数组[_插入临时数组索引] = a[_右边组遍历索引]; _右边组遍历索引++; } _插入临时数组索引++; }
while(_左边组遍历索引<=_中位数){ _临时数组[_插入临时数组索引] = a[_左边组遍历索引]; _左边组遍历索引++; _插入临时数组索引++; }
while(_右边组遍历索引<=_结束){ _临时数组[_插入临时数组索引] = a[_右边组遍历索引]; _右边组遍历索引++; _插入临时数组索引++; }
for (int i = _开始; i <=_结束 ; i++) { a[i] = _临时数组[i]; }
}
|