编程语言
首页 > 编程语言> > ⑪ 进阶算法之“搜索算法”

⑪ 进阶算法之“搜索算法”

作者:互联网

进阶算法之“搜索算法”

一、理论

1. 排序和搜索简介

1.1 js中的排序和搜索

1.2 排序算法

1.3 搜索算法

2. js实现排序

2.1 冒泡排序

2.2.1 思路
2.2.2 动画演示

冒泡排序动画演示

2.2.3 coding part
Array.prototype.bubbleSort = function () {
  for(let i = 0; i < this.length - 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
      }
    }
  }
}
2.2.4 时间复杂度

2.2 选择排序

2.2.1 思路
2.2.2 动画演示

选择排序动画演示

2.2.3 coding part
Array.prototype.selectionSort = function () {
  for(let i = 0; i < this.length-1; i++) {
    let indexMin = i
    for(let j = i; j < this.length; j++) {
      if(this[j] < this[indexMin]) {
        indexMin = j
      }
    }
    if(indexMin !== i) {
      const temp = this[i]
      this[i] = this[indexMin]
      this[indexMin] = temp
    }
  }
}
2.2.4 时间复杂度

2.3 插入排序

2.3.1 思路
2.3.2 动画演示

插入排序动画演示

2.3.3 coding part
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
  }
}
2.3.4 时间复杂度

2.4 归并排序

2.4.1 思路
合并两个有序数组
2.4.2 动画演示

归并排序动画演示

2.4.3 coding part
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)
}
2.4.4 时间复杂度

2.5 快速排序

2.5.1 思路
2.5.2 动画演示

快速排序动画演示

2.5.3 coding part
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 < this.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)
}
2.5.4 时间复杂度

3. js实现搜索

3.1 顺序搜索

3.1.1 思路
3.1.2 coding part
Array.prototype.sequentialSearch = function(item) {
  for(let i = 0; i < this.length; i++) {
    if(this[i] === item) {
      return i
    }
  }
  return -1
}
3.1.3 时间复杂度

3.2 二分搜索

前提:数组有序

3.2.1 思路
3.2.2 coding part
Array.prototype.binarySearch = function(item) {
  let low = 0, high = this.length - 1
  while(low <= high) {
    const mid = Math.floor((low + high) / 2)
    const element = this[mid]
    if(element < item) {
      low = mid + 1
    } else if(element > item) {
      high = mid - 1
    } else {
      return mid
    }
  }
  return -1
}
3.2.3 时间复杂度

二、刷题

1. 合并两个有序链表(21)

1.1 题目描述

1.2 解题思路

1.3 解题步骤

function mergeTwoLists(l1, l2) {
  const res = new ListNode(0);
  let p = res, p1 = l1, p2 = l2;
  while(p1 && p2) {
    if(p1.val < p2.val) {
      p.next = p1;
      p1 = p1.next;
    } else {
      p.next = p2;
      p2 = p2.next;
    }
    p = p.next;
  }
  if(p1) {
    p.next = p1;
  }
  if(p2) {
    p.next = p2
  }
  return res.next
}

1.4 时间复杂度&空间复杂度

2. 猜数字大小(374)

2.1 题目描述

2.2 解题思路

输入:n = 10, pick = 6
输出:6

2.3 解题步骤

function guessNumber(n) {
  let low = 1, high = n;
  while(low <= high) {
    const mid = Math.floor((low + high) / 2);
    const res = guess(mid)
    if(res === 0) {
      return mid
    } else if(res === 1) {
      low = mid + 1
    } else if(res === -1) {
      high = mid - 1
    }
  }
}

2.4 时间复杂度&空间复杂度

三、总结 -- 技术要点

标签:arr,const,进阶,res,复杂度,算法,搜索算法,length,数组
来源: https://www.cnblogs.com/pleaseAnswer/p/15850013.html