SlideShare a Scribd company logo
Searching Techniques:
Binary Search
SCJ2013 Data Structure & Algorithms
Nor Bahiah Hj Ahmad & Dayang
Norhayati A. Jawawi
Objectives
At the end of the class, students are expected to be able
to do the following:
• Understand the searching technique concept and the
purpose of searching operation.
• Understand the implementation of basic searching
algorithm;
1. Sequential search.
• Sequential search on unsorted data.
• Sequential search on sorted data.
2. Binary Search.
• Able to analyze the efficiency of the searching
technique.
• Able to implement searching technique in problem
solving.
3.0 Binary Search
Binary Search
• The drawback of sequential search
algorithm is having to traverse the entire
list, O(n)
• Sorting the list does minimize the cost of
traversing the whole data set, but we
can improve the searching efficiency by
using the Binary Search algorithm
4
Binary Search
• Consider a list in ascending sorted order.
For a sequential search, searching is from
the beginning until an item is found or
the end is reached.
• Binary search improve the algorithm by
removing as much of the data set as
possible so that the item is found more
quickly.
• Search process is started at the middle
of the list, then the algorithm determine
which half the item is in (because the list
is sorted).
– It divides the working range in half with a
single test. By repeating the procedure, the
result is an efficient search algorithm-O(log2
n). 5
• starts by comparing the search key
with the element at the middle
– If the value matches, it will be return to the
calling function (index = MIDDLE)
– If the search key < the middle element,
search will be focused on the elements
between the first element to the element
before the middle element (MIDDLE -1)
– If the search key is not found, the element
at the middle of the first element and the
MIDDLE –1 element will be compared
with the search key.
– If the search key > the middle element,
search will only be focused on the
elements between the second MIDDLE
element to the first MIDDLE element.
– Search is repeated until the searched key is
found or the last element in the subset is
traversed (LEFT>RIGHT ).
6
Binary Search
array
5522 443311
[0] [1] [2] [3] [4] [5] [6]
7766
5522 443311
[0] [1] [2] [3] [4] [5] [6]
7766
MIDDLE
[0] [1] [2]
22 3311
MIDDLE
[2]
33
MIDDLE
LEFT>RIGHT
35search_key
Binary Search Function
7
int binary_search( int search_key, int array_size,
const int array [] )
{ bool found = false;
int index = -1 //-1 means record not found
int MIDDLE,
LEFT = 0,
RIGHT = array_size-1;
while (( LEFT<= RIGHT ) && (!found))
{ MIDDLE = (LEFT + RIGHT )/ 2; // Get middle index
if ( array[MIDDLE] == search_key)
{ index = MIDDLE;
found = true;
}
else if (array[MIDDLE] > search_key)
RIGHT= MIDDLE– 1; // search is focused
// on the left side of list
else
LEFT= MIDDLE+ 1 // search is focused
// on the right side of the list
} //end while
return index;
}//end function
Binary Search on a Sorted List
• Search starts by obtaining the MIDDLE index
of the array
• assume:
– search_key = 35
• MIDDLE= ( 0 + 6 ) / 2
= 3 { First MIDDLE index}
8
35
11 22 33 44 55 66 77
search_key
array
• search_key 35 is compared with the element at the
fourth index in the array, which is array[3] with the value
44.
– search_key < MIDDLE value, therefore search will be
focused on the elements between the first index and
the third index only (index 1 to MIDDLE-1)
• Process to obtain MIDDLE index is repeated
MIDDLE = ( 0 + 2 ) / 2
= 1 { second MIDDLE index}
Search_key 35 is compared with the element at the second
index, which is array[1] with the value 22
– search_key > MIDDLE value, therefore search will be
focused only on the elements between the second
MIDDLE index to the first MIDDLE index.
MIDDLE = (2 + 2 ) / 2
= 2 { third MIDDLE index}
9
Binary Search on Sorted List
• Element at the third index, array[2] with the
value 33 is not equal to the value of the search
key.
• Search process has reached the last element of
the traversed subset, therefore search is
terminated and assumed fail.
• To search from the list sorted descending,
change operator “ > ” to operator “ < “ to the
following statement :
else if (array [ MIDDLE ] >
search_key)
RIGHT = MIDDLE – 1;
10
Binary Search on Sorted List
Step 1
Steps to Execute Binary Search
11
false
12 14 16 18 20 22 24 26 28 30
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]
LEFT MIDDLE RIGHT
LEFT
MIDDLE
RIGHT
4
9
0
-1
22
found
index
search_key
array
Assume: search_key = 22, array_size = 10
Step 2
12
12 14 16 18 20 22 24 26 28 30
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]
MIDDLE RIGHT
7
9
5
-1
22
falsefound
index
search_key
array
LEFT
LEFT
MIDDLE
RIGHT
Steps to Execute Binary Search
Step 3
13
12 14 16 18 20 22 24 26 28 30
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]
LEFT
MIDDLE
RIGHT
LEFT
MIDDLE
RIGHT
5
6
5
5
22
truestatus
index
search_key
array
Steps to Execute Binary Search
Binary Search Analysis
• Binary Search starts searching by comparing
element in the middle. Thus the searching
process start at n/2 for a list size = n.
• If the middle value does not matches with the
search key, then the searching area will be
reduced to the left or right sublist only. This will
reduce the searching area to ½ n.
• From half of the list, the second middle value will
be identified. Again, if the middle value does not
matches with the search key, the searching area
will be reduced to the left or right sub list only.
The searching area will reduce ½ ( ½ n).
14
Binary Search Analysis
• The process of looking for middle point and
reducing the searching area to the left or right
sublist will be repeated until the middle value is
equal to the middle value (search key is found)
or the last value in sublist has been traverse.
• If the repetition occur k times, then at iteration
k , the searching area is reduced to ( ½ )kn.
15
Binary Search Analysis
• figure above shows the reducing size for
binary search area.
• At iteration k for array size = n , searching
area will be reduced from n to ( ½ )kn.
16
k
n
n
(1/2) k
n
Binary Search Analysis
• Best case for binary search happen if the
search key is found in the middle array.
O(1).
• Worse case for binary search happen if the
search key is not in the list or the searching
size equal to 1.
• The searching time for worse case is
O(log2n).
17
Search Conclusion
• Searching is a process to allocate an element in a list and
return the index of the searched element.
• Basic searching techniques : sequential search and binary
search.
• Sequential search can be implemented on sorted and
unsorted list, while binary search can be implemented
only on sorted list.
• Sequential search on sorted data is more efficient than
sequential search on unsorted data.
• Binary search is more efficient than sequential search.
• Basic searching techniques explained in this class are
only suitable for small sets of data. Hashing and indexing
are suitable for searching large sets of data.
18

