forked from salman0ansari/Learn-Javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SearchingAlgo.js
41 lines (35 loc) · 851 Bytes
/
SearchingAlgo.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
//Linear Search
let linearSearch = (list,value)=>{
for (let i = 0; i < list.length; i++) {
if (list[i] === value) {
return i;
}
}
return -1;
}
var list = [12, 45, 48, 5, 451, 2,34 ,43,54,66 ]
var value = 2;
linearSearch(list , value) // result should 5
//Binary Search
let BinarySearch = (list,val)=>{
let left = 0;
let right = list.length - 1;
let mid = Math.floor((left + right) / 2);
while (list[mid] !== val && left <= right) {
if (val < list[mid]) {
right = mid - 1
} else {
left = mid + 1
}
mid = Math.floor((left + right) / 2);
}
if (list[mid] === val) {
return mid;
} else {
return -1
}
}
;
let list = [2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30]
let val = 20;
// should return 9