forked from algorithm024/algorithm024
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsort-basic.js
More file actions
54 lines (53 loc) · 1.09 KB
/
Copy pathsort-basic.js
File metadata and controls
54 lines (53 loc) · 1.09 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// O(n^2) O(1)
function swap(arr, i, j) {
if (i !== j) {
const tmp = arr[i]
arr[i] = arr[j]
arr[j] = tmp
}
}
// 不稳定
function selectionSort(arr) {
const n = arr.length
for (let i = 0; i < n - 1; ++i) {
let j, minIdx = i
for (j = i + 1; j < n; ++j) {
if (arr[j] < arr[minIdx]) {
minIdx = j
}
}
swap(arr, i, minIdx)
}
return arr
}
// 稳定
function insertionSort(arr) {
const n = arr.length
let preIdx, curr
for (let i = 1; i < n; ++i) {
preIdx = i - 1
curr = arr[i]
while (preIdx >= 0 && arr[preIdx] > curr) {
arr[preIdx + 1] = arr[preIdx]
--preIdx
}
arr[preIdx + 1] = curr
}
return arr
}
// 稳定
function bubbleSort(arr) {
const n = arr.length
for (let i = 0; i < n - 1; ++i) {
for (let j = 0; j < n - 1 - i; ++j) {
if (arr[j] > arr[j + 1]) {
swap(arr, j, j + 1)
}
}
}
return arr
}
// ---- test case ----
console.log(selectionSort([3,2,32,45,767,234,66,32]))
console.log(insertionSort([3,2,32,45,767,234,66,32]))
console.log(bubbleSort([3,2,32,45,767,234,66,32]))