More Related Content

PDF
PPT
Searching algorithm
PPTX
Searching
PPT
Search techniques and Hashing
PPTX
Rahat &amp; juhith
PDF
searching
PDF
ODD EVEN BASED BINARY SEARCH
PPTX
Searching linear &amp; binary search
Searching algorithm
Searching
Search techniques and Hashing
Rahat &amp; juhith
searching
ODD EVEN BASED BINARY SEARCH
Searching linear &amp; binary search

What's hot (20)

PPSX
Linear and binary search
PPTX
Search methods
PPT
SEARCHING AND SORTING ALGORITHMS
PDF
linear search and binary search
PDF
Searching and Sorting Techniques in Data Structure
PPTX
LINEAR SEARCH
PPTX
Unit 7 sorting
PPTX
Data structure
PDF
Search Algprithms
PPTX
Binary search Algorithm
PDF
Binary search algorithm
PPTX
Link list 2
PPT
Searching algorithms
PPTX
Sequential & binary, linear search
PPTX
Linear search-and-binary-search
PDF
Algorithm and Data Structure - Linear Search
PPT
Chapter 14
PDF
Algorithm and Data Structure - Binary Search
PPTX
Ppt of operations on one way link list
PPTX
Searching techniques
Linear and binary search
Search methods
SEARCHING AND SORTING ALGORITHMS
linear search and binary search
Searching and Sorting Techniques in Data Structure
LINEAR SEARCH
Unit 7 sorting
Data structure
Search Algprithms
Binary search Algorithm
Binary search algorithm
Link list 2
Searching algorithms
Sequential & binary, linear search
Linear search-and-binary-search
Algorithm and Data Structure - Linear Search
Chapter 14
Algorithm and Data Structure - Binary Search
Ppt of operations on one way link list
Searching techniques
Ad

