JS的几种排序方式

1、冒泡排序

var a = [1, 3, 6, 3, 23, 76, 1, 34, 222, 6, 456, 221];
function bubbleSort(array) {
  const len = array.length
  if (len < 2) return array
  for (let i = 0; i < len; i++) {
    for (let j = 0; j < i; j++) {
      if (array[j] > array[i]) {
        const temp = array[j]
        array[j] = array[i]
        array[i] = temp
      }
    }
  }
  return array
}

bubbleSort(a);  // [1, 1, 3, 3, 6, 6, 23, 34, 76, 221, 222, 456]

 2、快排

let bubble = (arr) => {
  if (arr.length < 2) {
    return arr;
  }
  let left = [];
  let right = [];
  let numIndex = Math.floor(arr.length / 2);
  let num = arr.splice(numIndex, 1);
  //这里注意不能像下面这样直接写,因为splice会直接改变数组,这样在最后面连上num,才没问题
  // let num = arr[Math.floor(arr.length / 2)];
  arr.forEach((item) => {
    if (item < num) {
      left.push(item);
    } else if (item > num) {
      right.push(item);
    }
  });
  return bubble(left).concat(num, bubble(right));
};
console.log(bubble(a));

 3、二路归并

function fn(arr) {
  function sort(left, right) {
    let i = 0;
    let j = 0;
    let result = [];

    while (left[i] && right[j]) {
      if (left[i] < right[j]) {
        result.push(left[i++]);
      } else {
        result.push(right[j++]);
      }
    }

    while (left[i]) {
      result.push(left[i++]);
    }

    while (right[j]) {
      result.push(right[j++]);
    }

    return result;
  }

  function fen(arr) {
    if (arr.length < 2) return arr;
    let midIndex = Math.floor(arr.length / 2);
    let left = arr.slice(0, midIndex);
    let right = arr.slice(midIndex, arr.length);
    return sort(fen(left), fen(right));
  }

  return fen(arr);
}

4、插入排序

var a = [1, 3, 6, 3, 23, 76, 1, 34, 222, 6, 456, 221];

function insertSort(array) {
  const len = array.length
  let current
  let prev
  for (let i = 1; i < len; i++) {
    current = array[i]
    prev = i - 1
    while (prev >= 0 && array[prev] > current) {
      array[prev + 1] = array[prev]
      prev--
    }
    array[prev + 1] = current
  }
  return array
}

insertSort(a); // [1, 1, 3, 3, 6, 6, 23, 34, 76, 221, 222, 456]

5、选择排序

var a = [1, 3, 6, 3, 23, 76, 1, 34, 222, 6, 456, 221];

function selectSort(array) {
  const len = array.length
  let temp
  let minIndex
  for (let i = 0; i < len - 1; i++) {
    minIndex = i
    for (let j = i + 1; j < len; j++) {
      if (array[j] <= array[minIndex]) {
        minIndex = j
      }
    }
    temp = array[i]
    array[i] = array[minIndex]
    array[minIndex] = temp
  }
  return array
}

selectSort(a); // [1, 1, 3, 3, 6, 6, 23, 34, 76, 221, 222, 456]

6、堆排序

var a = [1, 3, 6, 3, 23, 76, 1, 34, 222, 6, 456, 221];

function heap_sort(arr) {
  var len = arr.length
  var k = 0
  function swap(i, j) {
    var temp = arr[i]
    arr[i] = arr[j]
    arr[j] = temp
  }

  function max_heapify(start, end) {
    var dad = start
    var son = dad * 2 + 1
    if (son >= end) return
    if (son + 1 < end && arr[son] < arr[son + 1]) {
      son++
    }

    if (arr[dad] <= arr[son]) {
      swap(dad, son)
      max_heapify(son, end)
    }
  }

  for (var i = Math.floor(len / 2) - 1; i >= 0; i--) {
    max_heapify(i, len)
  }



  for (var j = len - 1; j > k; j--) {
    swap(0, j)
    max_heapify(0, j)
  }
  return arr

}

heap_sort(a); // [1, 1, 3, 3, 6, 6, 23, 34, 76, 221, 222, 456]

区别(时间/空间复杂度): 

  • 0
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue.js 提供了很多方便的指令来循环数组,以下是几种常见的方式: 1. 使用`v-for`指令 `v-for`指令是Vue.js中最常用的指令之一,它可以用来循环数组、对象或数字。对于数组的循环,可以这样写: ``` <div v-for="(item, index) in items" :key="index">{{ item }}</div> ``` 其中,`items`是要循环的数组,`item`是当前循环的元素,`index`是当前循环的索引。注意要为每个循环的元素设置一个唯一的`key`属性。 2. 使用`v-bind`指令 `v-bind`指令可以用来绑定DOM元素的属性,例如`class`、`style`等。对于数组的循环,可以这样写: ``` <div v-bind:class="{ active: item.isActive }" v-for="(item, index) in items" :key="index">{{ item }}</div> ``` 其中,`item.isActive`是一个布尔值,如果为`true`,则给当前循环的元素添加一个`active`类。 3. 使用`v-model`指令 `v-model`指令可以用来实现双向数据绑定,对于数组的循环,可以这样写: ``` <div v-for="(item, index) in items" :key="index"> <input type="text" v-model="items[index]"> </div> ``` 其中,`v-model`指令将输入框的值与数组中对应索引的元素进行双向绑定。 4. 使用`computed`属性 如果需要对数组进行一些计算,例如过滤、排序等,可以使用`computed`属性来处理。例如: ``` computed: { filteredItems: function() { return this.items.filter(function(item) { return item.isActive; }); } } ``` 其中,`filteredItems`是一个计算属性,它返回一个过滤后的数组。 以上是几种常见的循环数组的方式,根据不同的需求选择合适的方式可以使代码更加简洁、易读。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值