每日前端手写题--day11

以下题目来自掘金等其它博客,但是问题的答案都是根据笔者自己的理解做出的。如果你最近想要换工作或者巩固一下自己的前端知识基础,不妨和我一起参与到每日刷题的过程中来,如何?

第11天要刷的手写题如下:

  1. 使用js实现选择排序
  2. 使用js实现插入排序
  3. 使用js实现希尔排序

下面是我自己写的答案:

1. 使用js实现选择排序

  • 原理:遍历数组,将搜索当前索引位置及之后的元素中的最小值,找到之后和当前索引位置的元素交换位置。
  • 简单来说就是:选择剩余数组中最小的值放到有序数组末尾。
  • 技巧:将数组元素交换过程和寻找剩余数组中最小元素的过程抽离成独立方法可以使整个过程更加清晰。
js 复制代码
function selectSort(source) {
    const _tmp = [...source];
    // 寻找指定区间之内的最小值对应的下标
    const findMinIndex = (start, end) => {
        let _curMin = _tmp[end];
        let _curMinIndex = end;
        for (let i = start; i <= end; i++) {
            if (_tmp[i] < _curMin) {
                _curMin = _tmp[i];
                _curMinIndex = i;
            }
        }
        return _curMinIndex;
    }
    // 交换数组两个元素
    const swap = (i, j) => {
        if (i === j) return;
        [_tmp[i], _tmp[j]] = [_tmp[j], _tmp[i]];
    }
    // 遍历,并且交换顺序
    for (let i = 0; i < _tmp.length; i++) {
        const _curMinIndex = findMinIndex(i, _tmp.length - 1);
        swap(i, _curMinIndex);
    }

    return _tmp;
}

const a = [0, 5, 36, 5, 6, 6, 58, 2, 3, 6]; // [0, 2, 3, 5, 5, 6, 6, 6, 36, 58]
console.log(selectSort(a));
  1. 使用js实现插入排序
  • 原理:将元素插入到已经存在的有序数组中去
  • 实现过程:遍历数组,将当前索引位置的元素插入到此索引位置之前的有序数组中去
  • 理解:假如现在遍历到的索引位置为n,则[0...n-1]已经是有数组了,对于n索引位置的元素x,遍历[0...n-1],找到x应该所在的位置p,将[p...n-1]的元素逐个向后移动,变成[p+1...n],然后将x放到p位置上。
  • 难点:p=0的情况的处理
js 复制代码
function insertSortClassic(source) {
    const _tmp = [...source];
    const _len = _tmp.length;

    for (let i = 1; i < _len; i++) {
        const current = _tmp[i]; // 当前待插入元素
        let j = i - 1; // 拟插入位置

        while (j >= 0 && _tmp[j] > current) {
            _tmp[j + 1] = _tmp[j]; // 元素后移
            j -= 1;
        }

        _tmp[j + 1] = current; // 插入当前元素到正确位置
    }

    return _tmp;
}

const b = [0, 5, 36, 5, 6, 6, 58, 2, 3, 6]; // [0, 2, 3, 5, 5, 6, 6, 6, 36, 58]
console.log(insertSort(b))

3. 使用js实现希尔排序

  • 捷径:改造插入排序算法,获得希尔排序算法
    *
    1. 引入gap,初始值为1;将原来插入排序中的所有1用gap替换
      1. 给原来插入排序和循环外面加一层循环(希尔排序的精髓所在)
      1. 将gap的初始值从1修改成数组长度的一半,直接从插入排序变成希尔排序
js 复制代码
function shellSort(source) {
   const _tmp = [...source];
   const _len = _tmp.length;
   let gap = _len >> 1; // 改动1

   while (gap) { // 改动2
       for (let i = gap; i < _len; i++) {
           const current = _tmp[i];
           let j = i - gap;
   
           while (j >= 0 && _tmp[j] > current) {
               _tmp[j + gap] = _tmp[j];
               j -= gap;
           }
   
           _tmp[j + gap] = current; 
       }
   
       gap = gap >> 1; // 改动3
   }


   return _tmp;
}

const c = [0, 5, 36, 5, 6, 6, 58, 2, 3, 6]; // [0, 2, 3, 5, 5, 6, 6, 6, 36, 58]
console.log(shellSort(c))

后记:虽然实现希尔排序基于插入排序,但是对于希尔排序本身还是需要掌握其原理的:

  • 希尔排序的实现过程:
    *
    1. 生成gap数列
      1. 对于每一个gap值排序
  • 误区:
    • 第二步对于每一个gap值排序的时候,不一定要使用插入排序
相关推荐
yours_Gabriel1 小时前
【java面试】微服务篇
java·微服务·中间件·面试·kafka·rabbitmq
crary,记忆2 小时前
Angular微前端架构:Module Federation + ngx-build-plus (Webpack)
前端·webpack·angular·angular.js
漂流瓶jz2 小时前
让数据"流动"起来!Node.js实现流式渲染/流式传输与背后的HTTP原理
前端·javascript·node.js
SamHou02 小时前
手把手 CSS 盒子模型——从零开始的奶奶级 Web 开发教程2
前端·css·web
我不吃饼干3 小时前
从 Vue3 源码中了解你所不知道的 never
前端·typescript
开航母的李大3 小时前
【中间件】Web服务、消息队列、缓存与微服务治理:Nginx、Kafka、Redis、Nacos 详解
前端·redis·nginx·缓存·微服务·kafka
Bruk.Liu3 小时前
《Minio 分片上传实现(基于Spring Boot)》
前端·spring boot·minio
鱼樱前端3 小时前
Vue3+d3-cloud+d3-scale+d3-scale-chromatic实现词云组件
前端·javascript·vue.js
coding随想4 小时前
JavaScript中的原始值包装类型:让基本类型也能“变身”对象
开发语言·javascript·ecmascript