Viewers also liked (20)

PPT
Business model
PPT
DISTRIBUTED INTERACTIVE VIRTUAL ENVIRONMENT
PPTX
Trees data structure
PDF
SOLUTION MANUAL OF COMMUNICATION NETWORKS BY ALBERTO LEON GARCIA & INDRA WIDJAJA
PPTX
Graphs data Structure
PDF
VTU 1ST SEM PROGRAMMING IN C & DATA STRUCTURES SOLVED PAPERS OF JUNE-2015 & ...
KEY
Ancient Ideas of Creation & Evolution
PPTX
National Air And Space Museum Washington DC
PPT
Introduction to Information Technology ch 02_a
KEY
History of Creationism, Parts II & III
PPT
Dc parent 14 2
PPTX
How We Got Where We Are: 40 Years of Planning...
PPT
Google
KEY
Introduction to "Origins, Evolution & Creation"
PDF
Algorithms - Aaron Bloomfield
KEY
The Scientific Revolution
PPTX
Chapter one
Business model
DISTRIBUTED INTERACTIVE VIRTUAL ENVIRONMENT
Trees data structure
SOLUTION MANUAL OF COMMUNICATION NETWORKS BY ALBERTO LEON GARCIA & INDRA WIDJAJA
Graphs data Structure
VTU 1ST SEM PROGRAMMING IN C & DATA STRUCTURES SOLVED PAPERS OF JUNE-2015 & ...
Ancient Ideas of Creation & Evolution
National Air And Space Museum Washington DC
Introduction to Information Technology ch 02_a
History of Creationism, Parts II & III
Dc parent 14 2
How We Got Where We Are: 40 Years of Planning...
Google
Introduction to "Origins, Evolution & Creation"
Algorithms - Aaron Bloomfield
The Scientific Revolution
Chapter one
Ad

Similar to Ocw chp6 2searchbinary (20)

PPTX
Chapter 2. data structure and algorithm
PPTX
Chapter 2 Sorting and Searching .pptx.soft
PPTX
searching techniques.pptx
PPTX
Data structure Unit - II Searching and Sorting.pptx
PPTX
Searching_Sorting.pptx
PDF
dsa pdf.pdf
PPTX
Unit 8 searching and hashing
PPTX
Lecture_Oct26.pptx
PPTX
Searching & Algorithms IN DATA STRUCTURES
PPTX
data_structure_Chapter two_computer.pptx
PPTX
unit II_2_i.pptx
PPTX
Data Structures Unit 2 FINAL presentation.pptx
PPTX
Searching Algorithms - Foundations of Algorithms
PPTX
Chapter #3 (Searchinmmmg ALgorithm).pptx
PDF
advanced searching and sorting.pdf
PPT
search_sort.ppt
PPTX
an Introduction to binary search algorithm
PPT
4- searching.ppt
PDF
BINARY SEARCH - A - DATA STRUCTURE AND ALGORITHM.pdf
PPTX
AJisthewewrtyuiojhghfdfsgvhjhklopi87ytrytfghjk
Chapter 2. data structure and algorithm
Chapter 2 Sorting and Searching .pptx.soft
searching techniques.pptx
Data structure Unit - II Searching and Sorting.pptx
Searching_Sorting.pptx
dsa pdf.pdf
Unit 8 searching and hashing
Lecture_Oct26.pptx
Searching & Algorithms IN DATA STRUCTURES
data_structure_Chapter two_computer.pptx
unit II_2_i.pptx
Data Structures Unit 2 FINAL presentation.pptx
Searching Algorithms - Foundations of Algorithms
Chapter #3 (Searchinmmmg ALgorithm).pptx
advanced searching and sorting.pdf
search_sort.ppt
an Introduction to binary search algorithm
4- searching.ppt
BINARY SEARCH - A - DATA STRUCTURE AND ALGORITHM.pdf
AJisthewewrtyuiojhghfdfsgvhjhklopi87ytrytfghjk

