js常用的几种排序方式

文章介绍了JavaScript中几种常见的排序算法,包括冒泡排序、插入排序、选择排序和快速排序,给出了每种排序算法的实现示例,并提到了JavaScript内置的Array.prototype.sort()函数用于排序。
摘要由CSDN通过智能技术生成

在JavaScript中,有多种排序方式可供选择。以下是几种常见的排序方式以及对应的示例:

  1. 冒泡排序(Bubble Sort): 冒泡排序是一种比较简单的排序算法,它重复地比较相邻的两个元素并交换位置,直到整个数组排序完成。
    function 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;
    }
    
    const nums = [5, 3, 8, 4, 2];
    const sortedNums = bubbleSort(nums); // [2, 3, 4, 5, 8]
    

  2. 插入排序(Insertion Sort): 插入排序的思想是将数组分为已排序和未排序两部分,每次从未排序部分取出一个元素插入到已排序部分的正确位置。
    function insertionSort(arr) {
      const len = arr.length;
      for (let i = 1; i < len; i++) {
        let current = arr[i];
        let j = i - 1;
        while (j >= 0 && arr[j] > current) {
          arr[j + 1] = arr[j];
          j--;
        }
        arr[j + 1] = current;
      }
      return arr;
    }
    
    const nums = [5, 3, 8, 4, 2];
    const sortedNums = insertionSort(nums); // [2, 3, 4, 5, 8]
    

  3. 选择排序(Selection Sort): 选择排序的思想是每次从未排序部分选择最小(或最大)的元素,放到已排序部分的末尾。
    function selectionSort(arr) {
      const len = arr.length;
      for (let i = 0; i < len - 1; i++) {
        let minIndex = i;
        for (let j = i + 1; j < len; j++) {
          if (arr[j] < arr[minIndex]) {
            minIndex = j;
          }
        }
        if (minIndex !== i) {
          [arr[i], arr[minIndex]] = [arr[minIndex], arr[i]]; // 交换位置
        }
      }
      return arr;
    }
    
    const nums = [5, 3, 8, 4, 2];
    const sortedNums = selectionSort(nums); // [2, 3, 4, 5, 8]
    

  4. 快速排序(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));
    }
    
    const nums = [5, 3, 8, 4, 2];
    const sortedNums = quickSort(nums); // [2, 3, 4, 5, 8]
    

    这些是几种常见的排序方式和对应的示例。值得注意的是,在实际应用中,可以根据排序需求和数据规模选择合适的排序算法。另外,JavaScript还提供了内置的排序函数Array.prototype.sort(),可以直接调用该函数对数组进行排序。

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`是一个计算属性,它返回一个过滤后的数组。 以上是几种常见的循环数组的方式,根据不同的需求选择合适的方式可以使代码更加简洁、易读。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值