-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathRank.ts
62 lines (60 loc) · 1.16 KB
/
Rank.ts
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// 排序算法
class Rank{
/**
* 插入排序
* @param {Array<number>} ...a [需要排序的数据]
*/
static insert([...a]:Array<number>):Array<number>{
var key:number,j:number;
for(var i:number=2;i <= a.length;i++){
key = a[i];
j = i-1;
while(i>0 && a[j]>key){
a[j+1]=a[j];
j=j-1;
a[j+1]=key;
}
}
return a;
}
/**
* 冒泡排序
* @param {Array<number>} ...a [需要排序的数据]
* @return {Array<number>} [排序好的数据]
*/
static bubbleSort([...a]:Array<number>):Array<number>{
let temp:number=0;
for(let i=0;i<a.length-1;i++){
for(let j=0;j<a.length-i;j++){
if(a[j]>a[j+1]){
temp=a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
}
return a;
}
static SelectSort(){
}
static quickSort([...arry]:Array<number>,begin:number,end:number):Array<number>{
var i=begin,j=end,key=arry[begin];
while(i<j){
while(i<j&&arry[j]>=key) j--;
if (i<j) {
arry[i]=arry[j];
i++;
}
while(i<j&&arry[i]<key) i++;
if(i<j){
arry[j]=arry[i];
j--;
}
}
arry[i]=key;
Rank.quickSort(arry,begin,i-1);
Rank.quickSort(arry,i+1,end);
return arry;
}
}
export {Rank};