-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinearSearch.js
More file actions
140 lines (120 loc) · 2.81 KB
/
linearSearch.js
File metadata and controls
140 lines (120 loc) · 2.81 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
// linearSearch.js
var sleepTime;
var level = 180; // might remove if not used
var box4;
var numItem2;
var speed;
let array = [];
/**
* Initialize after DOM is ready
*/
$(document).ready(function() {
box4 = $(".box4");
speed = $("#speedBtn");
// Toggle concept of linear search
$("#showBtn2").click(function() {
$("#content2").toggle();
});
// Generate initial array
array = generateRandomArray2();
displayArray2();
// Set initial speed
speedChange();
});
/**
* Called when speed changes
*/
function speedChange() {
sleepTime = (6 - speed.val()) * 400;
}
/**
* Highlight line in the code snippet
*/
function codeRunnerLinear(index) {
let code = $(".code2");
code.css("color", "black");
code.eq(index).css("color", "green");
}
/**
* Move highlight box
*/
function move(obj, x, y) {
obj.animate({ left: x, top: y });
}
/**
* Return left offset
*/
function getLeft(items, i) {
return items.eq(i).offset().left - 2;
}
/**
* Return top offset
*/
function getTop(items, i) {
return items.eq(i).offset().top - 2;
}
/**
* Sleep utility
*/
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
/**
* Generate new array
*/
function generateArray2() {
array = generateRandomArray2();
displayArray2();
}
/**
* Random unsorted array
*/
function generateRandomArray2() {
let length = Math.floor(Math.random() * 10) + 5;
let arr = [];
for (let i = 0; i < length; i++) {
arr.push(Math.floor(Math.random() * 100));
}
return arr;
}
/**
* Display array
*/
function displayArray2() {
let arrayDiv = document.getElementById("array2");
while (arrayDiv.firstChild) {
arrayDiv.removeChild(arrayDiv.firstChild);
}
for (let i = 0; i < array.length; i++) {
let item = document.createElement("div");
item.className = "array-item2";
item.innerHTML = array[i];
arrayDiv.appendChild(item);
}
numItem2 = $(".array-item2");
move(box4, getLeft(numItem2, 0), getTop(numItem2, 0));
}
/**
* Perform linear search with animation
*/
async function linearSearch() {
let target = document.getElementById('target2').value;
for (let i = 0; i < array.length; i++) {
// for(let i=0; i< array.length;i++){
codeRunnerLinear(0);
await sleep(sleepTime);
// Move highlight to index i
move(box4, getLeft(numItem2, i), getTop(numItem2, i));
await sleep(sleepTime);
// if(array[i] == target)
codeRunnerLinear(1);
await sleep(sleepTime);
if (array[i] == target) {
document.getElementById('result2').innerHTML = 'Found at index ' + i;
codeRunnerLinear(2);
await sleep(sleepTime);
return;
}
}
document.getElementById('result2').innerHTML = 'Not found';
}