We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
2 parents 05f73e1 + 153c648 commit da338f8Copy full SHA for da338f8
Searching/Binary Search/Javascript/BinarySearch.js
@@ -0,0 +1,21 @@
1
+/** Binary search algorithm in Javascript **/
2
+/** Follows the README.md **/
3
+
4
+function binarySearch(array, target) {
5
+ let leftIndex = 0;
6
+ let rightIndex = array.length - 1;
7
+ let middleIndex;
8
9
+ while (leftIndex <= rightIndex) {
10
+ middleIndex = leftIndex + Math.floor((rightIndex - leftIndex) / 2);
11
+ if (array[middleIndex] === target) {
12
+ return middleIndex;
13
+ }
14
+ if (arr[middleIndex] < target) {
15
+ leftIndex = middleIndex + 1;
16
+ } else {
17
+ rightIndex = middleIndex - 1;
18
19
20
+ return -1;
21
+}
Searching/Linear Search/Javascript/LinearSearch.js
@@ -0,0 +1,10 @@
+function linearSearch(array, item) {
+ for (let i = 0; i < array.length; i++) {
+ if(array[i] === item) {
+ return i;
0 commit comments