SlideShare a Scribd company logo
Sorting And Searching 
Arrays 
The Binary Search Algorithm
Binary Search Algorithm-Advantages of 
Binary Search 
 Binary search s more efficient with large arrays 
 Binary search’s only requirement is – must be 
sorted in ascending order. 
 Starts in the middle instead of at the beginning 
 The search is over if the desired value is in 
that element. Otherwise,
Binary Search Algorithm 
 Instead of testing the first element in the array, the binary 
search starts with the element in the middle and if that 
element has the desired value, then the search is over 
 If the value in the middle is not the desired element, then it is 
either greater than or less than the value being searched. 
 If it is greater, then the desired value(if it is in the list) will be 
found somewhere in the first half of the array. 
 If it is less, then the desired value will be found somewhere in 
the last half of the array(if it is in the list)
Binary Search Algorithm 
 If the desired value isn't found in the middle element, the 
procedure is repeated for half the array. Example: if the last 
half of the array is to be searched, the algorithm tests its 
middle element. 
 If the value still isn't found in the middle element, the search 
is narrowed to the quarter of the array before or after that 
element. 
 The search continues until a value is either found or there is 
no more elements to test.
Binary Search Algorithm 
 The Binary Search Algorithm uses three variables to mark 
positions within the array: first, last, and middle. 
 The first and last variables mark the boundaries of the portion of 
array currently being searched and are initialized with the 
subscripts of the arrays first and last elements. 
 The subscript halfway between the first and last element is 
calculated and stored in the middle variable. 
 If the middle element of the array does not contain the search 
value, then first or last variables are adjusted so that only the 
top or bottom half of the array is searched during the next 
iteration. This cuts the portion of the array being searched in half 
each time the loop fails to locate the search value.
Binary Search Algorithm-Efficiency 
of a binary search. 
 Each time a binary search fails to find the desired item, it e 
eliminates half of the remaining portion of the array that must be 
searched. 
 Example: if a binary search fails to find an item on the first 
attempt in an array with 1000 elements, the number of elements 
that remains to be searched is 500. if the value is not found on 
the second attempt, the number of elements that remains to be 
searched is 250, and so on. 
 The process continues until the binary search has either located 
the desired item or determined that it is not in the array. 
 With 1000 elements, the binary search takes no more than 10 
comparisons. A sequential search would take an average of 500 
comparisons, so the binary search his much more efficient.
1 Module main() 
2 // Constant for array sizes 
3 Constant Integer SIZE = 6 
4 
5 // Array of instructor names, already sorted in 
6 // ascending order. 
7 Declare String names[SIZE] = "Hall", "Harrison", 
8 "Hoyle", "Kimura", 
9 "Lopez", "Pike" 
10 
11 // Parallel array of instructor phone numbers. 
12 Declare String phones[SIZE] = "555-6783", "555-0199", 
13 "555-9974", "555-2377", 
14 "555-7772", "555-1716" 
15 
16 // Variable to hold the last name to search for. 
17 Declare String searchName 
18 
19 // Variable to hold the subscript of the name. 
20 Declare Integer index 
21
22 // Variable to control the loop. 
23 Declare String again = "Y" 
24 
25 While (again == "Y" OR again == "y") 
26 // Get the name to search for. 
27 Display "Enter a last name to search for." 
28 Input searchName 
29 
30 // Search for the last name. 
31 index = binarySearch(names, searchName, SIZE) 
32 
33 If index ! = -1 Then 
34 // Display the phone number. 
35 Display "The phone number is ", phones[index] 
36 Else 
37 // The name was not found in the array. 
38 Display searchName, " was not found." 
39 End If 
40 
41 // Search again? 
42 Display "Do you want to search again? (Y=Yes, N=No)" 
43 Input again 
44 End While 
45 
46 End Module
47 
48 // The binarySearch function accepts as arguments a String 
49 // array, a value to search the array for, and the size 
50 // of the array. If the value is found in the array, its 
51 // subscript is returned. Otherwise, -1 is returned, 
52 // indicating that the value was not found in the array. 
53 Function Integer binarySearch(String array[], String value, 
54 Integer arraySize) 
55 // Variable to hold the subscript of the first element. 
56 Declare Integer first = 0 
57 
58 // Variable to hold the subscript of the last element. 
59 Declare Integer last = arraySize - 1 
60 
61 // Position of the search value 
62 Declare Integer position = -1 
63 
64 // Flag 
65 Declare Boolean found = False 
66 
67 // Variable to hold the subscript of the midpoint. 
68 Declare Integer middle
69 
70 While (NOT found) AND (first <= last) 
71 // Calculate the midpoint. 
72 Set middle = (first + last) / 2 
73 
74 // See if the value is found at the midpoint... 
75 If array[middle] == value Then 
76 Set found = True 
77 Set position = middle 
78 
79 // Else, if the value is in the lower half... 
80 Else If array[middle] > value Then 
81 Set last = middle - 1 
82 
83 // Else, if the value is in the upper half... 
84 Else 
85 Set first = middle + 1 
86 End If 
87 End While 
88 
89 // Return the position of the item, or -1 
90 // if the item was not found. 
91 Return position 
92 End Function
88 
89 // Return the position of the item, or -1 
90 // if the item was not found. 
91 Return position 
92 End Function 
Program Output (with Input Shown in Bold) 
Enter a last name to search for. 
Lopez [Enter] 
The phone number is 555-7772 
Do you want to search again? (Y=Yes, N=No) 
Y [Enter] 
Enter a last name to search for. 
Harrison [Enter] 
The phone number is 555-0199 
Do you want to search again? (Y=Yes, N=No) 
Y [Enter] 
Enter a last name to search for. 
Lee [Enter] 
Lee was not found. 
Do you want to search again? (Y=Yes, N=No) 
N [Enter]
Ad

