1+ /**
2+ * Given a sorted array of integers,
3+ * return the index of the given key. R
4+ * eturn -1 if the key is not found.
5+ */
6+
7+ // Problem Solving
8+ /**
9+ * binary search
10+ *
11+ * Algo
12+ * Divide and concur (technique)
13+ *
14+ * 1 2 3 4 5
15+ * 2
16+ *
17+ * iterative easy to implement
18+ * O(log n) Runtime complexity
19+ * O(1) Memory complexity
20+ *
21+ * recursive easy to implement
22+ * O(log n) Runtime complexity
23+ * O(log n) Memory complexity
24+ *
25+ *
26+ */
27+
28+ /* Approach 2: Recursive Search
29+ Runtime Complexity: O(log n)
30+ Memory Complexity: O(log n)
31+ */
32+ export function recursiveBinarySearch ( arr , key , low , high ) {
33+ let result = - 1 ;
34+ // let low = 0;
35+ //let high = arr.length - 1;
36+ let mid ;
37+
38+ mid = low + Math . round ( high - low ) ;
39+ if ( arr [ mid ] === key ) {
40+ result = mid ;
41+
42+ return result ;
43+ }
44+ if ( arr [ mid ] < key ) {
45+ low = mid + 1 ;
46+ } else {
47+ high = mid - 1 ;
48+ }
49+
50+ if ( low <= high ) {
51+ result = recursiveBinarySearch ( arr , key , low , high ) ;
52+ }
53+
54+ return result ;
55+ }
56+
57+ // test
58+ console . warn ( 'recursiveBinarySearch' ) ;
59+ log ( recursiveBinarySearch ( [ 1 , 2 , 3 ] , 3 , 0 , 2 ) , 2 ) ;
60+ log ( recursiveBinarySearch ( [ 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 ] , 9 , 0 , 7 ) , 5 ) ;
61+ log (
62+ recursiveBinarySearch ( [ - 10 , - 9 , - 8 , - 7 , - 6 , - 5 , - 4 , 5 , 6 , 7 , 8 ] , - 6 , 0 , 10 ) ,
63+ 4
64+ ) ;
65+ log ( recursiveBinarySearch ( [ 10 , 20 , 30 , 35 , 40 , 50 ] , 30 , 0 , 5 ) , 2 ) ;
66+ log ( recursiveBinarySearch ( [ 10 , 20 , 20 , 20 , 40 , 50 ] , 40 , 0 , 5 ) , 4 ) ;
67+
68+
69+ let arr = [ 1 , 10 , 20 , 47 , 59 , 63 , 75 , 88 , 99 , 107 , 120 , 133 , 155 , 162 , 176 , 188 , 199 , 200 , 210 , 222 ]
70+ let inputs = [ [ 10 , 1 ] , [ 49 , - 1 ] , [ 99 , 8 ] , [ 110 , - 1 ] , [ 176 , 14 ] ]
71+
72+ for ( var i = 0 ; i < inputs . length ; i ++ ) {
73+ log ( recursiveBinarySearch ( arr , inputs [ i ] [ 0 ] , 0 , arr . length - 1 ) , inputs [ i ] [ 1 ] ) ;
74+ }
75+
76+
77+
78+
79+ /* Approach 1: Iterative Search
80+ Runtime Complexity: O(log n)
81+ Memory Complexity: O(1)
82+ */
83+ export function iterativeBinarySearch ( arr , key ) {
84+ let result = - 1 ;
85+ let low = 0 ;
86+ let high = arr . length - 1 ;
87+ let mid ;
88+
89+ // find the mid
90+ while ( low <= high ) {
91+ mid = low + Math . round ( high - low ) ;
92+ if ( arr [ mid ] === key ) {
93+ result = mid ;
94+
95+ return result ;
96+ }
97+ if ( arr [ mid ] < key ) {
98+ low = mid + 1 ;
99+ } else {
100+ high = mid - 1 ;
101+ }
102+ }
103+
104+ return result ;
105+ }
106+
107+ function log ( result , expected ) {
108+ console . log ( '========' ) ;
109+ if ( result === expected ) {
110+ console . info ( '%c PASS' , 'color:black;background-color:lawngreen' ) ;
111+ } else {
112+ console . info ( '%c FAIL' , 'color:white;background-color:red' ) ;
113+ }
114+
115+ console . log ( `result: ${ result } , expected: ${ expected } ` ) ;
116+ }
117+
118+ // test
119+ console . warn ( 'iterativeBinarySearch' ) ;
120+ log ( iterativeBinarySearch ( [ 1 , 2 , 3 ] , 3 ) , 2 ) ;
121+ log ( iterativeBinarySearch ( [ 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 ] , 9 ) , 5 ) ;
122+ log ( iterativeBinarySearch ( [ - 10 , - 9 , - 8 , - 7 , - 6 , - 5 , - 4 , 5 , 6 , 7 , 8 ] , - 6 ) , 4 ) ;
123+ log ( iterativeBinarySearch ( [ 10 , 20 , 30 , 35 , 40 , 50 ] , 30 ) , 2 ) ;
124+ log ( iterativeBinarySearch ( [ 10 , 20 , 20 , 20 , 40 , 50 ] , 40 ) , 4 ) ;
125+ for ( var i = 0 ; i < inputs . length ; i ++ ) {
126+ log ( iterativeBinarySearch ( arr , inputs [ i ] [ 0 ] ) , inputs [ i ] [ 1 ] ) ;
127+ }
0 commit comments