Download presentation
Presentation is loading. Please wait.
Published byRatna Setiawan Modified over 6 years ago
1
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] 2 6 9 14 18 23 28 36 47 52 68 81 93 99
2
binSearch(52) [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] 2 6 9 14 18 23 28 36 47 52 68 81 93 99
3
binSearch(52) 2 6 9 14 18 23 28 36 47 52 68 81 93 99 TARGET 52 [0] [1]
[2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] 2 6 9 14 18 23 28 36 47 52 68 81 93 99 TARGET 52
4
binSearch(52) 2 6 9 14 18 23 28 36 47 52 68 81 93 99 FIRST >
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] 2 6 9 14 18 23 28 36 47 52 68 81 93 99 FIRST > FIRST = 0 TARGET 52 FIRST
5
binSearch(52) 2 6 9 14 18 23 28 36 47 52 68 81 93 99 FIRST >
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] 2 6 9 14 18 23 28 36 47 52 68 81 93 99 FIRST > LAST > FIRST = 0 LAST = ARRAY.length – 1 TARGET 52 FIRST LAST 13
6
MIDDLE = (FIRST + LAST) / 2 TARGET 52
binSearch(52) [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] 2 6 9 14 18 23 28 36 47 52 68 81 93 99 FIRST > MIDDLE > LAST > FIRST = 0 LAST = ARRAY.length – 1 MIDDLE = (FIRST + LAST) / 2 TARGET 52 FIRST LAST 13 MIDDLE 6
7
MIDDLE = (FIRST + LAST) / 2 FOUND = FALSE TARGET 52
binSearch(52) [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] 2 6 9 14 18 23 28 36 47 52 68 81 93 99 FIRST > MIDDLE > LAST > FIRST = 0 LAST = ARRAY.length – 1 MIDDLE = (FIRST + LAST) / 2 FOUND = FALSE TARGET 52 FIRST LAST 13 MIDDLE 6 FOUND FALSE
8
loop while FIRST <= LAST AND FOUND = FALSE
binSearch(52) [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] 2 6 9 14 18 23 28 36 47 52 68 81 93 99 FIRST > MIDDLE > LAST > loop while FIRST <= LAST AND FOUND = FALSE if ARRAY[MIDDLE] = TARGET then FOUND = TRUE else if ARRAY[MIDDLE] > TARGET then LAST = MIDDLE – 1 else FIRST = MIDDLE + 1 end if MIDDLE = (FIRST + LAST) / 2 end loop TARGET 52 FIRST LAST 13 MIDDLE 6 FOUND FALSE
9
loop while FIRST <= LAST AND FOUND = FALSE
binSearch(52) [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] 2 6 9 14 18 23 28 36 47 52 68 81 93 81 FIRST > MIDDLE > LAST > loop while FIRST <= LAST AND FOUND = FALSE if ARRAY[MIDDLE] = TARGET then FOUND = TRUE else if ARRAY[MIDDLE] > TARGET then LAST = MIDDLE – 1 else FIRST = MIDDLE + 1 end if MIDDLE = (FIRST + LAST) / 2 end loop TARGET 52 FIRST LAST 13 MIDDLE 6 FOUND FALSE
10
loop while FIRST <= LAST AND FOUND = FALSE
binSearch(52) [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] 2 6 9 14 18 23 28 36 47 52 68 81 93 81 MIDDLE > FIRST > LAST > loop while FIRST <= LAST AND FOUND = FALSE if ARRAY[MIDDLE] = TARGET then FOUND = TRUE else if ARRAY[MIDDLE] > TARGET then LAST = MIDDLE – 1 else FIRST = MIDDLE + 1 end if MIDDLE = (FIRST + LAST) / 2 end loop TARGET 52 FIRST 7 LAST 13 MIDDLE 6 FOUND FALSE
11
loop while FIRST <= LAST AND FOUND = FALSE
binSearch(52) [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] 2 6 9 14 18 23 28 36 47 52 68 81 93 81 MIDDLE > FIRST > LAST > loop while FIRST <= LAST AND FOUND = FALSE if ARRAY[MIDDLE] = TARGET then FOUND = TRUE else if ARRAY[MIDDLE] > TARGET then LAST = MIDDLE – 1 else FIRST = MIDDLE + 1 end if MIDDLE = (FIRST + LAST) / 2 end loop TARGET 52 FIRST 7 LAST 13 MIDDLE 6 FOUND FALSE
12
loop while FIRST <= LAST AND FOUND = FALSE
binSearch(52) [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] 2 6 9 14 18 23 28 36 47 52 68 81 93 99 FIRST > MIDDLE > LAST > loop while FIRST <= LAST AND FOUND = FALSE if ARRAY[MIDDLE] = TARGET then FOUND = TRUE else if ARRAY[MIDDLE] > TARGET then LAST = MIDDLE – 1 else FIRST = MIDDLE + 1 end if MIDDLE = (FIRST + LAST) / 2 end loop TARGET 52 FIRST 7 LAST 13 MIDDLE 10 FOUND FALSE
13
loop while FIRST <= LAST AND FOUND = FALSE
binSearch(52) [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] 2 6 9 14 18 23 28 36 47 52 68 81 93 99 FIRST > MIDDLE > LAST > loop while FIRST <= LAST AND FOUND = FALSE if ARRAY[MIDDLE] = TARGET then FOUND = TRUE else if ARRAY[MIDDLE] > TARGET then LAST = MIDDLE – 1 else FIRST = MIDDLE + 1 end if MIDDLE = (FIRST + LAST) / 2 end loop TARGET 52 FIRST 7 LAST 13 MIDDLE 10 FOUND FALSE
14
loop while FIRST <= LAST AND FOUND = FALSE
binSearch(52) [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] 2 6 9 14 18 23 28 36 47 52 68 81 93 99 FIRST > MIDDLE > LAST > loop while FIRST <= LAST AND FOUND = FALSE if ARRAY[MIDDLE] = TARGET then FOUND = TRUE else if ARRAY[MIDDLE] > TARGET then LAST = MIDDLE – 1 else FIRST = MIDDLE + 1 end if MIDDLE = (FIRST + LAST) / 2 end loop TARGET 52 FIRST 7 LAST 13 MIDDLE 10 FOUND FALSE
15
loop while FIRST <= LAST AND FOUND = FALSE
binSearch(52) [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] 2 6 9 14 18 23 28 36 47 52 68 81 93 99 FIRST > LAST > MIDDLE > loop while FIRST <= LAST AND FOUND = FALSE if ARRAY[MIDDLE] = TARGET then FOUND = TRUE else if ARRAY[MIDDLE] > TARGET then LAST = MIDDLE – 1 else FIRST = MIDDLE + 1 end if MIDDLE = (FIRST + LAST) / 2 end loop TARGET 52 FIRST 7 LAST 9 MIDDLE 10 FOUND FALSE
16
loop while FIRST <= LAST AND FOUND = FALSE
binSearch(52) [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] 2 6 9 14 18 23 28 36 47 52 68 81 93 99 FIRST > LAST > MIDDLE > loop while FIRST <= LAST AND FOUND = FALSE if ARRAY[MIDDLE] = TARGET then FOUND = TRUE else if ARRAY[MIDDLE] > TARGET then LAST = MIDDLE – 1 else FIRST = MIDDLE + 1 end if MIDDLE = (FIRST + LAST) / 2 end loop TARGET 52 FIRST 7 LAST 9 MIDDLE 10 FOUND FALSE
17
loop while FIRST <= LAST AND FOUND = FALSE
binSearch(52) [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] 2 6 9 14 18 23 28 36 47 52 68 81 93 99 FIRST > MIDDLE > LAST > loop while FIRST <= LAST AND FOUND = FALSE if ARRAY[MIDDLE] = TARGET then FOUND = TRUE else if ARRAY[MIDDLE] > TARGET then LAST = MIDDLE – 1 else FIRST = MIDDLE + 1 end if MIDDLE = (FIRST + LAST) / 2 end loop TARGET 52 FIRST 7 LAST 9 MIDDLE 8 FOUND FALSE
18
loop while FIRST <= LAST AND FOUND = FALSE
binSearch(52) [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] 2 6 9 14 18 23 28 36 47 52 68 81 68 81 FIRST > MIDDLE > LAST > loop while FIRST <= LAST AND FOUND = FALSE if ARRAY[MIDDLE] = TARGET then FOUND = TRUE else if ARRAY[MIDDLE] > TARGET then LAST = MIDDLE – 1 else FIRST = MIDDLE + 1 end if MIDDLE = (FIRST + LAST) / 2 end loop TARGET 52 FIRST 7 LAST 9 MIDDLE 8 FOUND FALSE
19
loop while FIRST <= LAST AND FOUND = FALSE
binSearch(52) [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] 2 6 9 14 18 23 28 36 47 52 68 81 68 81 FIRST > MIDDLE > LAST > loop while FIRST <= LAST AND FOUND = FALSE if ARRAY[MIDDLE] = TARGET then FOUND = TRUE else if ARRAY[MIDDLE] > TARGET then LAST = MIDDLE – 1 else FIRST = MIDDLE + 1 end if MIDDLE = (FIRST + LAST) / 2 end loop TARGET 52 FIRST 7 LAST 9 MIDDLE 8 FOUND FALSE
20
loop while FIRST <= LAST AND FOUND = FALSE
binSearch(52) [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] 2 6 9 14 18 23 28 36 47 52 68 81 68 81 MIDDLE > FIRST > LAST > loop while FIRST <= LAST AND FOUND = FALSE if ARRAY[MIDDLE] = TARGET then FOUND = TRUE else if ARRAY[MIDDLE] > TARGET then LAST = MIDDLE – 1 else FIRST = MIDDLE + 1 end if MIDDLE = (FIRST + LAST) / 2 end loop TARGET 52 FIRST 9 LAST MIDDLE 8 FOUND FALSE
21
loop while FIRST <= LAST AND FOUND = FALSE
binSearch(52) [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] 2 6 9 14 18 23 28 36 47 52 68 81 68 81 MIDDLE > FIRST > LAST > loop while FIRST <= LAST AND FOUND = FALSE if ARRAY[MIDDLE] = TARGET then FOUND = TRUE else if ARRAY[MIDDLE] > TARGET then LAST = MIDDLE – 1 else FIRST = MIDDLE + 1 end if MIDDLE = (FIRST + LAST) / 2 end loop TARGET 52 FIRST 9 LAST MIDDLE 8 FOUND FALSE
22
loop while FIRST <= LAST AND FOUND = FALSE
binSearch(52) [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] 2 6 9 14 18 23 28 36 47 52 68 81 68 81 FIRST > MIDDLE > LAST > loop while FIRST <= LAST AND FOUND = FALSE if ARRAY[MIDDLE] = TARGET then FOUND = TRUE else if ARRAY[MIDDLE] > TARGET then LAST = MIDDLE – 1 else FIRST = MIDDLE + 1 end if MIDDLE = (FIRST + LAST) / 2 end loop TARGET 52 FIRST 9 LAST MIDDLE FOUND FALSE
23
loop while FIRST <= LAST AND FOUND = FALSE
binSearch(52) [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] 2 6 9 14 18 23 28 36 47 52 68 81 68 81 FIRST > MIDDLE > LAST > loop while FIRST <= LAST AND FOUND = FALSE if ARRAY[MIDDLE] = TARGET then FOUND = TRUE else if ARRAY[MIDDLE] > TARGET then LAST = MIDDLE – 1 else FIRST = MIDDLE + 1 end if MIDDLE = (FIRST + LAST) / 2 end loop TARGET 52 FIRST 9 LAST MIDDLE FOUND FALSE
24
loop while FIRST <= LAST AND FOUND = FALSE
binSearch(52) [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] 2 6 9 14 18 23 28 36 47 52 68 81 68 81 FIRST > MIDDLE > LAST > loop while FIRST <= LAST AND FOUND = FALSE if ARRAY[MIDDLE] = TARGET then FOUND = TRUE else if ARRAY[MIDDLE] > TARGET then LAST = MIDDLE – 1 else FIRST = MIDDLE + 1 end if MIDDLE = (FIRST + LAST) / 2 end loop TARGET 52 FIRST 9 LAST MIDDLE FOUND FALSE
25
loop while FIRST <= LAST AND FOUND = FALSE
binSearch(52) [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] 2 6 9 14 18 23 28 36 47 52 68 81 68 81 FIRST > MIDDLE > LAST > loop while FIRST <= LAST AND FOUND = FALSE if ARRAY[MIDDLE] = TARGET then FOUND = TRUE else if ARRAY[MIDDLE] > TARGET then LAST = MIDDLE – 1 else FIRST = MIDDLE + 1 end if MIDDLE = (FIRST + LAST) / 2 end loop TARGET 52 FIRST 9 LAST MIDDLE FOUND TRUE
26
binSearch(52) 2 6 9 14 18 23 28 36 47 52 68 81 68 81 FIRST >
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] 2 6 9 14 18 23 28 36 47 52 68 81 68 81 FIRST > MIDDLE > LAST > if FOUND = TRUE return MIDDLE else return -1 end if TARGET 52 FIRST 9 LAST MIDDLE FOUND TRUE
27
binSearch(52) 2 6 9 14 18 23 28 36 47 52 68 81 68 81 FIRST >
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] 2 6 9 14 18 23 28 36 47 52 68 81 68 81 FIRST > MIDDLE > LAST > if FOUND = TRUE return MIDDLE else return -1 end if TARGET 52 FIRST 9 LAST MIDDLE FOUND TRUE
28
binSearch(52) 2 6 9 14 18 23 28 36 47 52 68 81 68 81 FIRST >
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] 2 6 9 14 18 23 28 36 47 52 68 81 68 81 FIRST > MIDDLE > LAST > binSearch(52) = 9 TARGET 52 FIRST 9 LAST MIDDLE FOUND TRUE
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.