Recently uploaded (20)

PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Network Security Unit 5.pdf for BCA BBA.
PPTX
cloud_computing_Infrastucture_as_cloud_p
PDF
Encapsulation theory and applications.pdf
PPTX
TechTalks-8-2019-Service-Management-ITIL-Refresh-ITIL-4-Framework-Supports-Ou...
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Getting Started with Data Integration: FME Form 101
PPTX
TLE Review Electricity (Electricity).pptx
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Accuracy of neural networks in brain wave diagnosis of schizophrenia
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Assigned Numbers - 2025 - Bluetooth® Document
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PPTX
SOPHOS-XG Firewall Administrator PPT.pptx
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PPTX
1. Introduction to Computer Programming.pptx
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Building Integrated photovoltaic BIPV_UPV.pdf
Network Security Unit 5.pdf for BCA BBA.
cloud_computing_Infrastucture_as_cloud_p
Encapsulation theory and applications.pdf
TechTalks-8-2019-Service-Management-ITIL-Refresh-ITIL-4-Framework-Supports-Ou...
Digital-Transformation-Roadmap-for-Companies.pptx
Getting Started with Data Integration: FME Form 101
TLE Review Electricity (Electricity).pptx
Unlocking AI with Model Context Protocol (MCP)
Accuracy of neural networks in brain wave diagnosis of schizophrenia
Diabetes mellitus diagnosis method based random forest with bat algorithm
Reach Out and Touch Someone: Haptics and Empathic Computing
Assigned Numbers - 2025 - Bluetooth® Document
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
SOPHOS-XG Firewall Administrator PPT.pptx
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
1. Introduction to Computer Programming.pptx
MIND Revenue Release Quarter 2 2025 Press Release
Mobile App Security Testing_ A Comprehensive Guide.pdf

