wtw's Frontend

道阻且长,行则将至💪🏻

0%

think of algorithm

算法

排序

冒泡排序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
function bubbleSort(array) {
const result = [...array],
length = result.length;
for (let i = 0; i < length; i++) {
for (let j = 0; j < length - i; j++) {
if (result[j] > result[j + 1]) {
const temp = result[j];
result[j] = result[j + 1];
result[j + 1] = temp;
}
}
}
return result;
}

插入排序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
function insertSort(array) {
const result = [...array],
length = result.length;
for (let i = 0; i < length; i++) {
let j = i;
while ((result[j - 1] > result[j]) && j > 0) {
const temp = result[j];
result[j] = result[j - 1];
result[j - 1] = temp;
j--;
}
}
return result;
}

归并排序

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
function merge(a, b) {
const a_length = a.length,
b_length = b.length,
arr = [];
let i = 0,
j = 0;
while (i < a_length && j < b_length) {
if (a[i] <= b[j]) {
arr.push(a[i]);
i++;
} else {
arr.push(b[j]);
j++
}
}
if (i === a_length && j < b_length) {
while (j < b_length) {
arr.push(b[j]);
j++;
}
}
if (j === b_length && i < a_length) {
while (i < a_length) {
arr.push(a[i]);
i++
}
}
return arr;
}

function mergeSort(arr) {
const length = arr.length;
if (length <= 1) {
return arr;
}
const middle = Math.floor(length / 2),
left = arr.slice(0, middle),
right = arr.slice(middle);
return merge(mergeSort(left), mergeSort(right));
}

快速排序

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 benchMark(arr, left, right) {
let i = left,
j = right,
x = arr[left];
while (i < j) {
while (i < j && arr[j] >= x) {
j--;
}
if (i < j) {
arr[i] = arr[j];
}
while (i < j && arr[i] <= x) {
i++
}
if (i < j) {
arr[j] = arr[i];
}
}
arr[i] = x;
return i;
}

function quickSort(arr, left = 0, right = arr.length - 1) {
if (left < right) {
const index = benchMark(arr, left, right);
quickSort(arr, left, index - 1);
quickSort(arr, index + 1, right);
}
return arr;
}

获取最长字符子串

1
2
3
4
5
6
7
8
9
10
11
12
function getLongStr(str) {
let arr = [], max = 0;
for (let i = 0; i < str.length; i++) {
const index = arr.indexOf(str[i]);
if (index !== -1) {
arr.splice(0, index + 1);
}
arr.push(str.charAt(i));
max = Math.max(arr.length, max);
}
return max;
}