-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathinsert.js
More file actions
39 lines (39 loc) · 879 Bytes
/
Copy pathinsert.js
File metadata and controls
39 lines (39 loc) · 879 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
//在一段有序数组中插入一个数字
const array = [1,2,4,8,9]
const b = 3
const index = array.find(a=>a>b)
if(index===undefined){
array.push(b)
}else{
array.splice(index,0,b)
}
//简化版
const index1 = array.indexOf(b)
array.splice(index1===-1?array.length:index1,0,b)
//原始实现方式
function inster(A,x){
//p指向下一个要比较的元素
//p+1指向空位
let p = A.length-1
while(p>=0&&A[p]>x){
A[p+1] = A[p]
p--
}
A[p+1]=x
}
//整个插入排序的过程
//1.数组的第一个元素和后面一个元素比较
function insertion_sort(A){
for(let i = 1;i<A.length;i++){//主循环执行N-1次
insetr(A,i,A[i])
}
}
function insetr(A,i,x){
let p = i-1
while(p>=0&&A[p]>x){//执行时间不定,最好不走循环提,最坏数组是倒叙
A[p+1]=A[p]
p--
}
A[p+1]=x
}
// 执行时间大概是:(N^2-N)/2