0

I have a problem I have an array that's being used in a binary search, I am looking for a number in the array and every time the search comes false I'm suppose to make a new array out of that search either looking in the bottom half or the upper half using a while loop.

This is how far I've gotten with how my professor wanted it but every binary search I've looked at is used differently than the way I'm trying to do it not sure if its possible to do this way or if I messed up something.

Just looking for the next step so I can try to proceed and finish

// Linear Search

var set = [45, 12, 87, 102, 97, 24, 11, 1, 23, 59, 87];
var number = 87;
for(var i = 0; i < set.length; i++){
    var current = set[i];
    if (current == number) {
       console.log(i);
       break;
    }
}


// Binary search
var set = [1, 2, 6, 9, 15, 35, 44, 55, 67, 78, 85];
var number = 78;
for(var i = set.length / 2; i< set.length;){
    var current = set[i];
    var found = false;
    while (!found) {

    }
}
Govind Samrow
  • 9,981
  • 13
  • 53
  • 90
David
  • 157
  • 1
  • 2
  • 11
  • I have to use a while loop not a if/else loop – David May 24 '17 at 09:30
  • I don't know what are you try to do... If you do not wat to use recursive way, it will be very comlicated. 1) you have to check the middle index. But in your code there is only one time for it (I guess `var current = set[i]` right?) 2) next, if make binary search you have to check bottom and upper but.. also it is not exist in your code and it will be very difficult to make it in `for` loop... – wallah May 24 '17 at 09:33
  • Also the binary search is already in order (least to greatest) and from what I was told I am suppose to go to the middle of the array which is 35. Since 35 is less than 78 I have to set a new array starting at 44 to 85 and keep repeating until I find the number 78. – David May 24 '17 at 09:34
  • Mistalis while the question is a bit different than mine I was able to find a comment in there that kind of helped what I'm looking to do while it isn't complete I think I can finish it from there. – David May 24 '17 at 09:45

0 Answers0