Recommended

PPTX
7 searching injava-binary
irdginfo
 
PPTX
Binary search
Raghu nath
 
PDF
Binary search algorithm
maamir farooq
 
PDF
Binary Search - Design & Analysis of Algorithms
Drishti Bhalla
 
PPTX
Rahat &amp; juhith
Rj Juhith
 
PPTX
Algorithm & data structures lec4&5
Abdul Khan
 
PDF
Linear search algorithm
NeoClassical
 
PPT
Searching Sorting
guest2cb109
 
PPT
L10 sorting-searching
mondalakash2012
 
PDF
Sorting Algorithms
Mohammed Hussein
 
PPTX
Searching linear &amp; binary search
nikunjandy
 
PPTX
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
Umesh Kumar
 
PPTX
Dsa – data structure and algorithms searching
sajinis3
 
PPT
SEARCHING AND SORTING ALGORITHMS
Gokul Hari
 
PPTX
Binary search
AparnaKumari31
 
PPT
Data Structures- Part3 arrays and searching algorithms
Abdullah Al-hazmy
 
PPTX
Searching & Sorting Algorithms
Rahul Jamwal
 
PPT
Binary Search
kunj desai
 
PPTX
Coin Changing, Binary Search , Linear Search - Algorithm
Md Sadequl Islam
 
PPT
Searching algorithms
Trupti Agrawal
 
PDF
linear search and binary search
Zia Ush Shamszaman
 
PDF
Searching
A. S. M. Shafi
 
PPT
Searching algorithm
MG Thushara Pradeesh
 
PPTX
Dsa – data structure and algorithms sorting
sajinis3
 
PPTX
Sorting
Jasmine Chen
 
PPTX
Sequential & binary, linear search
montazur420
 
PPSX
Lecture 3 data structures & algorithms - sorting techniques - http://techiem...
Dharmendra Prasad
 
PPTX
Linear search-and-binary-search
International Islamic University
 
PDF
Binary Search Algorithm
Anastasia Jakubow
 
PPTX
Sorting algorithms
Trupti Agrawal
 

More Related Content

What's hot (20)

PPT
L10 sorting-searching
mondalakash2012
 
PDF
Sorting Algorithms
Mohammed Hussein
 
PPTX
Searching linear &amp; binary search
nikunjandy
 
PPTX
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
Umesh Kumar
 
PPTX
Dsa – data structure and algorithms searching
sajinis3
 
PPT
SEARCHING AND SORTING ALGORITHMS
Gokul Hari
 
