插入排序的基本实现【数据结构与算法—TypeScript 实现】

本文介绍了在TypeScript中实现的插入排序算法,详细分析了其时间复杂度(最好O(n),最坏O(n^2)),适用于小规模已排序数据的排序,以及提供了一个示例代码和应用场景
摘要由CSDN通过智能技术生成

笔记整理自 coderwhy 『TypeScript 高阶数据结构与算法』课程

概念

本质:将数列分为已排序和未排序,将未排序中的元素插入到已排序中的合适位置

特性

复杂度分析

  • 时间复杂度:
    • 最好情况:O(n),有序序列
    • 最坏情况:O(n^2),倒序序列
    • 平均情况:O(n^2),随机数列
  • 空间复杂度:O(n),原地排序

使用场景

时间复杂度为 O(n^2)

  • 适合小数据量的数列
  • 适合有很多已排序好的数列
  • 不适合大数据量的数列

具体代码


/**
 - @desc  : 插入排序
 */
function insertionSort(arr: number[]): number[] {
  // 扫描数组,需知数组长度
  const n = arr.length;

  // 外层遍历,未排序数组的个数
  // 为何从 i 从 1 开始;因为以 i = 0 为初始排序元素,从左至右按升序排列
  for (let i = 1; i < n; i++) {
    // 获取未排序元素作为新数据
    let newData = arr[i];

    // 与新数据的前一个数据比较
    let j = i - 1;

    // 只要前一个数据比新数据大,则需要继续遍历
    // j 为 0,则已找到最前面的位置
    while (arr[j] > newData && j >= 0) {
      arr[j + 1] = arr[j];

      // 每遍历一次,则往前移动
      j--;
    }

    // arr[j] > newData,则更新下一个索引的数据
    // 如果新数据已经在正确的位置则不需更新,小优化点
    // 记录一下:这个小优化点是我自己想到的,自己还是有进步,加油!
    if (j + 1 !== i) {
      arr[j + 1] = newData;
    }
  }

  return arr;
}

// 算法测试
let arr = [10, 90, 20, 100, 50];
console.log('排序前:', arr); // [ 10, 90, 20, 100, 50 ]

let newArr = insertionSort(arr);
console.log('排序后:', newArr); // [ 10, 20, 50, 90, 100 ]

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
TypeScript是一种静态类型的编程语言,它提供了丰富的类型系统和面向对象的特性,使得开发者可以更好地组织和管理代码。在TypeScript中,高阶数据结构算法可以通过类和泛型等特性来实现。 高阶数据结构是指那些在基本数据结构的基础上进行扩展或组合得到的数据结构。例如,堆、图、树等都可以被视为高阶数据结构。在TypeScript中,我们可以使用类来定义这些高阶数据结构,并通过泛型来指定其内部存储的数据类型。 下面是一个使用TypeScript实现的堆(Heap)数据结构的示例: ```typescript class Heap<T> { private data: T[] = []; public size(): number { return this.data.length; } public isEmpty(): boolean { return this.data.length === 0; } public insert(value: T): void { this.data.push(value); this.siftUp(this.data.length - 1); } public extractMin(): T | null { if (this.isEmpty()) { return null; } const min = this.data[0]; const last = this.data.pop()!; if (!this.isEmpty()) { this.data[0] = last; this.siftDown(0); } return min; } private siftUp(index: number): void { while (index > 0) { const parentIndex = Math.floor((index - 1) / 2); if (this.data[index] >= this.data[parentIndex]) { break; } [this.data[index], this.data[parentIndex]] = [this.data[parentIndex], this.data[index]]; index = parentIndex; } } private siftDown(index: number): void { const size = this.size(); while (index * 2 + 1 < size) { let childIndex = index * 2 + 1; if (childIndex + 1 < size && this.data[childIndex + 1] < this.data[childIndex]) { childIndex++; } if (this.data[index] <= this.data[childIndex]) { break; } [this.data[index], this.data[childIndex]] = [this.data[childIndex], this.data[index]]; index = childIndex; } } } ``` 以上是一个最小堆的实现,使用了数组来存储数据,并提供了插入和提取最小值的操作。堆是一种常见的高阶数据结构,用于解决许多问题,如优先队列和排序等。 通过使用TypeScript,我们可以更加清晰地定义和使用高阶数据结构算法,并通过类型检查来减少错误和提高代码的可维护性。当然,这只是其中的一个例子,还有许多其他高阶数据结构算法可以在TypeScript实现
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值