Skip to content

Commit d6f65ed

Browse files
committed
add insert sort and bubble sort
1 parent 8e564ac commit d6f65ed

File tree

2 files changed

+130
-0
lines changed

2 files changed

+130
-0
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/**
2+
* 递归求解爬楼梯问题
3+
*/
4+
5+
/**
6+
* 最开始版本的递归算法
7+
* @param n
8+
*/
9+
function fn(n: number): any {
10+
if (n === 1) return 1
11+
if (n === 2) return 2
12+
return fn(n - 1) + fn(n - 2)
13+
}
14+
15+
const res1 = fn(10)
16+
// 89
17+
console.log(res1)
18+
19+
/**
20+
* 使用depth结合js的闭包特性实现限制函数调用次数的功能
21+
* @param depth 递归的深度
22+
*/
23+
function fnWithDepth(depth: number) {
24+
return function fn(n: number): any {
25+
depth++
26+
if (depth > 1000) throw new Error('function stack is too deep!')
27+
if (n === 1) return 1
28+
if (n === 2) return 2
29+
return fn(n - 1) + fn(n - 2)
30+
}
31+
}
32+
33+
const res2 = fnWithDepth(3)(10)
34+
// 89
35+
console.log(res2)
36+
37+
/**
38+
* 通过map来存储已经计算过的值,避免递归重复计算
39+
*/
40+
function fnWithMap() {
41+
const map = new Map<number, number>()
42+
return function fn(n: number): any {
43+
if (n === 1) return 1
44+
if (n === 2) return 2
45+
if (map.has(n)) {
46+
return map.get(n)
47+
}
48+
const ret = fn(n - 1) + fn(n - 2)
49+
map.set(n, ret)
50+
return ret
51+
}
52+
}
53+
54+
const res3 = fnWithMap()(10)
55+
console.log(res3)

typescript/11_sorts/simpleSort.ts

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/**
2+
* 简单的排序,分为冒泡和插入排序
3+
* 注意他们都是稳定的排序,并且是原地排序
4+
* 一般情况下更推荐使用插入排序,因为它所需要的操作更少
5+
* 这里使用简单工厂创建我们的排序算法
6+
*/
7+
8+
/**
9+
* 排序的枚举类型
10+
*/
11+
enum SortType {
12+
BubbleSort,
13+
InsertSort
14+
}
15+
16+
interface SortAlgo {
17+
sort(array: number[]): void
18+
}
19+
20+
class BubbleSort implements SortAlgo {
21+
sort(array: number[]) {
22+
for (let i = 0; i < array.length; i++) {
23+
let flag = false
24+
for (let j = 0; j < array.length; j++) {
25+
if (array[j] > array[j + 1]) {
26+
const temp = array[j]
27+
array[j] = array[j + 1]
28+
array[j + 1] = temp
29+
flag = true
30+
}
31+
}
32+
if (!flag) {
33+
break
34+
}
35+
}
36+
}
37+
}
38+
39+
class InsertSort implements SortAlgo {
40+
sort(array: number[]) {
41+
for (let i = 1; i < array.length; i++) {
42+
let j = i - 1
43+
const temp = array[i]
44+
for (; j >= 0; j--) {
45+
if (array[j] > array[j + 1]) {
46+
array[j + 1] = array[j]
47+
} else {
48+
// 这个说明之前的已经排好了,没必要继续比较
49+
break
50+
}
51+
}
52+
array[j + 1] = temp
53+
}
54+
}
55+
}
56+
57+
class SortFactory {
58+
static getSortAlgo(type: SortType): SortAlgo {
59+
switch (type) {
60+
case SortType.BubbleSort:
61+
return new BubbleSort()
62+
case SortType.InsertSort:
63+
return new InsertSort()
64+
default:
65+
throw new Error('unknown sort algorithm type')
66+
}
67+
}
68+
}
69+
70+
const insertSort = SortFactory.getSortAlgo(SortType.InsertSort)
71+
const test1 = [1, 0, 2, 4, 8, 5, 10]
72+
insertSort.sort(test1)
73+
console.log(test1)
74+
75+

0 commit comments

Comments
 (0)