PPTX
Binary search
AparnaKumari31
 
PPT
Data Structures- Part3 arrays and searching algorithms
Abdullah Al-hazmy
 
PPTX
Searching & Sorting Algorithms
Rahul Jamwal
 
PPT
Binary Search
kunj desai
 
PPTX
Coin Changing, Binary Search , Linear Search - Algorithm
Md Sadequl Islam
 
PPT
Searching algorithms
Trupti Agrawal
 
PDF
linear search and binary search
Zia Ush Shamszaman
 
PDF
Searching
A. S. M. Shafi
 
PPT
Searching algorithm
MG Thushara Pradeesh
 
PPTX
Dsa – data structure and algorithms sorting
sajinis3
 
PPTX
Sorting
Jasmine Chen
 
PPTX
Sequential & binary, linear search
montazur420
 
PPSX
Lecture 3 data structures & algorithms - sorting techniques - http://techiem...
Dharmendra Prasad
 
PPTX
Linear search-and-binary-search
International Islamic University
 
L10 sorting-searching
mondalakash2012
 
Sorting Algorithms
Mohammed Hussein
 
Searching linear &amp; binary search
nikunjandy
 
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
Umesh Kumar
 
Dsa – data structure and algorithms searching
sajinis3
 
SEARCHING AND SORTING ALGORITHMS
Gokul Hari
 
Binary search
AparnaKumari31
 
Data Structures- Part3 arrays and searching algorithms
Abdullah Al-hazmy
 
Searching & Sorting Algorithms
Rahul Jamwal
 
Binary Search
kunj desai
 
Coin Changing, Binary Search , Linear Search - Algorithm
Md Sadequl Islam
 
Searching algorithms
Trupti Agrawal
 
linear search and binary search
Zia Ush Shamszaman
 
Searching
A. S. M. Shafi
 
Searching algorithm
MG Thushara Pradeesh
 
Dsa – data structure and algorithms sorting
sajinis3
 
Sorting
Jasmine Chen
 
Sequential & binary, linear search
montazur420
 
Lecture 3 data structures & algorithms - sorting techniques - http://techiem...
Dharmendra Prasad
 
Linear search-and-binary-search
International Islamic University
 

Viewers also liked (20)

PDF
Binary Search Algorithm
Anastasia Jakubow
 
PPTX
Sorting algorithms
Trupti Agrawal
 
PDF
Bcsl 033 data and file structures lab s2-3
Dr. Loganathan R
 
PDF
Bcsl 033 data and file structures lab s3-3
Dr. Loganathan R
 
PPT
AS computing
Chris Cardew
 
PPT
Linear search Algorithm
amit kumar
 
PDF
04 sorting
martchasera92
 
PPTX
Sorting Technique
Salman Vadsarya
 
PPT
CHC Finance: Using the New IRS 990 Form
Phillip Bergquist - MPCA
 
PPTX
IgnouBCA
diwakar9903
 
PPTX
Stack and Queue
Apurbo Datta
 
PPTX
Mca ii dfs u-3 linklist,stack,queue
Rai University
 
PDF
Bubblesort Algorithm
Tobias Straub
 
PPT
Queue and stacks
grahamwell
 
PPTX
My lecture stack_queue_operation
Senthil Kumar
 
PPTX
Bubble Sort
geeortiz
 
PPT
Data Structures - Searching & sorting
Kaushal Shah
 
PDF
Sorting algorithms
Vicente García Díaz
 
PPTX
Data Structures - Lecture 8 [Sorting Algorithms]
Muhammad Hammad Waseem
 
Binary Search Algorithm
Anastasia Jakubow
 
Sorting algorithms
Trupti Agrawal
 
Bcsl 033 data and file structures lab s2-3
Dr. Loganathan R
 
Bcsl 033 data and file structures lab s3-3
Dr. Loganathan R
 
AS computing
Chris Cardew
 
Linear search Algorithm
amit kumar
 
04 sorting
martchasera92
 
Sorting Technique
Salman Vadsarya
 
CHC Finance: Using the New IRS 990 Form
Phillip Bergquist - MPCA
 
IgnouBCA
diwakar9903
 
Stack and Queue
Apurbo Datta
 
