Skip to content

Commit b97510d

Browse files
Merge pull request wangzheng0822#86 from nameczz/master
[javascript] 11 & 12 sorts
2 parents fb3fdc1 + 2226604 commit b97510d

File tree

3 files changed

+157
-0
lines changed

3 files changed

+157
-0
lines changed

javascript/11_sorts/sort.js

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/**
2+
* 冒泡,插入,选择排序
3+
*
4+
* Author: nameczz
5+
*/
6+
7+
// 冒泡排序
8+
const bubbleSort = (arr) => {
9+
if (arr.length <= 1) return
10+
for (let i = 0; i < arr.length; i++) {
11+
let hasChange = false
12+
for (let j = 0; j < arr.length - i - 1; j++) {
13+
if (arr[j] > arr[j + 1]) {
14+
const temp = arr[j]
15+
arr[j] = arr[j + 1]
16+
arr[j + 1] = temp
17+
hasChange = true
18+
}
19+
}
20+
// 如果false 说明所有元素已经到位
21+
if (!hasChange) break
22+
}
23+
console.log(arr)
24+
}
25+
26+
// 插入排序
27+
const insertionSort = (arr) => {
28+
if (arr.length <= 1) return
29+
for (let i = 1; i < arr.length; i++) {
30+
const temp = arr[i]
31+
let j = i - 1
32+
// 若arr[i]前有大于arr[i]的值的化,向后移位,腾出空间,直到一个<=arr[i]的值
33+
for (j; j >= 0; j--) {
34+
if (arr[j] > temp) {
35+
arr[j + 1] = arr[j]
36+
} else {
37+
break
38+
}
39+
}
40+
arr[j + 1] = temp
41+
}
42+
console.log(arr)
43+
}
44+
45+
// 选择排序
46+
const selectionSort = (arr) => {
47+
if (arr.length <= 1) return
48+
// 需要注意这里的边界, 因为需要在内层进行 i+1后的循环,所以外层需要 数组长度-1
49+
for (let i = 0; i < arr.length - 1; i++) {
50+
let minIndex = i
51+
for (let j = i + 1; j < arr.length; j++) {
52+
if (arr[j] < arr[minIndex]) {
53+
minIndex = j // 找到整个数组的最小值
54+
}
55+
}
56+
const temp = arr[i]
57+
arr[i] = arr[minIndex]
58+
arr[minIndex] = temp
59+
}
60+
console.log(arr)
61+
}
62+
63+
const test = [4, 5, 6, 3, 2, 1]
64+
bubbleSort(test)
65+
const testSort = [4, 1, 6, 3, 2, 1]
66+
insertionSort(testSort)
67+
const testSelect = [4, 8, 6, 3, 2, 1, 0, 12]
68+
selectionSort(testSelect)

javascript/12_sorts/MergeSort.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* 归并排序
3+
*
4+
* Author: nameczz
5+
*/
6+
7+
const mergeArr = (left, right) => {
8+
let temp = []
9+
let leftIndex = 0
10+
let rightIndex = 0
11+
// 判断2个数组中元素大小,依次插入数组
12+
while (left.length > leftIndex && right.length > rightIndex) {
13+
if (left[leftIndex] <= right[rightIndex]) {
14+
temp.push(left[leftIndex])
15+
leftIndex++
16+
} else {
17+
temp.push(right[rightIndex])
18+
rightIndex++
19+
}
20+
}
21+
// 合并 多余数组
22+
return temp.concat(left.slice(leftIndex)).concat(right.slice(rightIndex))
23+
}
24+
25+
const mergeSort = (arr) => {
26+
// 当任意数组分解到只有一个时返回。
27+
if (arr.length <= 1) return arr
28+
const middle = Math.floor(arr.length / 2) // 找到中间值
29+
const left = arr.slice(0, middle) // 分割数组
30+
const right = arr.slice(middle)
31+
// 递归 分解 合并
32+
return mergeArr(mergeSort(left), mergeSort(right))
33+
}
34+
35+
const testArr = []
36+
let i = 0
37+
while (i < 100) {
38+
testArr.push(Math.floor(Math.random() * 1000))
39+
i++
40+
}
41+
42+
const res = mergeSort(testArr)
43+
console.log(res)

javascript/12_sorts/QuickSort.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* 快速排序
3+
*
4+
* Author: nameczz
5+
*/
6+
7+
const swap = (arr, i, j) => {
8+
const temp = arr[i]
9+
arr[i] = arr[j]
10+
arr[j] = temp
11+
}
12+
13+
// 获取 pivot 交换完后的index
14+
const partition = (arr, pivot, left, right) => {
15+
const pivotVal = arr[pivot]
16+
let startIndex = left
17+
for (let i = left; i < right; i++) {
18+
if (arr[i] < pivotVal) {
19+
swap(arr, i, startIndex)
20+
startIndex++
21+
}
22+
}
23+
swap(arr, startIndex, pivot)
24+
return startIndex
25+
}
26+
27+
const quickSort = (arr, left, right) => {
28+
if (left < right) {
29+
let pivot = right
30+
let partitionIndex = partition(arr, pivot, left, right)
31+
quickSort(arr, left, partitionIndex - 1 < left ? left : partitionIndex - 1)
32+
quickSort(arr, partitionIndex + 1 > right ? right : partitionIndex + 1, right)
33+
}
34+
35+
}
36+
37+
38+
const testArr = []
39+
let i = 0
40+
while (i < 10) {
41+
testArr.push(Math.floor(Math.random() * 1000))
42+
i++
43+
}
44+
console.log('unsort', testArr)
45+
quickSort(testArr, 0, testArr.length - 1);
46+
console.log('sort', testArr)

0 commit comments

Comments
 (0)