-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinarySearch.js
More file actions
206 lines (179 loc) · 5 KB
/
binarySearch.js
File metadata and controls
206 lines (179 loc) · 5 KB
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
// binarySearch.js
var sleepTime;
var level = 180; // not directly used in this version, can remove if desired.
// Box references
var box1, box2, box3;
var leftNum, midNum, rightNum;
var numItem; // references the array-item elements
var speed; // reference to the speed input
// We'll store the array globally here
let array = [];
/**
* Initialize the page after DOM is ready
*/
$(document).ready(function() {
// Assign references
box1 = $(".box1");
box2 = $(".box2");
box3 = $(".box3");
leftNum = $("#leftNum");
midNum = $("#midNum");
rightNum = $("#rightNum");
speed = $("#speedBtn");
// Toggle concept of binary search
$("#showBtn1").click(function() {
$("#content1").toggle();
});
// Generate initial array
array = generateRandomArray();
displayArray();
// Set initial speed
speedChange();
});
/**
* Called when speed changes
*/
function speedChange() {
// If speed=5, sleepTime = (6-5)*400 = 400ms
// If speed=1, sleepTime = (6-1)*400 = 2000ms, etc.
sleepTime = (6 - speed.val()) * 400;
}
/**
* Highlight line in the code snippet
*/
function codeRunner(code, index) {
// 'code' is a jQuery selection of lines, we highlight the line at `index`
code.css("color", "black");
code.eq(index).css("color", "green");
}
/**
* For a small convenience, we directly pass the lines in the binary search code snippet here
*/
function codeRunnerBinary(index) {
let code = $(".code"); // lines with class="code"
codeRunner(code, index);
}
/**
* Move the highlight box (e.g. box1, box2, box3) to the position of a certain array element
*/
function move(obj, x, y) {
obj.animate({ left: x, top: y });
}
/**
* Return the x offset for item i
*/
function getLeft(items, i) {
return items.eq(i).offset().left - 2;
}
/**
* Return the y offset for item i
*/
function getTop(items, i) {
return items.eq(i).offset().top - 2;
}
/**
* Initialize position of highlight boxes
*/
function initBox() {
move(box1, getLeft(numItem, 0), getTop(numItem, 0));
move(box2, getLeft(numItem, 0), getTop(numItem, 0));
move(box3, getLeft(numItem, 0), getTop(numItem, 0));
}
/**
* A sleep utility so we can "await" to create animation effect
*/
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
/**
* Generate and display a random sorted array
*/
function generateArray() {
array = generateRandomArray();
displayArray();
}
function generateRandomArray() {
let length = Math.floor(Math.random() * 10) + 5; // 5~15
let arr = [];
for(let i = 0; i < length; i++) {
arr.push(Math.floor(Math.random() * 100));
}
arr.sort((a, b) => a - b);
return arr;
}
function displayArray() {
let arrayDiv = document.getElementById("array");
while (arrayDiv.firstChild) {
arrayDiv.removeChild(arrayDiv.firstChild);
}
for (let i = 0; i < array.length; i++) {
let item = document.createElement("div");
item.className = "array-item";
item.innerHTML = array[i];
arrayDiv.appendChild(item);
}
// Re-capture jQuery references
numItem = $(".array-item");
// Re-initialize highlight boxes
initBox();
}
/**
* Perform the Binary Search with animation
*/
async function binarySearch() {
let target = document.getElementById('target').value;
let left = 0;
let right = array.length - 1;
// Move the left (box1) and right (box3) indicators initially
move(box1, getLeft(numItem, left), getTop(numItem, left));
await sleep(sleepTime);
move(box3, getLeft(numItem, right), getTop(numItem, right));
leftNum.html(left);
rightNum.html(right);
while (left <= right) {
// while(left <= right)
codeRunnerBinary(0);
await sleep(sleepTime);
// mid = ...
codeRunnerBinary(1);
await sleep(sleepTime);
let mid = Math.floor((left + right) / 2);
move(box2, getLeft(numItem, mid), getTop(numItem, mid));
await sleep(sleepTime);
midNum.html(mid);
// if(array[mid] == target)
codeRunnerBinary(2);
await sleep(sleepTime);
if (array[mid] == target) {
document.getElementById('result').innerHTML = 'Found at index ' + mid;
codeRunnerBinary(3);
await sleep(sleepTime);
return;
}
else if (array[mid] < target) {
codeRunnerBinary(4);
await sleep(sleepTime);
// left = mid + 1
codeRunnerBinary(5);
await sleep(sleepTime);
left = mid + 1;
move(box1, getLeft(numItem, left), getTop(numItem, left));
await sleep(sleepTime);
leftNum.html(left);
}
else {
// else
codeRunnerBinary(6);
await sleep(sleepTime);
// right = mid - 1
codeRunnerBinary(7);
await sleep(sleepTime);
right = mid - 1;
move(box3, getLeft(numItem, right), getTop(numItem, right));
await sleep(sleepTime);
rightNum.html(right);
}
}
// Not found
document.getElementById('result').innerHTML = 'Not found';
}