Merge sort 算法的思想就是把数组分成更小的数组,合并的时候再排序。由于是二分,所以总的时间为 T(n) = 2 T(n/2) + \theta (n) = O(n * lgn)。
public void mergeSort(int[] array, int start, int end) {
if (start < end) {
int mid = start + (end - start) / 2;
mergeSort(array, start, mid);
mergeSort(array, mid + 1, end);
merge(array, start, mid, end);
}
}
public void merge(int[] array, int start, int mid, int end) {
int[] temp1 = new int[mid - start + 2];
int[] temp2 = new int[end - mid + 1];
for (int i = start; i <= mid; i++) {
temp1[i - start] = array[i];
}
temp1[mid - start + 1] = Integer.MAX_VALUE;
for (int i = mid + 1; i <= end; i++) {
temp2[i - mid - 1] = array[i];
}
temp2[end - mid] = Integer.MAX_VALUE;
int p1 = 0; //pointer of temp1 array
int p2 = 0; //pointer of temp2 array
//alert, not index = 0
int index = start;
//the while loop will stop if at least one of the array is empty
while ((p1 < mid - start + 1) && (p2 < end - mid)) {
if (temp1[p1] <= temp2[p2]) {
array[index++] = temp1[p1++];
} else {
array[index++] = temp2[p2++];
}
}
//check whether temp1 is empty
if (p1 == mid - start + 1) {
//alert, not p2 != end - mid - 1
while (p2 != end - mid) {
array[index++] = temp2[p2++];
}
}
//check whether temp2 is empty
if (p2 == end - mid) {
//alert, not p1 != mid - start;
while (p1 != mid - start + 1) {
array[index++] = temp1[p1++];
}
}
}
注意,有很多人写的 mergesort 都不能处理当数组中有多个 Integer.MAX_VALUE的情况。但是,本文的算法是可以的。
转载请注明出处:http://blog.csdn.net/beiyeqingteng
分享到:
相关推荐
基于python的排序算法-归并排序Merge Sort
归并排序(Merge sort)(台灣譯作:合併排序)是建立在归并操作上的一种有效的排序算法。该算法是采用分治法(Divide and Conquer)的一个非常典型的应用。
归并排序(MERGE-SORT)是建立在归并操作上的一种有效的排序算法,该算法是采用分治法(Divide and Conquer)的一个非常典型的应用。将已有序的子序列合并,得到完全有序的序列;即先使每个子序列有序,再使子序列段...
C#,单向链表(Simply Linked List)的归并排序(Merge Sort)算法与源代码 归并排序法(3Merge Sort,以下简称MS)是分治法思想运用的一个典范。 其主要算法操作可以分为以下步骤: Step 1:将n个元素分成两个含n/...
排序——归并排序(Merge sort)
C#,双向链表(Doubly Linked List)归并排序(Merge Sort)算法与源代码 1 双向链表 双向链表也叫双链表,是链表的一种,它的每个数据结点中都有两个指针,分别指向直接后继和直接前驱。所以,从双向链表中的任意一...
经典排序算法 - 归并排序Merge sort 经典排序算法 - 冒泡排序Bubble sort 经典排序算法 - 选择排序Selection sort 经典排序算法 - 鸡尾酒排序Cocktail sort 经典排序算法 - 希尔排序Shell sort 经典排序算法 - ...
归并算法的简单举例
算法练习,仅供参考 用递归实现的一个归并算法 void Merge(int *A,int p,int q,int r)实现对已排序的两部分合并 void Merge_sort(int *A,int p,int r) 调用上述函数实现排序
归并排序是建立在归并操作上的一种有效,稳定的排序算法,该算法是采用分治法(Divide and Conquer)的一个非常典型的应用。将已有序的子序列合并,得到完全有序的序列;即先使每个子序列有序,再使子序列段间有序。...
``` def merge_sort(arr): if len(arr) (arr) // 2 left = merge_sort(arr[:mid]) right = merge_sort(arr[mid:]) return merge(left, right) def merge(left, right): python-归并排序算法全文共3页,当前为第1页...
在B站讲归并排序的笔记,需要的同学可以免费下载
归并排序(MERGE-SORT)是建立在归并操作上的一种有效的排序算法,该算法是采用分治法(Divide and Conquer)的一个非常典型的应用。将已有序的子序列合并,得到完全有序的序列;即先使每个子序列有序,再使子序列段...
归并排序(Merge sort,台湾译作:合并排序)是建立在归并操作上的一种有效的排序算法。该算法是采用分治法(Divide and Conquer)的一个非常典型的应用。 算法步骤: 1. 申请空间,使其大小为两个已经排序序列之...
归并排序(MERGE-SORT)是利用归并的思想实现的排序方法,该算法采用经典的分治(divide-and-conquer)策略(分治法将问题分(divide)成一些小的问题然后递归求解,而治(conquer)的阶段则将分的阶段得到的各答案"...
主要给大家介绍了关于python基本算法之实现归并排序(Merge sort)的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
归并排序(Merge sort,台湾译作:合并排序)是建立在归并操作上的一种有效的排序算法。该算法是采用分治法(Divide and Conquer)的一个非常典型的应用。
归并排序(Merge Sort)是一种采用分治法(Divide and Conquer)策略的排序算法。该算法首先将已有序的子序列合并,得到完全有序的序列。在归并排序中,合并操作是将两个有序表合并成一个有序表的过程。 归并排序的...
Task 1: Sorting the LINEITEM table by External Merge Sort Consider two cases: 1) using 5 buffer pages in memory for the external merge sort; 2) using 129 buffer pages in memory for the external merge...