十大排序算法

1.冒泡排序(Bubble Sort)

// 冒泡排序(Bubble Sort)
function bubbleSort(arr) {
  const len = arr.length;
  for (let i = 0; i < len - 1; i++) {
    for (let j = 0; j < len - 1 - i; j++) {
      if (arr[j] > arr[j + 1]) {
        [arr[j], arr[j + 1]] = [arr[j + 1], arr[j]];
      }
    }
  }
  return arr;
}

十大排序算法_第1张图片

2.选择排序(Selection Sort)

// 选择排序(Selection Sort)
function selectionSort(arr) {
  const len = arr.length;
  let minIndex;
  for (let i = 0; i < len - 1; i++) {
    minIndex = i;
    for (let j = i + 1; j < len; j++) {
      if (arr[j] < arr[minIndex]) {
        minIndex = j;
      }
    }
    [arr[i], arr[minIndex]] = [arr[minIndex], arr[i]];
  }
  return arr;
}

十大排序算法_第2张图片

3.插入排序(Insertion Sort)

// 插入排序(Insertion Sort)
function insertionSort(arr) {
  const len = arr.length;
  let current, j;
  for (let i = 1; i < len; i++) {
    current = arr[i];
    j = i - 1;
    while (j >= 0 && arr[j] > current) {
      arr[j + 1] = arr[j];
      j--;
    }
    arr[j + 1] = current;
  }
  return arr;
}

十大排序算法_第3张图片

4.希尔排序(Shell Sort)

// 希尔排序(Shell Sort)
function shellSort(arr) {
  const len = arr.length;
  let gap = Math.floor(len / 2);
  while (gap > 0) {
    for (let i = gap; i < len; i++) {
      let temp = arr[i];
      let j = i - gap;
      while (j >= 0 && arr[j] > temp) {
        arr[j + gap] = arr[j];
        j -= gap;
      }
      arr[j + gap] = temp;
    }
    gap = Math.floor(gap / 2);
  }
  return arr;
}

十大排序算法_第4张图片

5.归并排序(Merge Sort)

// 归并排序(Merge Sort)
function mergeSort(arr) {
  if (arr.length <= 1) {
    return arr;
  }
  const mid = Math.floor(arr.length / 2);
  const left = arr.slice(0, mid);
  const right = arr.slice(mid);
  return merge(mergeSort(left), mergeSort(right));
}

function merge(left, right) {
  const result = [];
  let i = 0;
  let j = 0;
  while (i < left.length && j < right.length) {
    if (left[i] <= right[j]) {
      result.push(left[i]);
      i++;
    } else {
      result.push(right[j]);
      j++;
    }
  }
  return result.concat(left.slice(i)).concat(right.slice(j));
}

十大排序算法_第5张图片

6.快速排序(Quick Sort)

// 快速排序(Quick Sort)
function quickSort(arr) {
  if (arr.length <= 1) {
    return arr;
  }
  const pivotIndex = Math.floor(arr.length / 2);
  const pivot = arr.splice(pivotIndex, 1)[0];
  const left = [];
  const right = [];
  for (let i = 0; i < arr.length; i++) {
    if (arr[i] < pivot) {
      left.push(arr[i]);
    } else {
      right.push(arr[i]);
    }
  }
  return quickSort(left).concat([pivot], quickSort(right));
}

十大排序算法_第6张图片

7.堆排序(Heap Sort)

// 堆排序(Heap Sort)
function heapSort(arr) {
  const len = arr.length;
  for (let i = Math.floor(len / 2) - 1; i >= 0; i--) {
    heapify(arr, len, i);
  }
  for (let i = len - 1; i > 0; i--) {
    [arr[0], arr[i]] = [arr[i], arr[0]];
    heapify(arr, i, 0);
  }
  return arr;
}

function heapify(arr, len, i) {
  let largest = i;
  const left = 2 * i + 1;
  const right = 2 * i + 2;
  if (left < len && arr[left] > arr[largest]) {
    largest = left;
  }
  if (right < len && arr[right] > arr[largest]) {
    largest = right;
  }
  if (largest !== i) {
    [arr[i], arr[largest]] = [arr[largest], arr[i]];
    heapify(arr, len, largest);
  }
}

十大排序算法_第7张图片 

8.计数排序(Counting Sort)

// 计数排序(Counting Sort)
function countingSort(arr) {
  const len = arr.length;
  if (len <= 1) {
    return arr;
  }
  const max = Math.max(...arr);
  const countArr = new Array(max + 1).fill(0);
  const sortedArr = [];
  for (let i = 0; i < len; i++) {
    countArr[arr[i]]++;
  }
  for (let i = 0; i < countArr.length; i++) {
    while (countArr[i] > 0) {
      sortedArr.push(i);
      countArr[i]--;
    }
  }
  return sortedArr;
}

十大排序算法_第8张图片

9.桶排序(Bucket Sort)

// 桶排序(Bucket Sort)
function bucketSort(arr, bucketSize = 5) {
  if (arr.length === 0) {
    return arr;
  }
  const minValue = Math.min(...arr);
  const maxValue = Math.max(...arr);
  const bucketCount = Math.floor((maxValue - minValue) / bucketSize) + 1;
  const buckets = new Array(bucketCount);
  for (let i = 0; i < buckets.length; i++) {
    buckets[i] = [];
  }
  for (let i = 0; i < arr.length; i++) {
    const bucketIndex = Math.floor((arr[i] - minValue) / bucketSize);
    buckets[bucketIndex].push(arr[i]);
  }
  const sortedArr = [];
  for (let i = 0; i < buckets.length; i++) {
    if (buckets[i]) {
      insertionSort(buckets[i]);
      sortedArr.push(...buckets[i]);
    }
  }
  return sortedArr;
}

十大排序算法_第9张图片

10.基数排序(Radix Sort)

// 基数排序(Radix Sort)
function radixSort(arr) {
  const maxDigit = getMaxDigit(arr);
  let mod = 10;
  let dev = 1;
  for (let i = 0; i < maxDigit; i++, dev *= 10, mod *= 10) {
    const bucket = [];
    for (let j = 0; j < arr.length; j++) {
      const num = parseInt((arr[j] % mod) / dev);
      if (!bucket[num]) {
        bucket[num] = [];
      }
      bucket[num].push(arr[j]);
    }
    arr = [];
    for (let j = 0; j < bucket.length; j++) {
      if (bucket[j]) {
        arr.push(...bucket[j]);
      }
    }
  }
  return arr;
}

function getMaxDigit(arr) {
  let maxDigit = 1;
  for (let i = 0; i < arr.length; i++) {
    const numDigit = getDigit(arr[i]);
    if (numDigit > maxDigit) {
      maxDigit = numDigit;
    }
  }
  return maxDigit;
}

function getDigit(num) {
  return Math.floor(Math.log10(num)) + 1;
}

十大排序算法_第10张图片

十大排序算法详解:

【C#】十大排序算法(动图演示+代码实现)_c#排序算法-CSDN博客

你可能感兴趣的:(排序算法,算法,数据结构)