摘要:
归并排序(MERGE-SORT)是建立在归并操作上的一种有效的排序算法,该算法是采用分治法(Divide and Conquer)的一个非常典型的应用。将已有序的子序列合并,得到完全有序的序列;即先使每个子序列有序,再使子序列段间有序。若将两个有序表合并成一个有序表,称为二路归并。
目录:
- 基本思想
- Java实现
- Python实现
正文:
一. 基本思想
比较a[i]和a[j]的大小,若a[i]≤a[j],则将第一个有序表中的元素a[i]复制到r[k]中,并令i和k分别加上1;否则将第二个有序表中的元素a[j]复制到r[k]中,并令j和k分别加上1,如此循环下去,直到其中一个有序表取完,然后再将另一个有序表中剩余的元素复制到r中从下标k到下标t的单元。归并排序的算法我们通常用递归实现,先把待排序区间[s,t]以中点二分,接着把左边子区间排序,再把右边子区间排序,最后把左区间和右区间用一次归并操作合并成有序的区间[s,t]。
二. Java实现
package com.timen.daily_20161216;
public class MergerSort {
/**
* 读取数据
*/
public static void read_array(int[] test_data) {
for (int i = 0; i < test_data.length; i++) {
System.out.print(test_data[i] + " ");
}
System.out.println();
}
/**
* 二路归并
*/
public static void binary_merger(int[] array, int first_start, int second_start, int second_end) {
// 新建临时合并数组
int[] copy_array = new int[second_end - first_start + 1];
int k = 0;
int i = first_start;
int j = second_start;
while (i < second_start && j <= second_end) {
if (array[i] >= array[j]) {
copy_array[k] = array[i];
k++;
i++;
}else {
copy_array[k] = array[j];
k++;
j++;
}
}
// 如果第一段尚未扫描完全,将剩余的copy到合并序列
while (i < second_start) {
copy_array[k] = array[i];
k ++;
i ++;
}
// 如果第二段尚未扫描完全,将剩余的copy到合并序列
while (j <= second_end) {
copy_array[k] = array[j];
k++;
j++;
}
// 将临时合并数据数据复制到原始数组中
for (int m = first_start, n= 0; n < copy_array.length; m++, n++) {
array[m] = copy_array[n];
}
}
/**
* 归并排序
* @param test_data
*/
public static void merger_sort(int[] test_data) {
for (int i = 1; i < test_data.length; i*=2) {
int len = test_data.length;
int num = test_data.length / (2 * i);
if (test_data.length % (2 * i) >= 1) {
num += 1;
}
int start = 0;
for (int j = 0; j < num; j++) {
int second_start = start + i < len ? start + i : len -1;
int second_end = start + (2 * i) -1 < len ? start + (2 * i) -1 : len -1;
binary_merger(test_data, start, second_start, second_end);
start += (2 * i);
}
}
read_array(test_data);
}
public static void main(String[] args) {
int[] test_data = {1, 3, 6, 9, 12, 11, 15, 4, 2, 3, 8};
System.out.println("原始数据");
read_array(test_data);
System.out.println("\n归并排序");
merger_sort(test_data);
}
}
运行结果:
三. Python实现
# -*- coding:utf-8 -*-
# 二路归并
def binary_merger(left, right):
r, l = 0, 0
result = []
while l < len(left) and r < len(right):
if left[l] > right[r]:
result.append(left[l])
l += 1
else:
result.append(right[r])
r += 1
result += right[r:]
result += left[l:]
return result
# 归并排序
def merger_sort(data):
if len(data) <= 1:
return data
num = int(len(data) / 2)
left = merger_sort(data[:num])
right = merger_sort(data[num:])
return binary_merger(left, right)
if __name__ == '__main__':
test_data = [1, 3, 6, 9, 12, 11, 15, 4, 2, 3, 8]
print u"原始数据"
print test_data
print u"归并排序后数据"
print merger_sort(test_data)
运行结果:
D:\Python\python.exe D:/PythonProject/Timen_daily/daily_20161216_merger_sort.py
原始数据
[1, 3, 6, 9, 12, 11, 15, 4, 2, 3, 8]
归并排序后数据
[15, 12, 11, 9, 8, 6, 4, 3, 3, 2, 1]
Process finished with exit code 0
总结:
归并排序减少了数据的移动次数,像冒泡排序,每次都需要移动数据,时间复杂度O(n的平方),归并排序的时间复杂度时间复杂度为O(nlgn)
参考文献:
百度百科-归并排序