00:01
All right, so here we are going to be using the binary search algorithm, and that's algorithm 3 in the textbook.
00:08
Let's call it bind search.
00:15
Of course, you can name it whatever makes sense to you.
00:21
And we need, for this algorithm, a list of n integers, and it should be a non -empty list.
00:34
So n ought to be greater than or equal to one.
00:38
Then we also need, in this case, we're not just searching for an element in the list, but we're searching for where to put the element.
00:47
So that's the change that we're making, rather than finding a pre -existing one, we're placing one in, and the element will be placing as x.
00:56
And so this actually starts identical to the binary search algorithm, where we want to set our left endpoint of the search interval i, equal to 1 and then our right end point of our search interval j equal to n so initially we're setting it to the entire list here and then we'll say while i is less than j we're going to find the index of point that is roughly halfway through so we'll take i plus j divide by but then we floor it in case it's not an even number of elements in the list.
01:56
Once we've done that, we want to compare x to this value that's halfway through.
02:03
So let's check and see if x is greater than the value of the mth element of the list.
02:17
And if it is, then we want to reset our search endpoint.
02:23
And we want to reset the left end point to be one further than the mth number...