-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
30 lines (27 loc) · 727 Bytes
/
index.js
File metadata and controls
30 lines (27 loc) · 727 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
const { concurrentApiCalls } = require("./concurrentApiCalls");
function insertionSort(list) {
const len = list.length
for (let i = 1; i < len; i++) {
if (list[i] < list[0]) {
// move current element to the first position
list.unshift(list.splice(i, 1)[0])
}
else if (list[i] > list[i - 1]) {
// maintain element position
continue
}
else {
// find where element should go
for (let j = 1; j < i; j++) {
if (list[i] > list[j - 1] && list[i] < list[j]) {
// move element
list.splice(j, 0, list.splice(i, 1)[0])
}
}
}
}
return list
}
const list = [4, 2, 3, 1, 5]
const sorted = insertionSort(list)
console.log(sorted)