Explanation of common sorting algorithms

1. Bubble Sort

Compare adjacent elements, swap positions if the order is wrong, repeat multiple times until sorting is complete.

function bubbleSort(arr) {
    
    
  const n = arr.length;
  for (let i = 0; i < n - 1; i++) {
    
    
    for (let j = 0; j < n - i - 1; j++) {
    
    
      if (arr[j] > arr[j + 1]) {
    
    
        // 交换位置
        [arr[j], arr[j + 1]] = [arr[j + 1], arr[j]];
      }
    }
  }
  return arr;
}

const arr = [64, 34, 25, 12, 22, 11, 90];
console.log(bubbleSort(arr)); // 输出:[11, 12, 22, 25, 34, 64, 90]

2. Selection Sort

Each time the smallest (or largest) element is selected from the unsorted part and placed at the end of the sorted part until all sorting is completed.

function selectionSort(arr) {
    
    
  const n = arr.length;
  for (let i = 0; i < n - 1; i++) {
    
    
    let minIndex = i;
    for (let j = i + 1; j < n; j++) {
    
    
      if (arr[j] < arr[minIndex]) {
    
    
        minIndex = j;
      }
    }
    // 将最小元素放到已排序部分的末尾
    [arr[i], arr[minIndex]] = [arr[minIndex], arr[i]];
  }
  return arr;
}

const arr = [64, 34, 25, 12, 22, 11, 90];
console.log(selectionSort(arr)); // 输出:[11, 12, 22, 25, 34, 64, 90]

3. Insertion Sort

Insert elements one by one into the correct position of the sorted section, completing the sorting by continuously expanding the sorted section.

function insertionSort(arr) {
    
    
  const n = arr.length;
  for (let i = 1; i < n; i++) {
    
    
    let key = arr[i];
    let j = i - 1;
    while (j >= 0 && arr[j] > key) {
    
    
      arr[j + 1] = arr[j];
      j--;
    }
    arr[j + 1] = key;
  }
  return arr;
}

const arr = [64, 34, 25, 12, 22, 11, 90];
console.log(insertionSort(arr)); // 输出:[11, 12, 22, 25, 34, 64, 90]

In addition to this, there are the following sorting algorithms:

Merge Sort

Recursively split the array in half, sort the two subarrays separately, and then merge them into a single sorted array.

Quick Sort

Select a benchmark element, divide the array into left and right parts, so that the elements on the left are all less than or equal to the benchmark, and the elements on the right are all greater than the benchmark, and then recursively apply quick sort to the left and right parts.

Heap Sort

Build the array into a max-heap (or min-heap), then repeatedly extract the root node (most value) and adjust the structure of the heap until all elements are in order.

Counting Sort

Count the number of occurrences of each element in the array, and put the elements back into the correct position in the original array based on the statistical information to achieve ordering.

Bucket Sort

The elements are assigned to different buckets based on their range, then the elements in each bucket are sorted, and finally the results from all buckets are merged.

Radix Sort

Sort the elements from low to high according to their number of bits, and you can use a stable sorting algorithm as a sub-sorting for each round.

Only sample code for part of the sorting algorithm is shown here. Implementations of other sorting algorithms can be written in a similar manner. Hopefully these examples will help you understand how common sorting algorithms work.

Guess you like

Origin blog.csdn.net/weixin_43534452/article/details/132045602