-
Notifications
You must be signed in to change notification settings - Fork 0
/
bubble.js
119 lines (104 loc) · 1.94 KB
/
bubble.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
let value = [],i=0,j=0,flag=0;
let run_button,reset_button;
let slider;
let canvas;
let t1=-1,t2=-1;
function setup() {
canvas = createCanvas(400,400);
canvas.position(800,100);
slider = createSlider(1,width/10,width/20);
run_button = createButton("SORT");
reset_button = createButton("RESET");
sketch();
run_button.addClass("btn btn-success");
slider.input(changearray);
run_button.mousePressed(run);
reset_button.mousePressed(reset);
}
function changearray()
{
i=0;
j=0;
value=[];
for(let i=0;i<slider.value();i++)
{
value[i]=int(random(height));
}
}
function run()
{
i=0;
j=0;
flag=1;
}
function reset()
{
flag=0;
sketch();
loop();
}
function sketch(){
i=0;
j=0;
t1=-1;t2=-1;
for(let i=0;i<slider.value();i++)
{
value[i]=int(random(2,height));
}
}
function draw() {
//print("IN DRAW");
background(255);
stroke(255);
//draws rectangle vertically of width 10
for(let i=0;i<value.length;i++)
{
if(i==t1 || i==t2)
{
fill(255,0,0);
}
else
{
fill(0);
}
rect(i*10,height-value[i],10,value[i]);
}
//selection sort doing one step at a time
if (flag) {
if (i < value.length - 1) {
if (j < value.length - i - 1) {
if (value[j] > value[j + 1]) {
t1 = j;
t2 = j + 1
swap(j, j + 1);
}
j++;
} else {
i++;
j = 0;
}
}
}
}
//Swaps min index and current element
function swap(i,min_ind)
{
let temp = value[min_ind];
value[min_ind] = value[i];
value[i] = temp;
}
// if (flag) {
// if (i < value.length - 1) {
// if (j < value.length - i - 1) {
// if (value[j] > value[j + 1]) {
// t1 = j;
// t2 = j + 1
// swap(j, j + 1);
// }
// j++;
// } else {
// i++;
// j = 0;
// }
// }
// }