十大经典排序算法 👈

冒泡排序 O(n²)

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
function bubbleSort(arr) {
let len = arr.length
for (let j = 0; j < len; j++) {
let isSwap = false
for (let i = 0; i < len - 1 - j; i++) {
if (arr[i] > arr[i + 1]) {
isSwap = true
;[arr[i], arr[i + 1]] = [arr[i + 1], arr[i]]
}
}
if (!isSwap) break
}
return arr
}

function bubbleSort(arr) {
let j = arr.length - 1
while (j > 0) {
for (let i = 0; i < j; i++) {
if (arr[i] > arr[i + 1]) {
[arr[i], arr[i + 1]] = [arr[i + 1], arr[i]]
}
}
j--
}
return arr
}

bubbleSort([4, 5, 6, 3, 2, 1, 8, 9, 7])

选择排序 O(n²)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
function selectSort(arr) {
let len = arr.length
let minIndex
for (let i = 0; i < len - 1; i++) {
minIndex = i
for (let j = i + 1; j < len; j++) {
if (arr[j] < arr[minIndex]) {
minIndex = j
}
}
[arr[i], arr[minIndex]] = [arr[minIndex], arr[i]]
}
return arr
}
selectSort([4, 5, 6, 3, 2, 1, 8, 9, 7])

插入排序 O(n²)

图书馆整理图书

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
function insertSort(arr) {
let len = arr.length
let preIndex, current
for (let i = 1; i < len; i++) {
preIndex = i - 1
current = arr[i]
while(preIndex >= 0 && current < arr[preIndex]) {
arr[preIndex + 1] = arr[preIndex]
preIndex--
}
arr[preIndex + 1] = current
}
return arr
}
insertSort([4, 5, 6, 3, 2, 1, 8, 9, 7])

希尔排序 O(nlogn)

归并排序 O(nlogn)

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
function merge(left, right) {
let result = []

while (left.length && right.length) {
if (left[0] <= right[0]) {
result.push(left.shift())
} else {
result.push(right.shift())
}
}

while (left.length)
result.push(left.shift())

while (right.length)
result.push(right.shift())

return result
}
// function mergeSortEx(arr, start, end) {
// if (start < end) {
// /* 分而 */
// let middle = Math.floor((start + end) / 2),
// left = mergeSortEx(arr, start, middle),
// right = mergeSortEx(arr, middle + 1, end)
// /* 治之 */
// return merge(left, right)
// }
// return [arr[end]]
// }
// function mergeSort(arr) {
// return mergeSortEx(arr, 0, arr.length - 1)
// }
function mergeSort(arr) {
let len = arr.length
if (len < 2) return arr
let middle = Math.floor(len / 2),
left = arr.slice(0, middle),
right = arr.slice(middle)
/* 分而治之 */
return merge(mergeSort(left), mergeSort(right))
}
mergeSort([4, 5, 6, 3, 2, 1, 8, 9, 7])

快速排序 O(nlogn) 👍

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
function quickSort(arr) {
let len = arr.length
if (len < 2) {
return arr
} else {
let flag = arr[0]
let left = []
let right = []
for (let i = 1, temp; i < len; i++) {
temp = arr[i]
if (temp < flag) {
left.push(temp)
} else {
right.push(temp)
}
}
return quickSort(left).concat(flag, quickSort(right))
}
}
quickSort([4, 5, 6, 3, 2, 1, 8, 9, 7])

原地快排

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
function partition(arr, start, end) {
let pivotPos = start
const pivot = arr[start] // 基准值

for (let i = start + 1; i <= end; i++) {
if (arr[i] < pivot) {
pivotPos++
if (pivotPos !== i) {
[arr[pivotPos], arr[i]] = [arr[i], arr[pivotPos]]
}
}
}
arr[start] = arr[pivotPos]
arr[pivotPos] = pivot

return pivotPos
}
function quickSortEx(arr, start, end) {
if (start < end) {
let pivotPos = partition(arr, start, end)
/* 分治 */
quickSortEx(arr, start, pivotPos - 1)
quickSortEx(arr, pivotPos + 1, end)
}
}
function quickSort(arr) {
quickSortEx(arr, 0, arr.length - 1)
return arr
}
quickSort([4, 5, 6, 3, 2, 1, 8, 9, 7])

双指针

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
const quickSort = (function () {
/* 分治函数 */
function partition(arr, start, end) {
let v = arr[start]
let i = start + 1
let j = end

while (i < j) {
while (arr[i] <= v && i <= j) {
i++
}
while (arr[j] >= v && j >= i) {
j--
}
if (i < j) {
[arr[j], arr[i]] = [arr[i], arr[j]]
}
}

if (v > arr[j]) {
[arr[start], arr[j]] = [arr[j], v]
}

return j
}
function quickSortEx(arr, start, end) {
if (start < end) {
let pivotPos = partition(arr, start, end)
quickSortEx(arr, start, pivotPos - 1)
quickSortEx(arr, pivotPos + 1, end)
}
}
return function quickSort(arr) {
quickSortEx(arr, 0, arr.length - 1)
return arr
}
})()
quickSort([4, 5, 6, 3, 2, 1, 8, 9, 7])

快排 + 插入排序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
const sort = (function() {
function sortEx(arr, start, end) {
if (start < end) {
if (end - start <= 25) {
insertSort(arr)
} else {
let pivotPos = partition(arr, start, end)
sortEx(arr, start, pivotPos - 1)
sortEx(arr, pivotPos + 1, end)
}
}
}
return function sort(arr) {
sortEx(arr, 0, arr.length - 1)
return arr
}
})()
sort([4, 5, 6, 3, 2, 1, 8, 9, 7])

堆排序

计数排序

桶排序

基数排序