js 八大排序算法

307 阅读2分钟

交换排序1:冒泡排序

第一次 最后一个为最大(比较n次) 第二次倒数第二个最大(比较n-1次)

// 冒泡排序 第一次 最后一个为最大(比较n次)  第二次倒数第二个最大(比较n-1次)
let array = [5,8,0,9,4234,3,4,5,6]

let exchange = (m,n)=>{
  let tmp = array[n]
  array[n] = array[m]
  array[m] = tmp
}
Array.prototype.bubbleSort = function () {
  debugger
  let length = this.length
  for (let i = 0; i < length; i++) {
    for (let j = 0; j < length-1-i; j++) {
      console.log(j)
      if(this[j]>this[j+1]){
        // 交换
        // exchange(j, j+1)
        this.splice(j+1, 1 , ...this.splice(j,1,this[j+1]))
        //  [array[j+1],array[j]] = [array[j],array[j+1]]
        console.log(this)
      }
    }
  }
}
array.bubbleSort()

交换排序2:快速排序

/**快速排序 分而治之
 * 某个元素65的正确位置
 * 小于65  全部放在左边
 * 大于65  全部放在右边
 * @param {*} arr 
 */
let array = [5,8,0,9,4234,3,4,5,6]
Array.prototype.median = function (left,right) {
  // 1、取出中间位置
  let center  = Math.floor((left+right)/2)
  // 2、判断进行交换
  if(this[left]>this[center]){
    [this[center],this[left]] = [this[left],this[center]]
  }
  if(this[right]<this[center]){
    [this[right],this[center]] = [this[center],this[right]]
  }
  if(this[left]>this[right]){
    [this[right],this[left]] = [this[left],this[right]]
  }
  // 3、将center换到right-1的位置
  [this[right-1],this[center]] = [this[center],this[right-1]]
  
  return this[right-1]
}
// 
Array.prototype.quick = function (left,right) {
  // 1、结束条件 
  if(left>=right) return
  // 2、获取枢纽
  let pivot = this.median(left,right)
   
  // 3、定义变量,用于记录当前找到的位置
  let i= left
  let j= right-1
  // 4、开始进行交换
  while(true){
    while(this[++i]<pivot){}
    while(this[--j]>pivot){}
    if(i<j) {
      [this[j],this[i]] = [this[i],this[j]]
    } else {
      break
    }
  }
  //6、将枢纽放置在正确的位置,i的位置
  [this[right-1],this[i]] = [this[i],this[right-1]]
  console.log(this)
  debugger
  // 7、分而治之
  this.quick(left,i-1)
  this.quick(i+1,right)
  console.log(this)
}
// 快排的实现
Array.prototype.quickSort = function () {
  this.quick(0,this.length-1)
  console.log(this)
}
array.quickSort()

插入排序1:直接插入排序

要插入的队员 与有序的队员比较

/**插入排序:简单排序中最有效
 * 局部有序
 * 要插入的队员 与有序的队员比较 
 */
let array = [5,8,0,9,4234,3,4,5,6]
Array.prototype.insertSort = function () {
  let length = this.length
  // 2、外层循环:从第一个位置开始获取数据,像前面局部有序进行插入
  for (let i = 0; i < length; i++) {
    // 3、内存循环
    let temp = this[i]
    let j = i
    while(this[j-1]> temp && j>0){
      // 4、向右移
      this[j]= this[j-1]
      j--
      console.log(i,j,this)
    }
    this[j] = temp
  }
}
array.insertSort()

插入排序2:希尔排序

要插入的队员 与有序的队员比较

/**希尔排序
 * 第一次:间隔为N/2 分组 组内排序
 * 第二次:间隔为/2 分组 组内排序
 * 第三次:间隔为/2 分组 组内排序
 * @param {*} arr 
 */
let array = [5,8,0,9,4234,3,4,5,6]
Array.prototype.shellSort = function () {
  let length = this.length
  let gap = Math.floor(length/2)
  // 3、gap不断减小
  while(gap>=1){
    // 4、以gap为间隔,进行分组,对分组进行插入排序
    for (let i = gap; i < length; i++) {
      let temp = this[i]
      let j = i;
      // j>gap-1 停止插入
      while(this[j-gap]>temp && j>gap-1){
        this[j]= this[j-gap]
        j-= gap
      }    
      // 5.将j位置的元素赋值到temp
      this[j] = temp
      console.log(this)
    }
    // gap/2
    gap = Math.floor(gap/2)
  }
}
array.shellSort()

:2、选择排序

第一次:[0,,,n]看一轮选择最小值的下标值,交换,换到位置0 第二次:[1,,,n]看一轮选择最小值的下标值,交换, 换到位置1


/**选择排序
 * 第一次:[0,,,n]看一轮选择最小值的下标值,交换,换到位置0
   第二次:[1,,,n]看一轮选择最小值的下标值,交换, 换到位置1
 */
let array = [5,8,0,9,4234,3,4,5,6]
Array.prototype.selectSort = function () {
  let length = this.length
  let minIndex = 0
  for (let i = 0; i < length; i++) {
    minIndex = i
    for (let j = i+1; j < length; j++) {
      if(this[minIndex]>this[j]){
        minIndex = j
      }
      console.log(j,minIndex)
    }
    if(i !== minIndex){
      [array[i],array[minIndex]] = [array[minIndex],array[i]]
    }
    console.log(i,minIndex,this)
  }
}
array.selectSort()