Home > Developer > JavaScript

Source: Internet
Author: User

Merge sort is an efficient sorting algorithm based on merging operations. The algorithm is a very typical application of the partition method (Divide and Conquer).

The merge (merge) Sort method combines two (or more) ordered tables into a new ordered table, which divides the sequence into several subgroups, each of which is ordered. Then the ordered Subsequence is merged into the whole ordered sequence.

Merge ordering is an effective sorting algorithm based on merging operation. The algorithm is a very typical application of the partition method (Divide and Conquer). The ordered Subsequence is merged to obtain a fully ordered sequence, that is, the sequence of each subsequence is ordered, and then the sequence between the subsequence segments is ordered. If the two ordered table is merged into an ordered table, it is called 2-way merge.

The process of merging operations is as follows:

1. Application space so that its size is the sum of two sorted sequences, which are used to store the merged sequence

2. Set two pointers, initially at the beginning of two sorted sequences

3. Compare the elements pointed to by the two pointers, select the relatively small elements into the merged space, and move the pointer to the next position

4. Repeat step 3 until a pointer reaches the end of the sequence

5. Copy all remaining elements of another sequence directly to the end of the merge sequence

Example 1:

/**

* Merge operations (merge), also known as merging algorithms, refer to the operation of merging two sorted sequences into one sequence.

* The merge sort algorithm relies on the merge operation.

*

* The process of merging operations is as follows:

*

* 1, the application space, so that its size is the sum of two sorted sequence, the space used to store the merged sequence

* 2, set two pointers, the initial position is two already sorted sequence starting position

* 3, compare the elements pointed to by the two pointers, select the relatively small elements into the merged space, and move the pointer to the next position

* 4, repeat step 3 until a pointer reaches the end of the sequence

* 5, copy all remaining elements of another sequence directly to the end of the merge sequence

*

*/

function MergeSort (items) {

if (Items.length < 2) {

return items;

}

var middle = Math.floor (ITEMS.LENGTH/2),

left = Items.slice (0, middle),

right = Items.slice (middle),

params = merge (MergeSort (left), MergeSort (right));

Params.unshift (0, items.length);

Items.splice.apply (items, params);

return items;

function merge (left, right) {

var result = [],

Il = 0,

IR = 0;

while (Il < left.length && ir < right.length) {

if (Left[il] < Right[ir]) {

Result.push (left[il++]);

} else {

Result.push (right[ir++]);

}

}

Return Result.concat (Left.slice (IL)). Concat (Right.slice (IR));

}

}

Test

var arr = [2, 1, 3, 12, 5, 66, 23, 87, 15, 32];

MergeSort (arr);

Example 2:

<script type= "Text/javascript" >

document.write ("----------Merge sort-----The only stable time complexity in the complex order is nlogn------<br/>");

var array = new Array (12, 25, 32, 16, 18, 27, 59, 69, 36);

var count = 0;

Call sort method to sort

Msort (array, array, 0, array.length-1);

Source Array

Dest Target Array

s start subscript

T target subscript

function Msort (source, dest, S, t) {

var result = "";

var m; Take the middle value

var dest2 = new Array ();

if (s = = t) {

Dest[s] = Source[s];

}

else {

m = Math.floor ((s + t)/2);

Msort (source, Dest2, S, m);

Msort (source, Dest2, m+1, T);

Merge (Dest2, dest, S, M, T);

/* Output Result * *

result = = "<br/>" + ++count + "all over the order of the results are:";

for (var n = 0; n < dest.length; n++) {

result+= Array[n] + ",";

}

/* Output END/*

}

return result;

}

/* Output END/*

Merging two arrays in small to large order

Source Original Array

Dest Sorted Array

s first subscript

M Second array table below

N Total length

function merge (source, dest, S, M, N) {

for (var j = m+1, k = s; J <= N && s <= m; k++) {

if (Source[s] < source[j]) {

DEST[K] = source[s++];

}

else {

DEST[K] = source[j++];

}

}

Add an ordered array of remaining rows to the end of the Dest

if (s <= m) {

for (var l = 0; l <= m-s l++) {

Dest[k + L] = source[s+l];

}

}

if (j <= N) {

for (var l = 0; l <= n-j l++) {

Dest[k + L] = source[j+l];

}

}

}

document.write ("<br/><br/>")

</script>

Related Article