@@ -18,9 +18,11 @@ function binarySearchRecursive(array, search, offset = 0) {
1818
1919  if  ( current  ===  search )  { 
2020    return  offset  +  half ; 
21-   }  if  ( array . length  ===  1 )  { 
21+   } 
22+   if  ( array . length  ===  1 )  { 
2223    return  - 1 ; 
23-   }  if  ( search  >  current )  { 
24+   } 
25+   if  ( search  >  current )  { 
2426    const  right  =  array . slice ( half ) ; 
2527    return  binarySearchRecursive ( right ,  search ,  offset  +  half ) ; 
2628  } 
@@ -38,50 +40,24 @@ function binarySearchRecursive(array, search, offset = 0) {
3840 * @param  {string|number } search value to search for 
3941 */ 
4042function  binarySearchIterative ( array ,  search )  { 
41-   // console.log('binarySearchIterative', {array, search}); 
4243  let  start  =  0 ; 
43-   let  end  =  array . length ; 
44-   const  half  =  ( )  =>  parseInt ( ( end  -  start )  /  2 ,  10 )   +   start ; 
44+   let  end  =  array . length   -   1 ; 
45+   const  half  =  ( )  =>  start   +   parseInt ( ( end  -  start )  /  2 ,  10 ) ; 
4546
46-   while  ( end   -   start  >   0 )  { 
47+   while  ( start  <=   end )  { 
4748    const  currentIndex  =  half ( ) ; 
4849    const  current  =  array [ currentIndex ] ; 
4950
50-     if  ( current  ===  search )  { 
51-        return   currentIndex ; 
52-     }   if  ( search  >  current )  { 
53-       start  =  currentIndex ; 
51+     if  ( current  ===  search )  return   currentIndex ; 
52+ 
53+     if  ( search  >  current )  { 
54+       start  =  currentIndex   +   1 ; 
5455    }  else  if  ( search  <  current )  { 
55-       end  =  currentIndex ; 
56+       end  =  currentIndex   -   1 ; 
5657    } 
5758  } 
5859
5960  return  - 1 ; 
6061} 
6162
62- // const binarySearch = binarySearchRecursive; 
63- const  binarySearch  =  binarySearchIterative ; 
64- 
65- // function test() { 
66- //  const directory = ['Adrian', 'Bella', 'Charlotte', 'Daniel', 
67- //  'Emma', 'Hanna', 'Isabella', 'Jayden', 'Kaylee', 'Luke', 'Mia', 
68- //  'Nora', 'Olivia', 'Paisley', 'Riley', 'Thomas', 'Wyatt', 'Xander', 'Zoe']; 
69- // 
70- //   const assert = require('assert'); 
71- //   assert.equal(binarySearch([], 'not found'), -1); 
72- //   assert.equal(binarySearch([1], 2), -1); 
73- //   assert.equal(binarySearch([1], 1), 0); 
74- //   assert.equal(binarySearch([1, 2, 3], 1), 0); 
75- //   assert.equal(binarySearch([1, 2, 3], 2), 1); 
76- //   assert.equal(binarySearch([1, 2, 3], 3), 2); 
77- //   assert.equal(binarySearch([1, 2, 3], 31), -1); 
78- //   assert.equal(binarySearch(directory, 'Adrian'), 0); 
79- //   assert.equal(binarySearch(directory, 'Hanna'), 5); 
80- //   assert.equal(binarySearch(directory, 'Zoe'), 18); 
81- //   assert.equal(binarySearch(directory, 'not found'), -1); 
82- // } 
83- 
84- // test(); 
85- 
86- 
87- module . exports  =  {  binarySearch,  binarySearchIterative,  binarySearchRecursive } ; 
63+ module . exports  =  {  binarySearchIterative,  binarySearchRecursive } ; 
0 commit comments