// 遞歸退出條件,及left》=right的時候
if (left < right) {
// 找出中間索引
center = (left + right) / 2;
// 對左邊數組進行遞歸
mSort(k, 0, center);
// 對右邊數組進行遞歸
mSort(k, center + 1, right);
// 合併
merging(k, left, center, right);
}
private static void merging(int[] k, int left, int center, int right) {
int tempArr[] = new int[k.length];// 存放數據的數組
// third記錄中間數組的索引
int mid = center + 1;
int third = left;
int temp = left;
while (left <= center && mid <= right) {
// 從左右兩個數組找出最小的數存入tempArr數組
if (k[left] < k[mid]) {
tempArr[third++] = k[left++];
} else {
tempArr[third++] = k[mid++];
}
}
// 剩餘部分依次放入中間數組
while (mid <= right) {
tempArr[third++] = k[mid++];
}
while (left <= center) {
tempArr[third++] = k[left++];
}
// 將中間數組中的內容複製回原數組
while (temp <= right) {
k[temp] = tempArr[temp++];
}
}
}
//下面是遞歸版的
package com.xujun.mergesort;
public class MergeSort {
static int[] a = new int[] { 20, 9, 3, 5, 26, 100, 8, -1, 7, 50, -5 };
public static void main(String[] args) {
System.out.println("before sort");
ArrayUtils.printArray(a);
mergeSort(a);
System.out.println("after sort");
ArrayUtils.printArray(a);
}
private static void mergeSort(int[] k) {
mSort(k, 0, k.length - 1);
}
private static void mSort(int[] k, int left, int right) {
int center
// 遞歸退出條件,及left》=right的時候
if (left < right) {
// 找出中間索引
center = (left + right) / 2;
// 對左邊數組進行遞歸
mSort(k, 0, center);
// 對右邊數組進行遞歸
mSort(k, center + 1, right);
// 合併
merging(k, left, center, right);
}
}
private static void merging(int[] k, int left, int center, int right) {
// 存放數據的數組
int tempArr[] = new int[k.length];
// third記錄中間數組的索引
int mid = center + 1;
int third = left;
int temp = left;
while (left <= center && mid <= right) {
// 從左右兩個數組找出最小的數存入tempArr數組
if (k[left] < k[mid]) {
tempArr[third++] = k[left++];
} else {
tempArr[third++] = k[mid++];
}
}
// 剩餘部分依次放入中間數組
while (mid <= right) {
tempArr[third++] = k[mid++];
}
while (left <= center) {
tempArr[third++] = k[left++];
}
// 將中間數組中的內容複製回原數組
while (temp <= right) {
k[temp] = tempArr[temp++];
}
}
}
// 程序邊界的處理很是重要
while (len <= t.length) {
for (int i = 0; i + len <= t.length - 1; i += len * 2) {
// System.out.println("len="+len);
low = i;
mid = i + len - 1;
high = i + len * 2 - 1;
if (high > t.length - 1)
high = t.length - 1;
merge(t, i, mid, high);
}
//長度加倍
len += len;
}
return true;
}
源碼以下:java
package com.xujun.mergesort1;
public class MergeSort2 {
/**
* 二路歸併排序的遞歸算法-入口
*
* @param <T>
* @param t
* @return
*/
public static <T extends Comparable> boolean mergeSortRecursive(T[] t) {
if (t == null || t.length <= 1)
return true;
MSortRecursive(t, 0, t.length - 1);
return true;
}
/**
* 二路歸併排序的遞歸算法-遞歸主體
*
* @param <T>
* @param t
* @param low
* @param high
* @return
*/
private static <T extends Comparable> boolean MSortRecursive(T[] t,
int low, int high) {
if (t == null || t.length <= 1 || low == high)
return true;
int mid = (low + high) / 2;
MSortRecursive(t, low, mid);
MSortRecursive(t, mid + 1, high);
merge(t, low, mid, high);
return true;
}
public static <T extends Comparable> boolean mergeSortNonRecursive(T[] t) {
if (t == null || t.length <= 1)
return true;
int len = 1;
int low = 0;
int mid;
int high;
// 程序邊界的處理很是重要
while (len <= t.length) {
for (int i = 0; i + len <= t.length - 1; i += len * 2) {
// System.out.println("len="+len);
low = i;
mid = i + len - 1;
high = i + len * 2 - 1;
if (high > t.length - 1)
high = t.length - 1;
merge(t, i, mid, high);
}
//長度加倍
len += len;
}
return true;
}
/**
* 將兩個歸併段合併成一個歸併段
*
* @param <T>
* @param t
* @param low
* @param mid
* @param high
* @return
*/
private static <T extends Comparable> boolean merge(T[] t, int low,
int mid, int high) {
T[] s = t.clone();// 先複製一個輔助數組
int i, j, k;// 三個指示器,i指示t[low...mid],j指示t[mid+1...high],k指示s[low...high]
for (i = low, j = mid + 1, k = low; i <= mid && j <= high; k++) {
if (t[i].compareTo(t[j]) <= 0) {
s[k] = t[i++];
} else {
s[k] = t[j++];
}
}
// 將剩下的元素複製到s中
if (i <= mid) {
for (; k <= high; k++) {
s[k] = t[i++];
}
} else {
for (; k <= high; k++) {
s[k] = s[j++];
}
}
for (int m = low; m <= high; m++) {// 將輔助數組中的排序好的元素複製回原數組
t[m] = s[m];
}
return true;
}
public static void main(String[] args) {
Integer[] arr = new Integer[] { 2, 3, 6, 8, 9, 2, 0, 1 };
long startTime = System.currentTimeMillis(); // 獲取開始時間
mergeSortRecursive(arr);
long endTime = System.currentTimeMillis(); // 獲取開始時間
System.out.println("執行時間:" + (endTime - startTime));
for (int i : arr) {
System.out.println(i);
}
startTime = System.currentTimeMillis(); // 獲取開始時間
mergeSortNonRecursive(arr);
endTime = System.currentTimeMillis(); // 獲取開始時間
System.out.println("執行時間:" + (endTime - startTime));
for (int i : arr) {
System.out.println(i);
}
}
}