Mca ii dfs u-3 linklist,stack,queue
Rai University
 
Bubblesort Algorithm
Tobias Straub
 
Queue and stacks
grahamwell
 
My lecture stack_queue_operation
Senthil Kumar
 
Bubble Sort
geeortiz
 
Data Structures - Searching & sorting
Kaushal Shah
 
Sorting algorithms
Vicente García Díaz
 
Data Structures - Lecture 8 [Sorting Algorithms]
Muhammad Hammad Waseem
 
Ad

Similar to Sorting and searching arrays binary search algorithm (20)

PPT
1 class linear and Binary search (3).ppt
KanchanRaut13
 
DOCX
MODULE 5-Searching and-sorting
nikshaikh786
 
PPTX
Searching and Sorting Algorithms in Data Structures
poongothai11
 
PPTX
Algorithm 8th lecture linear & binary search(2).pptx
Aftabali702240
 
PPTX
Array ppt
Kaushal Mehta
 
PDF
Array.pdf
DEEPAK948083
 
PDF
Data structures arrays
maamir farooq
 
PPT
4.1 sequentioal search
Krish_ver2
 
PPTX
Searching techniques
ER Punit Jain
 
PPT
1 D Arrays in C++
poonam.rwalia
 
PPTX
Arrays
sana younas
 
PPTX
Searching in DSA that follow a dsa searching.pptx
StephenRobert15
 
PDF
Unit 6 dsa SEARCHING AND SORTING
PUNE VIDYARTHI GRIHA'S COLLEGE OF ENGINEERING, NASHIK
 
PDF
advanced searching and sorting.pdf
haramaya university
 
PPTX
Analysis of Algorithm - Binary Search.pptx
Maulana Abul Kalam Azad University of Technology
 
PPTX
Arrays
uos
 
PPTX
Linear and Binary search .pptx
p83629918
 
PPTX
ARRAY PPT.pptx for mca finals placement
utkarshKatiyar32
 
PPTX
data structures and algorithms Unit 3
infanciaj
 
PPTX
Arrays In C++
Awais Alam
 
1 class linear and Binary search (3).ppt
KanchanRaut13
 
MODULE 5-Searching and-sorting
nikshaikh786
 
Searching and Sorting Algorithms in Data Structures
poongothai11
 
Algorithm 8th lecture linear & binary search(2).pptx
Aftabali702240
 
Array ppt
Kaushal Mehta
 
Array.pdf
DEEPAK948083
 
Data structures arrays
maamir farooq
 
4.1 sequentioal search
Krish_ver2
 
Searching techniques
ER Punit Jain
 
1 D Arrays in C++
poonam.rwalia
 
Arrays
sana younas
 
Searching in DSA that follow a dsa searching.pptx
StephenRobert15
 
advanced searching and sorting.pdf
haramaya university
 
Analysis of Algorithm - Binary Search.pptx
Maulana Abul Kalam Azad University of Technology
 
Arrays
uos
 
Linear and Binary search .pptx
p83629918
 
ARRAY PPT.pptx for mca finals placement
utkarshKatiyar32
 
data structures and algorithms Unit 3
infanciaj
 
Arrays In C++
Awais Alam
 
Ad

Recently uploaded (20)

PDF
From Manual to Auto Searching- FME in the Driver's Seat
Safe Software
 
PDF
Cracking the Code - Unveiling Synergies Between Open Source Security and AI.pdf
Priyanka Aash
 
PDF
The Future of Product Management in AI ERA.pdf
Alyona Owens
 
PDF
Coordinated Disclosure for ML - What's Different and What's the Same.pdf
Priyanka Aash
 
PDF
Smarter Aviation Data Management: Lessons from Swedavia Airports and Sweco
Safe Software
 
PDF
2025_06_18 - OpenMetadata Community Meeting.pdf
OpenMetadata
 
PDF
Cyber Defense Matrix Workshop - RSA Conference
Priyanka Aash
 
PDF
The Growing Value and Application of FME & GenAI
Safe Software
 
PPTX
Curietech AI in action - Accelerate MuleSoft development
shyamraj55
 
PDF
AI vs Human Writing: Can You Tell the Difference?
Shashi Sathyanarayana, Ph.D
 
