合并排序可视化
结果
按照位置分色
按照数组值大小分色
可视化代码
参照 冒泡排序可视化
合并排序
public void mergeSort(List<Integer> list, int[] help, int l, int r) {
if (l >= r) {
return;
}
int mid = l + (r - l) / 2;
mergeSort(list, help, l, mid);
mergeSort(list, help, mid+1, r);
int c1 = l, c2 = mid + 1;
int c = l;
while (c1 <= mid && c2 <= r) {
if (list.get(c1) < list.get(c2)) {
help[c++] = list.get(c1++);
} else {
help[c++] = list.get(c2++);
}
}
while (c1 <= mid) {
help[c++] = list.get(c1++);
}
while (c2 <= r) {
help[c++] = list.get(c2++);
}
for (c = l; c <= r; c++) {
list.set(c, help[c]);
}
}