Introduction to 2 Examples of merge ordering of JavaScript sort algorithms

Source: Internet
Author: User
Tags sort

  This article mainly introduces the JavaScript sorting algorithm merging sort (merge sort) 2 examples, the need friend may refer to under

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) method combines two (or more) ordered tables into a new ordered table, that is, to divide the sequence into several subgroups, and each subsequence is ordered. Then the ordered Subsequence is merged into the whole ordered sequence.   Merging sort is an effective sort algorithm in 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   merge operation is as follows:   1. Application space so that it is sized to the sum of two sorted sequences, which are used to store the merged sequence 2. Set two pointers, initially two from the beginning position of the sorted sequence 3. Compare the elements pointed to by 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:     Code:/**  * Merge operation ( Merge), also called a merging algorithm, refers to the operation of merging two sorted sequences into one sequence. The  * merge sort algorithm relies on the merge operation. The process of  *  * merge operation is as follows:  *  * 1, the application space, so that its size is the sum of two sorted sequence, which is used to store the merged sequence  * 2, set two pointers, initially two sorted sequence of the starting position  * 3, compares the elements pointed to by two pointers, selects the relatively small elements into the merged space, and moves the pointer to the next position  * 4, repeats step 3 until a pointer reaches the sequence end  * 5, copies all remaining elements of the other sequence directly to the merge sequence tail   *  */  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 = [],       &NBSP ;     IL = 0,             IR = 0;           while (Il < left.length && ir < right.length) {      &NBS P     if (Left[il] < Right[ir]) {                Result.push (left[il++ ]);            } else {                Result.push (rig ht[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:   Code as follows: <script type= "Text/javascript" >//document.write ("---------- Merge sort-----The only stable, time complexity for nlogn------<br/> ") in complex ordering; var array = new Array (12, 25, 32, 16, 18, 27, 59, 69, 36); var count = 0; Call the Sort method to sort//msort (array, array, 0, array.length-1); Source array//dest target array//s start subscript//t object subscript function Msort (source, dest, S, t) { var result = "";  var m; Take intermediate value    var dest2 = new Array ();  if (s = = t) {   dest[s] = Source[s]    }   else {       m = Math.fl Oor ((s + t)/2);      msort (source, Dest2, S, m);       Msort (source, Dest2, m+1, T);        merge (Dest2, dest, S, M, T);       * Output results/  &NBSP   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////Two arrays are fused in order from small to large//source the original array//dest sorted array//s The first subscript//m the second array the following table//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++]     &NBSP}     else {      &NBS P  DEST[K] = source[j++];       &NBSP}  }    //add an ordered array of remaining rows to Dest's end    if (s <= m) {    &NB Sp   for (var l = 0; l <= m-s l++) {         dest[k + l] = source[s+l];     &N Bsp }  }  if (j <= N) {      for (var l = 0; l <= N-J; l++) {       dest[k + l] = source[j+l];          }//document.write ("< br/><br/> ") </script>  

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.