PPTX
Security Tips for Enterprise Azure Solutions
Michele Leroux Bustamante
 
PDF
WebdriverIO & JavaScript: The Perfect Duo for Web Automation
digitaljignect
 
PDF
Quantum AI Discoveries: Fractal Patterns Consciousness and Cyclical Universes
Saikat Basu
 
PPTX
CapCut Pro Crack For PC Latest Version {Fully Unlocked} 2025
pcprocore
 
PDF
ReSTIR [DI]: Spatiotemporal reservoir resampling for real-time ray tracing ...
revolcs10
 
PDF
Techniques for Automatic Device Identification and Network Assignment.pdf
Priyanka Aash
 
PPTX
You are not excused! How to avoid security blind spots on the way to production
Michele Leroux Bustamante
 
PPTX
UserCon Belgium: Honey, VMware increased my bill
stijn40
 
PDF
Python Conference Singapore - 19 Jun 2025
ninefyi
 
PDF
Raman Bhaumik - Passionate Tech Enthusiast
Raman Bhaumik
 
From Manual to Auto Searching- FME in the Driver's Seat
Safe Software
 
Cracking the Code - Unveiling Synergies Between Open Source Security and AI.pdf
Priyanka Aash
 
The Future of Product Management in AI ERA.pdf
Alyona Owens
 
Coordinated Disclosure for ML - What's Different and What's the Same.pdf
Priyanka Aash
 
Smarter Aviation Data Management: Lessons from Swedavia Airports and Sweco
Safe Software
 
2025_06_18 - OpenMetadata Community Meeting.pdf
OpenMetadata
 
Cyber Defense Matrix Workshop - RSA Conference
Priyanka Aash
 
The Growing Value and Application of FME & GenAI
Safe Software
 
Curietech AI in action - Accelerate MuleSoft development
shyamraj55
 
AI vs Human Writing: Can You Tell the Difference?
Shashi Sathyanarayana, Ph.D
 
Security Tips for Enterprise Azure Solutions
Michele Leroux Bustamante
 
WebdriverIO & JavaScript: The Perfect Duo for Web Automation
digitaljignect
 
Quantum AI Discoveries: Fractal Patterns Consciousness and Cyclical Universes
Saikat Basu
 
CapCut Pro Crack For PC Latest Version {Fully Unlocked} 2025
pcprocore
 
ReSTIR [DI]: Spatiotemporal reservoir resampling for real-time ray tracing ...
revolcs10
 
Techniques for Automatic Device Identification and Network Assignment.pdf
Priyanka Aash
 
You are not excused! How to avoid security blind spots on the way to production
Michele Leroux Bustamante
 
UserCon Belgium: Honey, VMware increased my bill
stijn40
 
Python Conference Singapore - 19 Jun 2025
ninefyi
 
Raman Bhaumik - Passionate Tech Enthusiast
Raman Bhaumik
 

