Step-by-Step Guide to the Interior Design Process for an Illustrated Novel
Binary search: illustrated step-by-step walk through
-
Upload
yoshi-watanabe -
Category
Technology
-
view
806 -
download
0
description
Transcript of Binary search: illustrated step-by-step walk through
![Page 1: Binary search: illustrated step-by-step walk through](https://reader034.fdocuments.net/reader034/viewer/2022051514/548d91d1b479590d2b8b4a9f/html5/thumbnails/1.jpg)
Binary SearchIllustrated walk through
![Page 2: Binary search: illustrated step-by-step walk through](https://reader034.fdocuments.net/reader034/viewer/2022051514/548d91d1b479590d2b8b4a9f/html5/thumbnails/2.jpg)
int begin = 0;int last = array.Length - 1; int mid = 0;
while (begin <= last) { mid = (begin + last) / 2; if (array[mid] < x) { begin = mid + 1; } else if (array[mid] > x) { last = mid - 1; } else { return mid; }}
return -1;
Part #1 Initialize pointers
Part #2 Search
Iterative binary search
![Page 3: Binary search: illustrated step-by-step walk through](https://reader034.fdocuments.net/reader034/viewer/2022051514/548d91d1b479590d2b8b4a9f/html5/thumbnails/3.jpg)
2 4 5 6
[0] [1] [2] [3]
begin last mid4x
This is what we search for
Let’s look for 4
![Page 4: Binary search: illustrated step-by-step walk through](https://reader034.fdocuments.net/reader034/viewer/2022051514/548d91d1b479590d2b8b4a9f/html5/thumbnails/4.jpg)
2 4 5 6
[0] [1] [2] [3]
int begin = 0;int last = array.Length - 1; int mid = 0;
begin
last
mid
4x0
0
3
![Page 5: Binary search: illustrated step-by-step walk through](https://reader034.fdocuments.net/reader034/viewer/2022051514/548d91d1b479590d2b8b4a9f/html5/thumbnails/5.jpg)
2 4 5 6
[0] [1] [2] [3]
0begin
last
mid
while (begin <= last) { mid = (begin + last) / 2; if (array[mid] < x) { begin = mid + 1; } else if (array[mid] > x) { last = mid - 1; } else { return mid; }}
return -1;
4x0 <= 3is true
0
3
![Page 6: Binary search: illustrated step-by-step walk through](https://reader034.fdocuments.net/reader034/viewer/2022051514/548d91d1b479590d2b8b4a9f/html5/thumbnails/6.jpg)
2 4 5 6
[0] [1] [2] [3]
begin
last
mid
while (begin <= last) { mid = (begin + last) / 2; if (array[mid] < x) { begin = mid + 1; } else if (array[mid] > x) { last = mid - 1; } else { return mid; }}
return -1;
4x
(0+3)/2 = 1
0
1
3
![Page 7: Binary search: illustrated step-by-step walk through](https://reader034.fdocuments.net/reader034/viewer/2022051514/548d91d1b479590d2b8b4a9f/html5/thumbnails/7.jpg)
2 4 5 6
[0] [1] [2] [3]
begin
last
mid
while (begin <= last) { mid = (begin + last) / 2; if (array[mid] < x) { begin = mid + 1; } else if (array[mid] > x) { last = mid - 1; } else { return mid; }}
return -1;
4x
4 < 4is false
0
1
3
![Page 8: Binary search: illustrated step-by-step walk through](https://reader034.fdocuments.net/reader034/viewer/2022051514/548d91d1b479590d2b8b4a9f/html5/thumbnails/8.jpg)
2 4 5 6
[0] [1] [2] [3]
begin
last
mid
while (begin <= last) { mid = (begin + last) / 2; if (array[mid] < x) { begin = mid + 1; } else if (array[mid] > x) { last = mid - 1; } else { return mid; }}
return -1;
4x
4 > 4is false
0
1
3
![Page 9: Binary search: illustrated step-by-step walk through](https://reader034.fdocuments.net/reader034/viewer/2022051514/548d91d1b479590d2b8b4a9f/html5/thumbnails/9.jpg)
2 4 5 6
[0] [1] [2] [3]
begin
last
mid
while (begin <= last) { mid = (begin + last) / 2; if (array[mid] < x) { begin = mid + 1; } else if (array[mid] > x) { last = mid - 1; } else { return mid; }}
return -1;
4x0
1
3
We found x=4 at index 1!
![Page 10: Binary search: illustrated step-by-step walk through](https://reader034.fdocuments.net/reader034/viewer/2022051514/548d91d1b479590d2b8b4a9f/html5/thumbnails/10.jpg)
2 4 5 6
[0] [1] [2] [3]
begin last mid5x
This is what we search for
Let’s look for 5
![Page 11: Binary search: illustrated step-by-step walk through](https://reader034.fdocuments.net/reader034/viewer/2022051514/548d91d1b479590d2b8b4a9f/html5/thumbnails/11.jpg)
2 4 5 6
[0] [1] [2] [3]
int begin = 0;int last = array.Length - 1; int mid = 0;
begin
last
mid
5x0
0
3
![Page 12: Binary search: illustrated step-by-step walk through](https://reader034.fdocuments.net/reader034/viewer/2022051514/548d91d1b479590d2b8b4a9f/html5/thumbnails/12.jpg)
2 4 5 6
[0] [1] [2] [3]
0begin
last
mid
while (begin <= last) { mid = (begin + last) / 2; if (array[mid] < x) { begin = mid + 1; } else if (array[mid] > x) { last = mid - 1; } else { return mid; }}
return -1;
5x0 <= 3is true
0
3
![Page 13: Binary search: illustrated step-by-step walk through](https://reader034.fdocuments.net/reader034/viewer/2022051514/548d91d1b479590d2b8b4a9f/html5/thumbnails/13.jpg)
2 4 5 6
[0] [1] [2] [3]
begin
last
mid
while (begin <= last) { mid = (begin + last) / 2; if (array[mid] < x) { begin = mid + 1; } else if (array[mid] > x) { last = mid - 1; } else { return mid; }}
return -1;
5x
(0+3)/2 = 1
0
1
3
![Page 14: Binary search: illustrated step-by-step walk through](https://reader034.fdocuments.net/reader034/viewer/2022051514/548d91d1b479590d2b8b4a9f/html5/thumbnails/14.jpg)
2 4 5 6
[0] [1] [2] [3]
begin
last
mid
while (begin <= last) { mid = (begin + last) / 2; if (array[mid] < x) { begin = mid + 1; } else if (array[mid] > x) { last = mid - 1; } else { return mid; }}
return -1;
5x
4 < 5is true
0
1
3
![Page 15: Binary search: illustrated step-by-step walk through](https://reader034.fdocuments.net/reader034/viewer/2022051514/548d91d1b479590d2b8b4a9f/html5/thumbnails/15.jpg)
2 4 5 6
[0] [1] [2] [3]
begin
last
mid
while (begin <= last) { mid = (begin + last) / 2; if (array[mid] < x) { begin = mid + 1; } else if (array[mid] > x) { last = mid - 1; } else { return mid; }}
return -1;
5x2
1
3
![Page 16: Binary search: illustrated step-by-step walk through](https://reader034.fdocuments.net/reader034/viewer/2022051514/548d91d1b479590d2b8b4a9f/html5/thumbnails/16.jpg)
2 4 5 6
[0] [1] [2] [3]
begin
last
mid
while (begin <= last) { mid = (begin + last) / 2; if (array[mid] < x) { begin = mid + 1; } else if (array[mid] > x) { last = mid - 1; } else { return mid; }}
return -1;
5x2
1
32 <= 3is true
![Page 17: Binary search: illustrated step-by-step walk through](https://reader034.fdocuments.net/reader034/viewer/2022051514/548d91d1b479590d2b8b4a9f/html5/thumbnails/17.jpg)
2 4 5 6
[0] [1] [2] [3]
begin
last
mid
while (begin <= last) { mid = (begin + last) / 2; if (array[mid] < x) { begin = mid + 1; } else if (array[mid] > x) { last = mid - 1; } else { return mid; }}
return -1;
5x2
2
3
(2+3)/2 =2
![Page 18: Binary search: illustrated step-by-step walk through](https://reader034.fdocuments.net/reader034/viewer/2022051514/548d91d1b479590d2b8b4a9f/html5/thumbnails/18.jpg)
2 4 5 6
[0] [1] [2] [3]
begin
last
mid
while (begin <= last) { mid = (begin + last) / 2; if (array[mid] < x) { begin = mid + 1; } else if (array[mid] > x) { last = mid - 1; } else { return mid; }}
return -1;
5x2
2
3
5 < 5is false
![Page 19: Binary search: illustrated step-by-step walk through](https://reader034.fdocuments.net/reader034/viewer/2022051514/548d91d1b479590d2b8b4a9f/html5/thumbnails/19.jpg)
2 4 5 6
[0] [1] [2] [3]
begin
last
mid
while (begin <= last) { mid = (begin + last) / 2; if (array[mid] < x) { begin = mid + 1; } else if (array[mid] > x) { last = mid - 1; } else { return mid; }}
return -1;
5x2
2
3
5 > 5is false
![Page 20: Binary search: illustrated step-by-step walk through](https://reader034.fdocuments.net/reader034/viewer/2022051514/548d91d1b479590d2b8b4a9f/html5/thumbnails/20.jpg)
2 4 5 6
[0] [1] [2] [3]
while (begin <= last) { mid = (begin + last) / 2; if (array[mid] < x) { begin = mid + 1; } else if (array[mid] > x) { last = mid - 1; } else { return mid; }}
return -1;
5xbegin
last
mid
2
2
3
We found x=5 at index 2!
![Page 21: Binary search: illustrated step-by-step walk through](https://reader034.fdocuments.net/reader034/viewer/2022051514/548d91d1b479590d2b8b4a9f/html5/thumbnails/21.jpg)
2 4 5 6
[0] [1] [2] [3]
begin last mid6x
This is what we search for
Let’s look for 6
![Page 22: Binary search: illustrated step-by-step walk through](https://reader034.fdocuments.net/reader034/viewer/2022051514/548d91d1b479590d2b8b4a9f/html5/thumbnails/22.jpg)
2 4 5 6
[0] [1] [2] [3]
int begin = 0;int last = array.Length - 1; int mid = 0;
begin
last
mid
6x0
0
3
![Page 23: Binary search: illustrated step-by-step walk through](https://reader034.fdocuments.net/reader034/viewer/2022051514/548d91d1b479590d2b8b4a9f/html5/thumbnails/23.jpg)
2 4 5 6
[0] [1] [2] [3]
0begin
last
mid
while (begin <= last) { mid = (begin + last) / 2; if (array[mid] < x) { begin = mid + 1; } else if (array[mid] > x) { last = mid - 1; } else { return mid; }}
return -1;
6x0 <= 3is true
0
3
![Page 24: Binary search: illustrated step-by-step walk through](https://reader034.fdocuments.net/reader034/viewer/2022051514/548d91d1b479590d2b8b4a9f/html5/thumbnails/24.jpg)
2 4 5 6
[0] [1] [2] [3]
begin
last
mid
while (begin <= last) { mid = (begin + last) / 2; if (array[mid] < x) { begin = mid + 1; } else if (array[mid] > x) { last = mid - 1; } else { return mid; }}
return -1;
6x
(0+3)/2 = 1
0
1
3
![Page 25: Binary search: illustrated step-by-step walk through](https://reader034.fdocuments.net/reader034/viewer/2022051514/548d91d1b479590d2b8b4a9f/html5/thumbnails/25.jpg)
2 4 5 6
[0] [1] [2] [3]
begin
last
mid
while (begin <= last) { mid = (begin + last) / 2; if (array[mid] < x) { begin = mid + 1; } else if (array[mid] > x) { last = mid - 1; } else { return mid; }}
return -1;
6x
4 < 6is true
0
1
3
![Page 26: Binary search: illustrated step-by-step walk through](https://reader034.fdocuments.net/reader034/viewer/2022051514/548d91d1b479590d2b8b4a9f/html5/thumbnails/26.jpg)
2 4 5 6
[0] [1] [2] [3]
begin
last
mid
while (begin <= last) { mid = (begin + last) / 2; if (array[mid] < x) { begin = mid + 1; } else if (array[mid] > x) { last = mid - 1; } else { return mid; }}
return -1;
6x2
1
3
![Page 27: Binary search: illustrated step-by-step walk through](https://reader034.fdocuments.net/reader034/viewer/2022051514/548d91d1b479590d2b8b4a9f/html5/thumbnails/27.jpg)
2 4 5 6
[0] [1] [2] [3]
begin
last
mid
while (begin <= last) { mid = (begin + last) / 2; if (array[mid] < x) { begin = mid + 1; } else if (array[mid] > x) { last = mid - 1; } else { return mid; }}
return -1;
6x2
1
32 <= 3is true
![Page 28: Binary search: illustrated step-by-step walk through](https://reader034.fdocuments.net/reader034/viewer/2022051514/548d91d1b479590d2b8b4a9f/html5/thumbnails/28.jpg)
2 4 5 6
[0] [1] [2] [3]
begin
last
mid
while (begin <= last) { mid = (begin + last) / 2; if (array[mid] < x) { begin = mid + 1; } else if (array[mid] > x) { last = mid - 1; } else { return mid; }}
return -1;
6x2
2
3
(2+3)/2 =2
![Page 29: Binary search: illustrated step-by-step walk through](https://reader034.fdocuments.net/reader034/viewer/2022051514/548d91d1b479590d2b8b4a9f/html5/thumbnails/29.jpg)
2 4 5 6
[0] [1] [2] [3]
begin
last
mid
while (begin <= last) { mid = (begin + last) / 2; if (array[mid] < x) { begin = mid + 1; } else if (array[mid] > x) { last = mid - 1; } else { return mid; }}
return -1;
6x2
2
3
5 < 6is true
![Page 30: Binary search: illustrated step-by-step walk through](https://reader034.fdocuments.net/reader034/viewer/2022051514/548d91d1b479590d2b8b4a9f/html5/thumbnails/30.jpg)
2 4 5 6
[0] [1] [2] [3]
begin
last
mid
while (begin <= last) { mid = (begin + last) / 2; if (array[mid] < x) { begin = mid + 1; } else if (array[mid] > x) { last = mid - 1; } else { return mid; }}
return -1;
6x3
2
3
![Page 31: Binary search: illustrated step-by-step walk through](https://reader034.fdocuments.net/reader034/viewer/2022051514/548d91d1b479590d2b8b4a9f/html5/thumbnails/31.jpg)
2 4 5 6
[0] [1] [2] [3]
begin
last
mid
while (begin <= last) { mid = (begin + last) / 2; if (array[mid] < x) { begin = mid + 1; } else if (array[mid] > x) { last = mid - 1; } else { return mid; }}
return -1;
6x3
2
33 <= 3is true
![Page 32: Binary search: illustrated step-by-step walk through](https://reader034.fdocuments.net/reader034/viewer/2022051514/548d91d1b479590d2b8b4a9f/html5/thumbnails/32.jpg)
2 4 5 6
[0] [1] [2] [3]
begin
last
mid
while (begin <= last) { mid = (begin + last) / 2; if (array[mid] < x) { begin = mid + 1; } else if (array[mid] > x) { last = mid - 1; } else { return mid; }}
return -1;
6x3
3
3
(3+3)/2 = 3
![Page 33: Binary search: illustrated step-by-step walk through](https://reader034.fdocuments.net/reader034/viewer/2022051514/548d91d1b479590d2b8b4a9f/html5/thumbnails/33.jpg)
2 4 5 6
[0] [1] [2] [3]
begin
last
mid
while (begin <= last) { mid = (begin + last) / 2; if (array[mid] < x) { begin = mid + 1; } else if (array[mid] > x) { last = mid - 1; } else { return mid; }}
return -1;
6x3
3
3
6 < 6is false
![Page 34: Binary search: illustrated step-by-step walk through](https://reader034.fdocuments.net/reader034/viewer/2022051514/548d91d1b479590d2b8b4a9f/html5/thumbnails/34.jpg)
2 4 5 6
[0] [1] [2] [3]
begin
last
mid
while (begin <= last) { mid = (begin + last) / 2; if (array[mid] < x) { begin = mid + 1; } else if (array[mid] > x) { last = mid - 1; } else { return mid; }}
return -1;
6x3
3
3
6 > 6is false
![Page 35: Binary search: illustrated step-by-step walk through](https://reader034.fdocuments.net/reader034/viewer/2022051514/548d91d1b479590d2b8b4a9f/html5/thumbnails/35.jpg)
2 4 5 6
[0] [1] [2] [3]
while (begin <= last) { mid = (begin + last) / 2; if (array[mid] < x) { begin = mid + 1; } else if (array[mid] > x) { last = mid - 1; } else { return mid; }}
return -1;
6x
We found x=6 at index 3!
begin
last
mid
3
3
3