Sorting Algorithms - Merging Sort

Merging Sort

Reference article:

https://www.cnblogs.com/chengxiao/p/6194356.html

https://zh.wikipedia.org/wiki/%E5%BD%92%E5%B9%B6%E6%8E%92%E5%BA%8F

The basic idea:

Merge sort (MERGE-SORT) using merge sort of thought to realize the method, the algorithm uses the classic divide and conquer (divide-and-conquer) strategy.

Divide and conquer the problem points to solve the (divide) into smaller problems, and treatment (conquer) stage will receive points in each stage of the answer to "integrate" together, so called divide and conquer.

 

Implement merge sort of two methods:

Recursion (Top-down) 

  1. Merge space applications, so that the size of the sum of two sorted sequences, the sequence space for the combined storage.
  2. Setting two pointers p1 and p2, respectively, the initial position of the starting position of the two sorted sequence of left [0], right [0].
  3. Comparison of two pointer points to an element, the selected element into a relatively small space to merge, and move the cursor to the next position.
    • If the sequence is an array of structures, the pointer may not move, but the delete operation. I.e., after each comparison, the first element of that array is moved, deleted using the method of the first shift element is moved, and then continue this comparison, left [0] <=> right [0]. But ⚠️, the first element of the array delete cause it to re-allocate pointers to each array element, it takes time. Still move the pointer using methods well ✌️.
    • The movement of the pointer sequence, the sequence is relatively small elements.
  4. Repeat step 3 until the pointer reaches one end of the sequence.
  5. The rest of all the elements of another sequence directly copied to the tail of the combined sequence merge.
function Merge (left, right) {
   var Result = [];
   // pointer to improve speed. Do not use shift method, because the array will be reallocated pointer, leading to spend more time. 
  the while (left.length> 0 && right.length> 0 ) {
     IF (left [0] <right [0 ]) { 
      result.push (left.shift ()); 
    } the else { 
      result.push (right.shift ( )); 
    } 
  } 
  return result.concat (left, right); 
} 

function mergesort (ARR) {
   IF (arr.length <=. 1) return ARR;
   var Middle = Math.floor (arr.length / 2 );
   var left = arr.slice (0, middle);
  var right = arr.slice(middle);
  return merge(mergeSort(left), mergeSort(right));
}

 

ruby implementation:

 

 

Iterative Methods (Bottom-up) 

Principle is as follows (assuming consensus sequence {\ displaystyle n} nelements):

  1. The sequence of each adjacent two numbers merge operation to form {\ displaystyle ceil (n / 2 )} {\displaystyle ceil(n/2)}sequences, each sequence comprising two sorted / element
  2. If the sequence number is not a case the above sequence will be merged again to form {\ displaystyle ceil (n / 4 )} {\displaystyle ceil(n/4)}sequences, each sequence comprising four / three elements
  3. Repeat Step 2 until all the elements sorted, i.e. the sequence number of 1

 

Guess you like

Origin www.cnblogs.com/chentianwei/p/11619688.html