Two examples of Javascript sort algorithm merging and sorting (merging and sorting)

Source: Internet
Author: User

Merge sort is an effective Sorting Algorithm Based on the Merge operation. This algorithm is a very typical application of Divide and Conquer.

The Merge Sorting method combines two (or more) ordered tables into a new ordered table, that is, the sequence to be sorted is divided into several subsequences, each of which is ordered. Then combine the ordered subsequences into the overall ordered sequence.

Merge Sorting is an effective Sorting Algorithm Based on merge operations. This algorithm is a very typical application of Divide and Conquer. Merges ordered subsequences to obtain a fully ordered sequence. That is, first orders each subsequence, and then orders the subsequence segments. If two ordered tables are merged into an ordered table, it is called a 2-way merge.

The merge operation process is as follows:

1. Apply for a space so that the size is the sum of the two sorted sequences. This space is used to store the merged sequences.
2. Set two pointers. The initial position is the start position of the two sorted sequences respectively.
3. Compare the elements pointed to by the two pointers, select a relatively small element, and move the pointer to the next position.
4. Repeat Step 3 until a pointer reaches the end of the sequence.
5. Copy all the remaining elements of another sequence directly to the end of the merging sequence.

Example 1:

Copy codeThe Code is as follows:
/**
* A merge operation (merge) is an operation that combines two sorted sequences into one.
* The Merge Sorting Algorithm depends on the merge operation.
*
* The merging process is as follows:
*
* 1. Apply for a space to make it the sum of two sorted sequences. This space is used to store the merged sequence.
* 2. set two pointers. The initial position is the start position of the two sorted sequences respectively.
* 3. Compare the elements pointed to by the two pointers, select a relatively small element, and move the pointer to the next position.
* 4. Repeat Step 3 until a pointer reaches the end of the sequence.
* 5. Copy all the remaining elements of another sequence directly to the end of the merging 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:

Copy codeThe Code is as follows:
<Script type = "text/javascript">
// Document. write ("---------- merge sort ----- the only stable and time-complexity in a complex sort is nlogn ------ <br/> ");
// Var array = new Array (12, 25, 32, 16, 18, 27, 59, 69, 36 );
Var count = 0;
// Call the sorting method for sorting
// MSort (array, array, 0, array. length-1 );
// Source array
// Dest target array
// Start subscript of s
// T target subscript
Function mSort (source, dest, s, t ){
Var result = "";
Var m; // returns the median 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/> the result of the" ++ count + "sorting is :";
For (var n = 0; n <dest. length; n ++ ){
Result + = array [n] + ",";
}
/* Output result ends */
}
Return result;
}

/* Output result ends */
// Merge the two arrays in ascending order
// Source original array
// Dest sorted Array
// The first subscript of s
// The table below lists the second array of m.
// 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 the endless ordered arrays to the end of 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/> ")
</Script>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.