-
Notifications
You must be signed in to change notification settings - Fork 0
/
obj.js
137 lines (120 loc) · 3.53 KB
/
obj.js
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
class ObjSet{
constructor(nums, type = "bar"){
this.nums = nums
this.obj = []
this.visType = type
this.swaps = 0
this.comparisons = 0
for(let i = 0 ; i < nums; ++i){
this.obj.push(new Object(i + 1, i + 1))
}
}
randomise(){
for(let i = 0 ; i < nums ; ++i){
let rand = Math.ceil(Math.random() * (nums - 1))
let temp = this.obj[i]
this.obj[i] = this.obj[rand]
this.obj[rand] = temp
}
}
compare(i, j){
this.comparisons++
return (this.obj[i].key < this.obj[j].key)
}
getClone(begin, end){
let clone = new ObjSet(0, this.visType);
for (let i = begin ; i <= end ; ++i){
clone.obj.push(new Object(this.obj[i].key, this.obj[i].val))
clone.nums++
}
return clone
}
async draw(ctx = this.ctx){
this.ctx = ctx
switch(this.visType){
case "bar":
await Visualizer.barVisualizer(ctx, this)
break
case "point":
await Visualizer.pointVisualizer(ctx, this)
break
case "coloredTri":
await Visualizer.coloredTriangleVisualizer(ctx, this)
break
}
}
async sort(type){
this.comparisons = 0
this.swaps = 0
switch(type){
case 'merge':
await Algorithms.mergesort(this, 0, this.nums - 1)
break
case "quick":
await Algorithms.quicksort(this, 0, this.nums - 1)
break
case "selection":
await Algorithms.selectionsort(this)
break
case "bubble":
await Algorithms.bubblesort(this)
break
case "insertion":
await Algorithms.insertionsort(this)
break
case "comb":
await Algorithms.combsort(this)
break
case "counting":
await Algorithms.countingSort(this)
break
case "radix":
await Algorithms.radixSort(this)
break
case "cyclic":
await Algorithms.cyclicSort(this)
break
}
this.issorted = await this.check()
}
async swap(i, j){
await this.recolor(i, j, "red")
let temp = this.obj[i]
this.obj[i] = this.obj[j]
this.obj[j] = temp
await this.recolor(i, j, "green")
await this.recolor(i, j, "#e5eaf5")
this.swaps++
}
async assignNode(i, arr ,j){
await this.recolor(i, j, "red")
this.obj[i] = arr[j]
await this.recolor(i, j, "green")
await this.recolor(i, j, "#e5eaf5")
}
async recolor(i, j, clr){
this.obj[j].color = clr
this.obj[i].color = clr
await this.draw(this.ctx, this.visType)
}
async check(){
for(let i = 0 ; i < this.nums - 1 ; ++i){
if(this.obj[i].key < this.obj[i + 1].key){
await this.recolor(i, i + 1, "green")
await this.recolor(i, i + 1, "#e5eaf5")
}
else{
await this.recolor(i, i + 1, "red")
return false
}
}
return true
}
}
class Object{
constructor(key, val, color = "#e5eaf5") {
this.key = key
this.val = val
this.color = color
}
}