1.冒泡排序(Bubble Sort

// 冒泡排序(Bubble Sortfunction 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;
}

2.选择排序(Selection Sort

// 选择排序(Selection Sortfunction 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;
}

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 &gt;= 0 &amp;&amp; arr[j] &gt; current) {
      arr[j + 1] = arr[j];
      j--;
    }
    arr[j + 1] = current;
  }
  return arr;
}

4.希尔排序(Shell Sort)

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

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 &amp;&amp; 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));
}

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));
}

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 &amp;&amp; arr[left] > arr[largest]) {
    largest = left;
  }
  if (right < len &amp;&amp; arr[right] > arr[largest]) {
    largest = right;
  }
  if (largest !== i) {
    [arr[i], arr[largest]] = [arr[largest], arr[i]];
    heapify(arr, len, largest);
  }
}

 

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;
}

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;
}

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;
}

十大排序算法详解

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

原文地址:https://blog.csdn.net/qq_48691693/article/details/134674052

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。

如若转载,请注明出处:http://www.7code.cn/show_12129.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱suwngjj01@126.com进行投诉反馈,一经查实,立即删除

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注