需要用到的工具函数类(utils.ts)
/**
** Created with WebStorm IDEA.
** @Author: xinyu
** @Date: 2022/03/01/19:29
** @Description: 基本方法
**/
class baseFunction{
*// public LESS_THAN:number;
*// public BIGGER_THAN:number;
public Compare={LESS_THAN:-1,BIGGER_THAN:1}
public testArray=[5,4,3,2,1];
constructor() {
// this.LESS_THAN=-1;
// this.BIGGER_THAN=1;
}
*//比较两个数大小 第一个数比第二个数大返回1 相等返回0 小于返回-1
public defaultCompare (a,b){
//这里为什么拿不到this.Compare*
// this指向问题 使用call转变this指向对象得以解决,index1中的把1此函数赋值给一个参数 this指向改变
if (a===b)
return 0
return a<b?this.Compare.LESS_THAN:this.Compare.BIGGER_THAN
}
//交换位置
public swap(array,a,b){
[array[a],array[b]] = [array[b],array[a]];
}
//merge函数负责合并和排序小数组来产生大数组,直到回到原始数组并已排序完成
public merge(left,right,compareFn){
let [i,j] = [0,0]
const result = []
while (i<left.length&&j<right.length){
result.push(
compareFn.call(this,left[i],right[j])===this.Compare.LESS_THAN?left[i++]:right[j++]
);
}
return result.concat(i<left.length?left.slice(i):right.slice(j))
}
}
export=baseFunction
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
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
上次更新: 2022/05/12 14:57:53