Merge sort of data structures and algorithms

Merge sort ( Merge Sort) is a sorting algorithm based on the divide-and-conquer idea. It divides the array to be sorted into two sub-arrays, sorts the two sub-arrays respectively, and finally merges the sorted sub-arrays into an ordered array. Its basic idea is to merge two ordered subsequences into one ordered sequence.

code show as below:

// 归并排序算法
function mergeSort(arr) {
    
    
  // 递归出口,当数组长度小于等于1时,直接返回数组本身
  if (arr.length <= 1) {
    
    
    return arr;
  }

  // 找到数组的中间位置,将数组分成两个子数组
  const mid = Math.floor(arr.length / 2);
  const left = arr.slice(0, mid);
  const right = arr.slice(mid);

  // 递归对左右两个子数组进行排序
  const sortedLeft = mergeSort(left);
  const sortedRight = mergeSort(right);

  // 合并两个有序的子数组
  return merge(sortedLeft, sortedRight);
}

// 合并两个有序的子数组
function merge(left, right) {
    
    
  const merged = [];
  let i = 0; // 左子数组指针
  let j = 0; // 右子数组指针

  // 比较左右子数组的元素,将较小的元素依次放入合并后的数组中
  while (i < left.length && j < right.length) {
    
    
    if (left[i] <= right[j]) {
    
    
      merged.push(left[i]);
      i++;
    } else {
    
    
      merged.push(right[j]);
      j++;
    }
  }

  // 将剩余的元素放入合并后的数组中
  while (i < left.length) {
    
    
    merged.push(left[i]);
    i++;
  }
  while (j < right.length) {
    
    
    merged.push(right[j]);
    j++;
  }

  return merged;
}

// 测试
const arr = [4, 2, 7, 5, 1, 6, 3, 8];
const sortedArr = mergeSort(arr);
console.log(sortedArr); // 输出 [1, 2, 3, 4, 5, 6, 7, 8]

The algorithm steps are as follows:

  1. The merge sort algorithm sorts an array to be sorted recursively.
  2. In each level of recursion, the array is divided into two, and the left and right sub-arrays are obtained respectively.
  3. Continue to recursively call the merge sort algorithm to sort the left and right subarrays respectively.
  4. When the length of the subarray is less than or equal to 1, the recursion exits and directly returns the subarray itself.
  5. Merge two sorted subarrays into one sorted array.
  6. During the merging process, use two pointers to point to the elements of the left and right sub-arrays respectively, compare the sizes of the two elements, and select the smaller element to put into the merged array until the left and right sub-arrays are traversed.
  7. Put the remaining elements into the merged array one by one.
  8. Return the merged array as the result.

The following is a flow chart representation of the merge sort algorithm:

               [4,2,7,5,1,6,3,8]
             /                 \
      [4,2,7,5]              [1,6,3,8] 
       /     \                /     \
    [4,2]   [7,5]           [1,6]  [3,8] 
   /    \     /   \        /    \    /    \
 [4]   [2]   [7]   [5]    [1]  [6]  [3]  [8] 
   \    /     \   /        \    /    \    /
    [2,4]    [5,7]         [1,6]    [3,8] 
       \      /    \         /       \       
    [2,4,5,7]    [1,3,6,8]            
           \       /   
      [1,2,3,4,5,6,7,8]

In this flowchart, each level of recursion splits the array into two parts and finally merges them into an ordered array.

Guess you like

Origin blog.csdn.net/jieyucx/article/details/132964729