Merge exchange sort:归并交换排序
Animation
An example of merge sort. First divide the list into the smallest unit (1 element), then compare each element with the adjacent list to sort and merge the two adjacent lists. Finally all the elements are sorted and merged.
Merge sort animation. The sorted elements are represented by dots.
A recursive merge sort algorithm used to sort an array of 7 integer values. These are the steps a human would take to emulate merge sort (top-down).
Complexity
| Class | Sorting algorithm |
|---|---|
| Data structure | Array |
| Worst case performance | O(nlogn) |
| Best case performance | O(nlogn) typical, O(n) natural variant |
| Average case performance | O(nlogn) |
| Worst case space complexity | О(n) total, O(n) auxiliary |
Algorithm M
Algorithm M (Merge exchange). Records
R1,...,RN
are rearranged in place;
after sorting is complete their keys will be in order,
K1<=...<=KN
. We assume
that
N>=2
.
M1. [Initialize
p
.] Set
2t>=N
. (Steps M2 through M5 will be performed for
p=2(t−1),2(t−2),...,1
.)
M2. [Initialize
q,r,d
.] Set
q←2(t−1),r←0,d←p
.
M3. [Loop on
i
.] For all
Then go to step M5. (Here i & p means the “bitwise and” of the binary
representations of
i
and
i
and
(10101)
=
(00101)
=
5
. At this point,
a power of
2
, so that i&p != (i+d)&p ; it follows that the actions of step M4
can be done for all relevant
i
in any order, even simultaneously.)
M4. [Compare/exchange
records
Ri+1↔Ri+d+1
.
M5. [Loop on
q
.] If
M6. [Loop on
p
.] (At this point the permutation
Set
p←⌊p2⌋
. If
p>0
, go back to M2. |
Flow diagram
Data table
Jave program
In this program, R1,…,RN were simplified to K1,…,KN.
/**
* Created with IntelliJ IDEA.
* User: 1O1O
* Date: 11/28/13
* Time: 10:01 PM
* :)~
* Merge exchange sort:Sorting by Exchanging:Internal Sorting
*/
public class Main {
public static void main(String[] args) {
int N = 16;
int[] K = new int[17];
Double t = Math.ceil(Math.log(N)/Math.log(2));
int temp;
/*Prepare the data*/
K[1] = 503;
K[2] = 87;
K[3] = 512;
K[4] = 61;
K[5] = 908;
K[6] = 170;
K[7] = 897;
K[8] = 275;
K[9] = 653;
K[10] = 426;
K[11] = 154;
K[12] = 509;
K[13] = 612;
K[14] = 677;
K[15] = 765;
K[16] = 703;
/*Output unsorted Ks*/
System.out.println("Unsorted Ks:");
for(int i=1; i<=N; i++){
System.out.println(i+":"+K[i]);
}
System.out.println();
/*Kernel of the Algorithm!*/
for(int p=(int)Math.pow(2, t-1); p>0; p=(int)Math.floor(p/2)){
int q = (int)Math.pow(2, t-1);
int r = 0;
int d = p;
for(int i=0; i<N-d; i++){
if((i & p) == r){
if(K[i+1] > K[i+d+1]){
temp = K[i+1];
K[i+1] = K[i+d+1];
K[i+d+1] = temp;
}
}
}
while (q > p){
d = q - p;
q /= 2;
r = p;
for(int i=0; i<N-d; i++){
if((i & p) == r){
if(K[i+1] > K[i+d+1]){
temp = K[i+1];
K[i+1] = K[i+d+1];
K[i+d+1] = temp;
}
}
}
}
}
/*Output sorted Ks*/
System.out.println("Sorted Ks:");
for(int i=1; i<=N; i++){
System.out.println(i+":"+K[i]);
}
}
}
Outputs
Unsorted Ks:
1:503
2:87
3:512
4:61
5:908
6:170
7:897
8:275
9:653
10:426
11:154
12:509
13:612
14:677
15:765
16:703
Sorted Ks:
1:61
2:87
3:154
4:170
5:275
6:426
7:503
8:509
9:512
10:612
11:653
12:677
13:703
14:765
15:897
16:908
Reference
<< The art of computer programming: Sorting and Searching >> VOLUME 3, DONALD E. KNUTH
https://en.wikipedia.org/wiki/Merge_sort
本文详细介绍了归并交换排序(Merge Exchange Sort)的工作原理,通过动画展示、复杂度分析、算法M的步骤解析、流程图、数据表格以及Java程序实例,全面阐述了该排序算法的过程和应用。归并交换排序通过不断比较和交换元素,最终实现列表的有序排列。
454

被折叠的 条评论
为什么被折叠?