Ocw chp6 2searchbinary

  • 1. Searching Techniques: Binary Search SCJ2013 Data Structure & Algorithms Nor Bahiah Hj Ahmad & Dayang Norhayati A. Jawawi
  • 2. Objectives At the end of the class, students are expected to be able to do the following: • Understand the searching technique concept and the purpose of searching operation. • Understand the implementation of basic searching algorithm; 1. Sequential search. • Sequential search on unsorted data. • Sequential search on sorted data. 2. Binary Search. • Able to analyze the efficiency of the searching technique. • Able to implement searching technique in problem solving.
  • 4. Binary Search • The drawback of sequential search algorithm is having to traverse the entire list, O(n) • Sorting the list does minimize the cost of traversing the whole data set, but we can improve the searching efficiency by using the Binary Search algorithm 4
  • 5. Binary Search • Consider a list in ascending sorted order. For a sequential search, searching is from the beginning until an item is found or the end is reached. • Binary search improve the algorithm by removing as much of the data set as possible so that the item is found more quickly. • Search process is started at the middle of the list, then the algorithm determine which half the item is in (because the list is sorted). – It divides the working range in half with a single test. By repeating the procedure, the result is an efficient search algorithm-O(log2 n). 5
  • 6. • starts by comparing the search key with the element at the middle – If the value matches, it will be return to the calling function (index = MIDDLE) – If the search key < the middle element, search will be focused on the elements between the first element to the element before the middle element (MIDDLE -1) – If the search key is not found, the element at the middle of the first element and the MIDDLE –1 element will be compared with the search key. – If the search key > the middle element, search will only be focused on the elements between the second MIDDLE element to the first MIDDLE element. – Search is repeated until the searched key is found or the last element in the subset is traversed (LEFT>RIGHT ). 6 Binary Search array 5522 443311 [0] [1] [2] [3] [4] [5] [6] 7766 5522 443311 [0] [1] [2] [3] [4] [5] [6] 7766 MIDDLE [0] [1] [2] 22 3311 MIDDLE [2] 33 MIDDLE LEFT>RIGHT 35search_key
  • 7. Binary Search Function 7 int binary_search( int search_key, int array_size, const int array [] ) { bool found = false; int index = -1 //-1 means record not found int MIDDLE, LEFT = 0, RIGHT = array_size-1; while (( LEFT<= RIGHT ) && (!found)) { MIDDLE = (LEFT + RIGHT )/ 2; // Get middle index if ( array[MIDDLE] == search_key) { index = MIDDLE; found = true; } else if (array[MIDDLE] > search_key) RIGHT= MIDDLE– 1; // search is focused // on the left side of list else LEFT= MIDDLE+ 1 // search is focused // on the right side of the list } //end while return index; }//end function
  • 8. Binary Search on a Sorted List • Search starts by obtaining the MIDDLE index of the array • assume: – search_key = 35 • MIDDLE= ( 0 + 6 ) / 2 = 3 { First MIDDLE index} 8 35 11 22 33 44 55 66 77 search_key array
  • 9. • search_key 35 is compared with the element at the fourth index in the array, which is array[3] with the value 44. – search_key < MIDDLE value, therefore search will be focused on the elements between the first index and the third index only (index 1 to MIDDLE-1) • Process to obtain MIDDLE index is repeated MIDDLE = ( 0 + 2 ) / 2 = 1 { second MIDDLE index} Search_key 35 is compared with the element at the second index, which is array[1] with the value 22 – search_key > MIDDLE value, therefore search will be focused only on the elements between the second MIDDLE index to the first MIDDLE index. MIDDLE = (2 + 2 ) / 2 = 2 { third MIDDLE index} 9 Binary Search on Sorted List
  • 10. • Element at the third index, array[2] with the value 33 is not equal to the value of the search key. • Search process has reached the last element of the traversed subset, therefore search is terminated and assumed fail. • To search from the list sorted descending, change operator “ > ” to operator “ < “ to the following statement : else if (array [ MIDDLE ] > search_key) RIGHT = MIDDLE – 1; 10 Binary Search on Sorted List
  • 11. Step 1 Steps to Execute Binary Search 11 false 12 14 16 18 20 22 24 26 28 30 [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] LEFT MIDDLE RIGHT LEFT MIDDLE RIGHT 4 9 0 -1 22 found index search_key array Assume: search_key = 22, array_size = 10
  • 12. Step 2 12 12 14 16 18 20 22 24 26 28 30 [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] MIDDLE RIGHT 7 9 5 -1 22 falsefound index search_key array LEFT LEFT MIDDLE RIGHT Steps to Execute Binary Search
  • 13. Step 3 13 12 14 16 18 20 22 24 26 28 30 [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] LEFT MIDDLE RIGHT LEFT MIDDLE RIGHT 5 6 5 5 22 truestatus index search_key array Steps to Execute Binary Search
  • 14. Binary Search Analysis • Binary Search starts searching by comparing element in the middle. Thus the searching process start at n/2 for a list size = n. • If the middle value does not matches with the search key, then the searching area will be reduced to the left or right sublist only. This will reduce the searching area to ½ n. • From half of the list, the second middle value will be identified. Again, if the middle value does not matches with the search key, the searching area will be reduced to the left or right sub list only. The searching area will reduce ½ ( ½ n). 14
  • 15. Binary Search Analysis • The process of looking for middle point and reducing the searching area to the left or right sublist will be repeated until the middle value is equal to the middle value (search key is found) or the last value in sublist has been traverse. • If the repetition occur k times, then at iteration k , the searching area is reduced to ( ½ )kn. 15
  • 16. Binary Search Analysis • figure above shows the reducing size for binary search area. • At iteration k for array size = n , searching area will be reduced from n to ( ½ )kn. 16 k n n (1/2) k n
  • 17. Binary Search Analysis • Best case for binary search happen if the search key is found in the middle array. O(1). • Worse case for binary search happen if the search key is not in the list or the searching size equal to 1. • The searching time for worse case is O(log2n). 17
  • 18. Search Conclusion • Searching is a process to allocate an element in a list and return the index of the searched element. • Basic searching techniques : sequential search and binary search. • Sequential search can be implemented on sorted and unsorted list, while binary search can be implemented only on sorted list. • Sequential search on sorted data is more efficient than sequential search on unsorted data. • Binary search is more efficient than sequential search. • Basic searching techniques explained in this class are only suitable for small sets of data. Hashing and indexing are suitable for searching large sets of data. 18