当前位置:网站首页>排序--选择排序
排序--选择排序
2022-08-07 06:55:00 【草莓味巧克力豆】
选择排序改进了冒泡排序
将交换次数由o(n^2) 减少到o(n)
但是比较的次数依然是o(n^2)
思路
- 选定第一个索引的位置,然后和后面的元素依次比较
- 如果后面的数,小于第一个索引的数,就交换位置
- 经过一轮比较之后,可以确定第一个位置是最小的 然后使用同样的方法把剩下的元素逐个进行比较
- 可以看出选择排序;第一轮会选出一个最小值,第二轮会选出第二个最小的值,直到最后。
function changeSort(nums){
let len = nums.length,min = 0
// 第一层循环
for(let i = 0;i < len;i++){
min = i
for(let j = i + 1;j < len;j++){
if(nums[j] < nums[min]){
min = j
}
}
[nums[min], nums[i]] = [nums[i], nums[min]]
}
console.log(nums)
}
边栏推荐
- LeetCode's sword is Offer 06. Print the linked list from end to end
- PriorityQueue(优先队列)
- 有 5nm 制程工艺的 MCU 吗?
- VoLTE基础自学系列 | IMS网络概述
- Top 20 most popular plugins for QGIS
- LeetCode 剑指 Offer 09. 用两个栈实现队列
- servlet tutorial 1: environment setup and new servlet project
- servlet tutorial 2: return to the jsp page
- 网络安全笔记3——双钥密码体制
- [Array Questions] LeetCode 969. Pancake Sorting
猜你喜欢

VoLTE Basic Self-Learning Series | Enterprise Voice Network Brief

动手学深度学习--计算性能篇

【井字棋】

程序员福音,关于如何使用Markdown写出一份漂亮的简历 —— 程序员简历 | md文档简历制作教程

C trap - infinite loop problem caused by array out of bounds

2022A特种设备相关管理(电梯)特种作业证考试题库模拟考试平台操作

LeetCode 剑指 Offer 06. 从尾到头打印链表

接口流量突增,如何做好性能调优?

2022年T电梯修理考试题模拟考试题库及答案

10 years of experience summary: 7 tools for data analysts, focus on causal analysis!
随机推荐
学神经网络需要什么基础,神经网络需要什么基础
神经网络ppt不足之处怎么写,神经网络ppt免费下载
31. 了解什么是 redis 的雪崩、穿透和击穿?redis 崩溃之后会怎么样?应对措施是什么
A Pursuit of Temporal Accuracy in General Activity Detection TAG论文阅读笔记
网络安全笔记3——双钥密码体制
[Acwing Weekly Replay] The 63rd weekly match 20220806
2020年初全国行政区划矢量数据
【Promise】Promise 使用 / 回调地狱问题 async-await /宏队列与微队列
LeetCode 剑指 Offer 24. 反转链表
netstat&firewall
How to use the @Async annotation
VoLTE Basic Self-Learning Series | Which scenarios will trigger CSFB on VoLTE terminals?
OC-run loop
servlet 教程 1:环境搭建和新建 servlet 项目
Are there MCUs with 5nm process technology?
0-1背包问题
MySQL - 索引优化
为什么Move将超越Solidity成为主流编程语言?
动手学深度学习--计算性能篇
360 Digital Security Brain of the Whole Network Won the "Digital Economy Innovation Leading Achievement" Award