总共遍历一遍数组实现功能(选择排序实现)

来源:5-8 缺失的第一个正数(1)

慕码人2187359

2020-02-05

  • 下面是代码:
// 测试数据如下:
let testArray = [3, 4, -1, 1]
// 默认最小正整数为:1
let zhengZhengShu = 1

for (let i = 0; i < testArray.length; i++) { // 排序的次数控制
  let mid = testArray[i]
  for (let j = i + 1, c; j < testArray.length; j++) { // 每一次循环的内容
    if (testArray[j] < mid) {
      c = testArray[j]
      testArray[j] = mid
      mid = c
    }
  }
  testArray[i] = mid
  if (testArray[i] === zhengZhengShu) {
    zhengZhengShu++
  }
}
console.log('打印排序后的数组:', testArray)
console.log('zhengZhengShu:', zhengZhengShu)
写回答

1回答

快乐动起来呀

2020-02-05

有没有在LeetCode跑一下全部的测试用例呢?算法没有标准答案,可以多尝试多思考

0
1
慕码人2187359
哦,跑了。
2020-02-05
共1条回复

JavaScript版 数据结构与算法

填补前端同学的算法短板,掌握面试中最常见的算法与数据结构

2467 学习 · 395 问题

查看课程