• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Binary search: illustrated step-by-step walk through
 

Binary search: illustrated step-by-step walk through

on

  • 642 views

A step-by-step illustration of Binary Search to help you walk through a series of operations. Illustration is accompanied by actual code with bold line indicating the current operation. ...

A step-by-step illustration of Binary Search to help you walk through a series of operations. Illustration is accompanied by actual code with bold line indicating the current operation.

https://github.com/yoshiwatanabe/Algorithms/blob/master/Finding/BinarySearch.cs

Statistics

Views

Total Views
642
Views on SlideShare
642
Embed Views
0

Actions

Likes
0
Downloads
4
Comments
0

0 Embeds 0

No embeds

Accessibility

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Binary search: illustrated step-by-step walk through Binary search: illustrated step-by-step walk through Presentation Transcript

    • Binary Search Illustrated walk through
    • Iterative binary search 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
    • begin last mid x 4 This is what we search for [0] [1] [2] [3] 2 4 5 6 Let’s look for 4
    • begin 0 x last mid 3 0 [0] [1] [2] [3] 2 4 5 6 int begin = 0; int last = array.Length - 1; int mid = 0; 4
    • begin 0 x 0 <= 3 is true mid last 3 0 [0] [1] [2] [3] 2 4 5 6 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; 4
    • begin 0 x last (0+3)/2 = 1 mid 3 1 [0] [1] [2] [3] 2 4 5 6 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; 4
    • begin 0 x last mid 4 3 1 [0] [1] [2] [3] 2 4 5 6 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; 4<4 is false
    • begin 0 x last mid 4 3 1 [0] [1] [2] [3] 2 4 5 6 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; 4>4 is false
    • begin 0 x last mid 4 3 1 [0] [1] [2] [3] 2 4 5 6 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; } } We found x=4 at index 1! return -1;
    • begin last mid x 5 This is what we search for [0] [1] [2] [3] 2 4 5 6 Let’s look for 5
    • begin 0 x last mid 3 0 [0] [1] [2] [3] 2 4 5 6 int begin = 0; int last = array.Length - 1; int mid = 0; 5
    • begin 0 x 0 <= 3 is true mid last 3 0 [0] [1] [2] [3] 2 4 5 6 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; 5
    • begin 0 x last (0+3)/2 = 1 mid 3 1 [0] [1] [2] [3] 2 4 5 6 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; 5
    • begin 0 x last mid 5 3 1 [0] [1] [2] [3] 2 4 5 6 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; 4<5 is true
    • begin 2 x last mid 3 1 [0] [1] [2] [3] 2 4 5 6 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; 5
    • begin 2 <= 3 is true 2 x last mid 3 1 [0] [1] [2] [3] 2 4 5 6 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; 5
    • begin 2 x last (2+3)/2 =2 mid 3 2 [0] [1] [2] [3] 2 4 5 6 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; 5
    • begin 2 x last mid 3 2 [0] [1] [2] [3] 2 4 5 6 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; 5 5<5 is false
    • begin 2 x last mid 3 2 [0] [1] [2] [3] 2 4 5 6 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; 5 5>5 is false
    • begin 2 x last mid 5 3 2 [0] [1] [2] [3] 2 4 5 6 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; } } We found x=5 at index 2! return -1;
    • begin last mid x 6 This is what we search for [0] [1] [2] [3] 2 4 5 6 Let’s look for 6
    • begin 0 x last mid 3 0 [0] [1] [2] [3] 2 4 5 6 int begin = 0; int last = array.Length - 1; int mid = 0; 6
    • begin 0 x 0 <= 3 is true mid last 3 0 [0] [1] [2] [3] 2 4 5 6 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; 6
    • begin 0 x last (0+3)/2 = 1 mid 3 1 [0] [1] [2] [3] 2 4 5 6 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; 6
    • begin 0 x last mid 6 3 1 [0] [1] [2] [3] 2 4 5 6 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; 4<6 is true
    • begin 2 x last mid 3 1 [0] [1] [2] [3] 2 4 5 6 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; 6
    • begin 2 <= 3 is true 2 x last mid 3 1 [0] [1] [2] [3] 2 4 5 6 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; 6
    • begin 2 x last (2+3)/2 =2 mid 3 2 [0] [1] [2] [3] 2 4 5 6 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; 6
    • begin 2 x last mid 3 2 [0] [1] [2] [3] 2 4 5 6 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; 6 5<6 is true
    • begin last mid 3 3 2 [0] [1] [2] [3] 2 4 5 6 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; x 6
    • begin last 3 <= 3 is true mid 3 3 2 [0] [1] [2] [3] 2 4 5 6 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; x 6
    • begin last 3 mid (3+3)/2 = 3 3 3 [0] [1] [2] [3] 2 4 5 6 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; x 6
    • begin 3 last 3 mid 3 [0] [1] [2] [3] 2 4 5 6 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; x 6 6<6 is false
    • begin 3 last 3 mid 3 [0] [1] [2] [3] 2 4 5 6 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; x 6 6>6 is false
    • begin 3 last 3 mid 3 [0] [1] [2] 4 5 6 [3] 2 x 6 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; } } We found x=6 at index 3! return -1;