[Sorting Algorithm] merged sorting [JS implementation]

Source: Internet
Author: User

1. First, we need to introduce the algorithm for merging two sorted tables. Suppose there is an array A [1... M], p, q, and r are three of its indexes, and there is 1 percentile p limit q <r limit m. Two sub-arrays A [p... Q], A [q + 1... R] in ascending order, we need to reschedule the position of the elements in A so that the sub-array A [p... R] are also arranged in ascending order. This is to merge A [p... Q] And A [q + 1... R. The merge algorithm is as follows. Two pointers s and t are used to point to A [p] And A [q + 1] respectively during initialization, and an empty array B [p... R] for the temporary storage. Each time the comparison elements A [s] and A [t] are compared, small elements are added to B. If they are the same, A [s] is added. Then update the pointer. If A [s] returns A [t], then s ++; otherwise, t ++, it ends when the condition s = q + 1 or t = r + 1 is set. In the first case, the array A [t... Add the remaining elements in r] to B. In another case, the array A [s... Q] Add the remaining elements to B. Finally, the array B [p... R] Copy and add A [p... R]. The pseudocode is as follows: /*************************************** **************************************** * ** algorithm: merge input: array A [1... m], p, q, and r are three of its indexes, with 1 <= p <= q <r <= m. Two sub-arrays A [p... q], A [q + 1... r] Output in ascending order: merge two sub-arrays A [p... q], A [q + 1... r] new array A [1... m] *********************************** **************************************** * ***/s = p; t = q + 1; k = p; while (s <= q & t <= r) {if (A [s] <= A [t]) {B [k] = A [s]; s ++;} else {B [k] = A [t]; t ++;} k ++ ;} if (s = q + 1) {B [k... r] = A [t... r];} else {B [k... r] = A [s... q];} A [p... r] = B [p... r]; 2. merge and sort /************************************* **************************************** * ***** algorithm: mergesort input: A [1... n] Output: array A [1... n] mergesort (A, 1, n ); **************************************** **************************************** * **/process mergesort (, low, high) if (low

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.