This material aims to enable students to:
1) Understanding searching algorithm concept
2) Understanding characteristic of binary search
3) Understanding steps of binary search
4) Knowing of advantage and disadvantage of binary search
1. Algorithm and
Data Structure
Andi Nurkholis, S.Kom, M.Kom
Study Program of Informatics
Faculty of Engineering and Computer Science
SY. 2020-2021
May 10, 2021
3. 3
What is Searching?
Searching for data stored in different data structures
is a crucial part of pretty much every single
application.
There are many different algorithms available to
utilize when searching, and each have different
implementations and rely on different data
structures to get the job done.
4. 4
Searching
Algorithm
1) Linear Search
2) Binary Search
Being able to choose a specific algorithm
for a given task is a key skill for
developers and can mean the difference
between a fast, reliable and stable
application and an application that
crumbles from a simple request.
6. 6
Binary Search
Binary Search is a searching algorithm for finding an element's
position in a sorted array.
In this approach, the element is always searched in the middle of a
portion of an array.
Binary search can be implemented only on a sorted array of items.
If the elements are not sorted already, we need to sort them first.
7. Steps of Binary Search
1. Start with the middle element:
a. If the target value is equal to the middle element of
the array, then return the index of the middle
element.
b. If not, then compare the middle element with the
target value,
• If the target value is greater than the number in
the middle index, then pick the elements to the
right of the middle index, and start with Step 1.
• If the target value is less than the number in the
middle index, then pick the elements to the left of
the middle index, and start with Step 1.
7
8. Steps of Binary Search (cont.)
2. When a match is found, return the index of the element
matched.
3. If no match is found, then return -1
8
9. Example
9
data_1 = {1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21}
Find value 15 in array data_1
data_2 = {1, 4, 7, 10, 13, 16, 19, 22, 25, 28, 31}
Find value 4 in array data_2
data_3 = {1, 5, 9, 13, 17, 21, 25, 29, 33, 37}
Find value 33 in array data_3
10. Answer 1
10
Low
1 3 5 7 9 11 13 15 17 19 21
Value searched for 15
Value in middle-index array is 11
Is value 15 = 11?
FALSE
Is value 15 > 11?
TRUE
Then Algorithm is CONTINUE
If value searched greater than
value in middle-index array, then
searching continue to the right
High
Mid
Step 1
11. 11
At each step, the middle-index changes
according to the direction of the search
At next step, low-index [1] changes to the
previous middle-index [11]. That is
because searching continues to the right
What is the meaning? The next high-index
[11] that changes will also cause the next
middle-index [15] to change
12. Answer 1 (cont.)
12
1 3 5 7 9 11 13 15 17 19 21
Step 2
Value searched for 15
Value in middle-index array is 15
Is value 15 = 15?
TRUE
Then Algorithm is STOP
Low High
Mid
14. Answer 2
14
Step 1
1 4 7 10 13 16 19 22 25 28 31
Value searched for 4
Value in middle-index array is 16
Is value 4 = 16?
FALSE
Is value 4 > 11?
FALSE
Then Algorithm is CONTINUE
If value searched less than value in
middle-index array, then searching
continue to the left
Low High
Mid
15. 15
At each step, the middle-index changes
according to the direction of the search
At next step, low-index [31] changes to the
previous middle-index [16]. That is
because searching continues to the left
What is the meaning? The next high-index
[16] that changes will also cause the next
middle-index [7] to change
16. Answer 2 (cont.)
16
1 4 7 10 13 16 19 22 25 28 31
Step 2
Value searched for 4
Value in middle-index array is 7
Is value 4 = 7? FALSE
Is value 4 > 7? FALSE
Then Algorithm is CONTINUE
Low High
Mid
If value searched less than value in
middle-index array, then searching
continue to the left
17. 17
At each step, the middle-index changes
according to the direction of the search
At next step, low-index [16] changes to the
previous middle-index [7]. That is because
searching continues to the left
What is the meaning? The next high-index
[7] that changes will also cause the next
middle-index [4] to change
18. Answer 2 (cont.)
18
1 4 7 10 13 16 19 22 25 28 31
Step 3
Value searched for 4
Value in middle-index array is 13
Is value 4 = 13? FALSE
Is value 4 > 11? FALSE
Then Algorithm is STOP
Low High
Mid
If value searched less than value in
middle-index array, then searching
continue to the left
20. Answer 3
20
Step 1
1 5 9 13 17 21 25 29 33 37
Value searched for 33
Value in middle-index array is 17
Is value 33 = 17? FALSE
Is value 33 > 17? TRUE
Then Algorithm is CONTINUE
If value searched greater than
value in middle-index array, then
searching continue to the right
For an even-number array, done
N/2, then N-1. For this case, 10/2
= 5, then 5-1 = 4. Therefore,
middle-index array is 4-index.
Low High
Mid
21. 21
At each step, the middle-index changes
according to the direction of the search
At next step, low-index [1] changes to the
previous middle-index [17]. That is
because searching continues to the right
What is the meaning? The next high-index
[17] that changes will also cause the next
middle-index [25] to change
22. Answer 3 (cont.)
22
1 5 9 13 17 21 25 29 33 37
Step 2
Value searched for 33
Value in middle-index array is 25
Is value 33 = 25? FALSE
Is value 33 > 25? TRUE
Then Algorithm is CONTINUE
If value searched greater than
value in middle-index array, then
searching continue to the right
Low High
Mid
23. 23
At each step, the middle-index changes
according to the direction of the search
At next step, low-index [17] changes to the
previous middle-index [25]. That is
because searching continues to the right
What is the meaning? The next high-index
[25] that changes will also cause the next
middle-index [29] to change
24. Answer 3 (cont.)
24
1 5 9 13 17 21 25 29 33 37
Step 3
Value searched for 33
Value in middle-index array is 29
Is value 33 = 29? FALSE
Is value 33 > 29? TRUE
Then Algorithm is CONTINUE
If value searched greater than
value in middle-index array, then
searching continue to the right
Low High
Mid
25. 25
At each step, the middle-index changes
according to the direction of the search
At next step, low-index [25] changes to the
previous middle-index [29]. That is
because searching continues to the right
What is the meaning? The next high-index
[29] that changes will also cause the next
middle-index [33] to change
26. Answer 3 (cont.)
26
1 5 9 13 17 21 25 29 33 37
Step 4
Value searched for 33
Value in middle-index array is 33
Is value 33 = 33? TRUE
Then Algorithm is STOP
If value searched greater than
value in middle-index array, then
searching continue to the right
Low High
Mid
28. Advantage of Linear Search
1. It eliminates half of the list from further searching by using the
result of each comparison.
2. For large array of data, it works significantly better than linear
search.
28
29. Disadvantage of Linear Search
1. The algorithm requires the array to be sorted first
2. In implementing, the algorithm more difficult
than linear search
29
30. Thank You, Next …
Sorting
May 10, 2021
Andi Nurkholis, S.Kom, M.Kom
Study Program of Informatics
Faculty of Engineering and Computer Science
SY. 2020-2021