研习算法第九站-排序算法(javascript版)

282 阅读2分钟

排序算法

  • 冒泡排序
  • 选择排序
  • 插入排序
  • 归并排序
  • 快速排序
  • ...

排序动画演示

visualgo.net/zh/sorting

冒泡排序思路

  • 比较所有相邻元素,如果第一个比第二个大,则交换它们
  • 一轮下来,可以保证最后一个数是最大的
  • 执行 n - 1 轮,就可以完成排序
Array.prototype.bubbleSort = function () {
  for (let i = 0; i < this.length - 1; i++) {
    // this.length - 1 - i
    // - 1 因为 j 和 j + 1比较,最后一位 索引会溢出 索引 要减 1
    // - i 每一轮排好,数据都会在最后,排好的数据不需要在循环排序了
    for (let j = 0; j < this.length - 1 - i; j++) {
      if (this[j] > this[j + 1]) {
        const temp = this[j];
        this[j] = this[j + 1];
        this[j + 1] = temp;
      }
    }
  }
};

let arr = [5, 4, 3, 2, 1];
arr.bubbleSort();
console.log(arr, "arr");

选择排序思路

  • 找到数组中最小值,选中它并将其放置在第一位
  • 接着找到第二小的值,选中它并将其放置在第二位
  • 依此类推,执行 n - 1 轮
Array.prototype.selectionSort = function () {
  for (let i = 0; i < this.length - 1; i++) {
    let minIndex = i;
    for (j = i; j < this.length; j++) {
      if (this[j] < this[minIndex]) {
        minIndex = j;
      }
    }
    if (minIndex !== i) {
      let temp = this[i];
      this[i] = this[minIndex];
      this[minIndex] = temp;
    }
  }
};
let arr = [5, 4, 3, 2, 1, 0];
arr.selectionSort();
console.log(arr, "arr");

插入排序思路

  • 从第二个数开始往前比
  • 比它大就往后排
  • 依此类推进行到最后一个数
Array.prototype.insertionSort = function () {
  for (let i = 1; i < this.length; i++) {
    const temp = this[i];
    let j = i;
    while (j > 0) {
      if (this[j - 1] > temp) {
        this[j] = this[j - 1];
      } else {
        break;
      }
      j--;
    }
    this[j] = temp; 
  }
};
const arr = [5, 4, 3, 2, 1, 0];
arr.insertionSort();
console.log(arr, 'arr')

归并排序思路

  • 分: 把数组劈成两半,在递归地对子数组进行“分“操作,直到分成一个个单独的数
  • 合: 把两个数合并为有序数组,再对有序数组进行合并,直到全部子数组合并为一个完整数组

合并两个有序数组

  • 新建一个空数组res,用于存放最终排序后的数组
  • 比较两个有序数组的头部,较小者出队并推入res中
  • 如果两个数组还有值,就重复第二步
Array.prototype.mergeSort = function () {
  const rec = (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, arr.length);
    const orderLeft = rec(left);
    const orderRight = rec(right);
    const res = [];
    while (orderLeft.length || orderRight.length) {
      if (orderLeft.length && orderRight.length) {
        res.push(
          orderLeft[0] < orderRight[0] ? orderLeft.shift() : orderRight.shift()
        );
      } else if (orderLeft.length) {
        res.push(orderLeft.shift());
      } else if (orderRight.length) {
        res.push(orderRight.shift());
      }
    }
    return res;
  };
  const res = rec(this);
  res.forEach((n, i) => {
    this[i] = n;
  });
};

const arr = [5, 4, 3, 2, 1, 0];
arr.mergeSort();
console.log(arr, "arr");

快速排序思路

  • 分区: 从数组中任意选择一个“基准“,所有比基准小的元素放在基准前面,比基准大的元素放在基准的后面
  • 递归: 递归地对基准前后的子数组进行分区
Array.prototype.quickSort = function () {
  const rec = (arr) => {
    if (arr.length <= 1) {
      return arr;
    }
    const left = [];
    const right = [];
    const mid = arr[0];
    for (let i = 1; i < arr.length; i++) {
      if (arr[i] < mid) {
        left.push(arr[i]);
      } else {
        right.push(arr[i]);
      }
    }
    return [...rec(left), mid, ...rec(right)];
  };
  const res = rec(this);
  res.forEach((n, i) => {
    this[i] = n;
  });
};

const arr = [5, 4, 3, 2, 1, 0];
arr.quickSort();
console.log(arr, "arr");

下一站 搜索算法