Sorting and searching arrays binary search algorithm

  • 1. Sorting And Searching Arrays The Binary Search Algorithm
  • 2. Binary Search Algorithm-Advantages of Binary Search  Binary search s more efficient with large arrays  Binary search’s only requirement is – must be sorted in ascending order.  Starts in the middle instead of at the beginning  The search is over if the desired value is in that element. Otherwise,
  • 3. Binary Search Algorithm  Instead of testing the first element in the array, the binary search starts with the element in the middle and if that element has the desired value, then the search is over  If the value in the middle is not the desired element, then it is either greater than or less than the value being searched.  If it is greater, then the desired value(if it is in the list) will be found somewhere in the first half of the array.  If it is less, then the desired value will be found somewhere in the last half of the array(if it is in the list)
  • 4. Binary Search Algorithm  If the desired value isn't found in the middle element, the procedure is repeated for half the array. Example: if the last half of the array is to be searched, the algorithm tests its middle element.  If the value still isn't found in the middle element, the search is narrowed to the quarter of the array before or after that element.  The search continues until a value is either found or there is no more elements to test.
  • 5. Binary Search Algorithm  The Binary Search Algorithm uses three variables to mark positions within the array: first, last, and middle.  The first and last variables mark the boundaries of the portion of array currently being searched and are initialized with the subscripts of the arrays first and last elements.  The subscript halfway between the first and last element is calculated and stored in the middle variable.  If the middle element of the array does not contain the search value, then first or last variables are adjusted so that only the top or bottom half of the array is searched during the next iteration. This cuts the portion of the array being searched in half each time the loop fails to locate the search value.
  • 6. Binary Search Algorithm-Efficiency of a binary search.  Each time a binary search fails to find the desired item, it e eliminates half of the remaining portion of the array that must be searched.  Example: if a binary search fails to find an item on the first attempt in an array with 1000 elements, the number of elements that remains to be searched is 500. if the value is not found on the second attempt, the number of elements that remains to be searched is 250, and so on.  The process continues until the binary search has either located the desired item or determined that it is not in the array.  With 1000 elements, the binary search takes no more than 10 comparisons. A sequential search would take an average of 500 comparisons, so the binary search his much more efficient.
  • 7. 1 Module main() 2 // Constant for array sizes 3 Constant Integer SIZE = 6 4 5 // Array of instructor names, already sorted in 6 // ascending order. 7 Declare String names[SIZE] = "Hall", "Harrison", 8 "Hoyle", "Kimura", 9 "Lopez", "Pike" 10 11 // Parallel array of instructor phone numbers. 12 Declare String phones[SIZE] = "555-6783", "555-0199", 13 "555-9974", "555-2377", 14 "555-7772", "555-1716" 15 16 // Variable to hold the last name to search for. 17 Declare String searchName 18 19 // Variable to hold the subscript of the name. 20 Declare Integer index 21
  • 8. 22 // Variable to control the loop. 23 Declare String again = "Y" 24 25 While (again == "Y" OR again == "y") 26 // Get the name to search for. 27 Display "Enter a last name to search for." 28 Input searchName 29 30 // Search for the last name. 31 index = binarySearch(names, searchName, SIZE) 32 33 If index ! = -1 Then 34 // Display the phone number. 35 Display "The phone number is ", phones[index] 36 Else 37 // The name was not found in the array. 38 Display searchName, " was not found." 39 End If 40 41 // Search again? 42 Display "Do you want to search again? (Y=Yes, N=No)" 43 Input again 44 End While 45 46 End Module
  • 9. 47 48 // The binarySearch function accepts as arguments a String 49 // array, a value to search the array for, and the size 50 // of the array. If the value is found in the array, its 51 // subscript is returned. Otherwise, -1 is returned, 52 // indicating that the value was not found in the array. 53 Function Integer binarySearch(String array[], String value, 54 Integer arraySize) 55 // Variable to hold the subscript of the first element. 56 Declare Integer first = 0 57 58 // Variable to hold the subscript of the last element. 59 Declare Integer last = arraySize - 1 60 61 // Position of the search value 62 Declare Integer position = -1 63 64 // Flag 65 Declare Boolean found = False 66 67 // Variable to hold the subscript of the midpoint. 68 Declare Integer middle
  • 10. 69 70 While (NOT found) AND (first <= last) 71 // Calculate the midpoint. 72 Set middle = (first + last) / 2 73 74 // See if the value is found at the midpoint... 75 If array[middle] == value Then 76 Set found = True 77 Set position = middle 78 79 // Else, if the value is in the lower half... 80 Else If array[middle] > value Then 81 Set last = middle - 1 82 83 // Else, if the value is in the upper half... 84 Else 85 Set first = middle + 1 86 End If 87 End While 88 89 // Return the position of the item, or -1 90 // if the item was not found. 91 Return position 92 End Function
  • 11. 88 89 // Return the position of the item, or -1 90 // if the item was not found. 91 Return position 92 End Function Program Output (with Input Shown in Bold) Enter a last name to search for. Lopez [Enter] The phone number is 555-7772 Do you want to search again? (Y=Yes, N=No) Y [Enter] Enter a last name to search for. Harrison [Enter] The phone number is 555-0199 Do you want to search again? (Y=Yes, N=No) Y [Enter] Enter a last name to search for. Lee [Enter] Lee was not found. Do you want to search again? (Y=Yes, N=No) N [Enter]