当前位置:网站首页>Several sorting methods for while and sort
Several sorting methods for while and sort
2022-07-24 18:12:00 【Solution without encirclement】
dayDif() {
var a = [
3, 11, 19, 14, 9, 19, 43, 432432, 43234, 231, 321321, 323, 54, 546,
];
Least efficient
// for (let i = 0; i < a.length; i++) {
// for (let j = 0; j < a.length; j++) {
// console.log(this.num++, 196);
// if (a[j] > a[j + 1]) {
// [a[j], a[j + 1]] = [a[j + 1], a[j]];
// //console.log(this.num++);
// }
// }
// }
The efficiency is similar to the following
// for (let i = a.length - 1; i >= 0; i--) {
// // The outer layer controls the number of executions and the inner layer j Maximum
// for (let j = 0; j <= i; j++) {
// // Every lying from 0 To j
// console.log(this.num++,105);
// if (a[j] > a[j + 1]) {
// [a[j], a[j + 1]] = [a[j + 1], a[j]];
// }
// }
// }
It's about the same as above
// for (let i = 0; i < a.length - 1; i++) {
// let indexMin = i;
// for (let j = i; j < a.length; j++) {
// console.log(this.num++,104);
// // If the current element Subscript less than the minimum Update the subscript of the minimum value
// if (a[j] < a[indexMin]) {
// indexMin = j;
// }
// }
// // Avoid exchanging with yourself
// if (indexMin !== i) {
// // Exchange data
// [a[i], a[indexMin]] = [a[indexMin], a[i]];
// }
// }
// Insertion sort From the second number , Start to compare If it's big, go to the back And so on to the last number
for (let i = 1; i < a.length; i++) {
// Get the second element
const temp = a[i];
let j = i;
while (j > 0) {
console.log(this.num++, 32);
// If the current element is smaller than the previous element Start moving back
if (a[j - 1] > temp) {
a[j] = a[j - 1];
} else {
// Otherwise, it will jump out of the loop
break;
}
// Decline
j--;
}
// The previous position is assigned to the current element
a[j] = temp;
}
console.log(a);
},边栏推荐
猜你喜欢

What are the pitfalls from single architecture to distributed architecture?

Icml2022 Best Paper Award: learning protein reverse folding from millions of predicted structures

The use and Simulation of character and string library functions in C language

手写博客平台~第二天

Go language interface and type

Install jumpserver

使用Prometheus+Grafana监控MySQL性能指标

Three ways of redis cluster

web渗透经验汇总ing

JumpServer的使用
随机推荐
0612~quartz timer frame
Model saving and loading of sklearn
05mysql lock analysis
web渗透经验汇总ing
Introduction and use of Pinia
Int8 & int8, have you ever stumbled like this?
猜JWT关键字
安装JumpServer
0625~<config>-<bus>
文件上传漏洞——.user.ini与.htaccess
Common methods of number and math classes
Laravel notes - RSA encryption of user login password (improve system security)
0615~用自定义注解实现RBAC权限管理
《STL源码剖析》应该怎样读?
Shengxin commonly used analysis graphics drawing 02 -- unlock the essence of volcano map!
int8 & int8,你栽过这样的跟头吗?
JMeter -- prometheus+grafana server performance visualization
Blackmagic Fusion Studio 18
【obs】依赖库: x264 vs 构建
如何遵循“低耦合”设计原则?