数据结构和算法实践-排序-归并排序
题目
排序
My Thought
然后再进行递归,递归要注意两个方面:
一、自我调用
二、终止条件:即函数边界
注意点:树、递归*
代码示例
JAVA-8
public class MergeSort {
public static void mergeSort(int arr[]) {
if (arr == null || arr.length < 2) {
return;
}
process(arr, 0, arr.length - 1);
}
public static void process(int arr[], int left, int right) {
if (left == right) {
return;
}
int mid = (left + right) / 2;
process(arr, left, mid);
process(arr, mid, right);
merge(arr, left, mid, right);
}
private static void merge(int[] arr, int left, int mid, int right) {
int help[] = new int[right - left + 1];
int i = 0;
int p1 = left;
int p2 = right;
while (p1 <= left && p2 <= right) {
help[i++] = arr[p1] <= arr[p2] ? arr[p1++] : arr[p2++];
}
while (p1 <= left) {
help[i++] = arr[p1++];
}
while (p2 <= right) {
help[i++] = arr[p2++];
}
for (i = 0; i < help.length; i++) {
arr[left + i] = help[i];
}
}
}
标签:归并,数据结构,help,int,arr,++,right,排序,left
From: https://blog.csdn.net/Mao_c/article/details/142288826