SlideShare a Scribd company logo
1 of 16
Unit 2 Example
Linear and Binary Search
Prof. D.R. Dhotre
Searching Techniques
 Linear search
small arrays
unsorted arrays
 Binary search
large arrays
sorted arrays
Linear Search Algorithm
Start at first element of array.
Compare value to value (key) for which you are searching
Continue with next element of the array until you find a
match or reach the last element in the array.
Note: On the average you will have to compare the search
key with half the elements in the array.
Linear Search Code
const int arraySize = 100;
int a[arraySize] = {1, 100, 2, 66, 55, 44, 88, 77, 12, 23, 45, 9, 87};
int key = 88;
bool found = false;
for (int i = 0; i < arraySize; i++)
{
if (a[i] = = key)
{
cout << “Found it at array subscript “ << i << endl;
found = true;
break;
}
}
if (! found)
cout << “Could not find element “ << key << “ in array a” << endl;
Binary Search Algorithm
1. May only be used on a sorted array.
2. Eliminates one half of the elements after each comparison.
3. Locate the middle of the array
4. Compare the value at that location with the search key.
5. If they are equal - done!
6. Otherwise, decide which half of the array contains the search
key.
7. Repeat the search on that half of the array and ignore the other
half.
8. The search continues until the key is matched or no elements
remain to be searched.
Binary Search Example
1
0
1
2
3
4
5
6
7
8
9
10
11
12
5
15
19
25
27
29
31
33
45
55
88
100
middle of the array
compare a[6] and 19
19 is smaller than 29 so the next
search will use the lower half of the array
search key = 19
a
Binary Search Pass 2
1
0
1
2
3
4
5
5
15
19
25
27
search key = 19
use this as the middle of the array
Compare a[2] with 19
15 is smaller than 19 so use the top
half for the next pass
a
Binary Search Pass 3
3
4
5
25
27
search key = 19
use this as the middle of the array
Compare a[4] with 19
25 is bigger than 19 so use the bottom
half
a
19
Binary Search Pass 4
3
search key = 19
use this as the middle of the array
Compare a[3] with 19
Found!!
a
19
Binary Search Example
1
0
1
2
3
4
5
6
7
8
9
10
11
12
5
15
19
25
27
29
31
33
45
55
88
100
middle of the array
compare a[6] and 18
18 is smaller than 29 so the next
search will use the lower half of the array
search key = 18
a
Binary Search Pass 2
1
0
1
2
3
4
5
5
15
19
25
27
search key = 18
use this as the middle of the array
Compare a[2] with 18
15 is smaller than 18 so use the top
half for the next pass
a
Binary Search Pass 3
3
4
5
25
27
search key = 18
use this as the middle of the array
Compare a[4] with 18
25 is bigger than 18 so use the bottom
half
a
19
Binary Search Pass 4
3
search key = 18
use this as the middle of the array
Compare a[3] with 18
Does not match and no more elements
to compare.
Not Found!!
a
19
const int arraySize = 13;
int a[arraySize] = {1, 2, 9, 12, 23, 44, 45, 55, 66, 77, 87, 88, 100 };
int key = 23 , low = 0, middle, high = (arraySize - 1);
while (low <= high)
{
middle = (low + high) / 2;
if (key = = a[middle])
{
cout << “Found element “ << key << “ at index “ << middle << endl;
break;
}
else if (key < a [middle])
high = middle -1; // search low end of array
else
low = middle + 1; // search high end of array
}
Efficiency
Searching an array of 1024 elements will take at most
10 passes to find a match or determine that the
element does not exist in the array.
512, 256, 128, 64, 32, 16, 8, 4, 2, 1
An array of one billion elements takes a maximum of
30 comparisons.
The bigger the array the better a binary search is as
compared to a linear search
Exercises
1) Write a function prototype for a function that does a bubble
sort named BubbleSort.
2) Write a function prototype for a function that does a binary
search named BinarySearch. The return value for BinarySearch
is of type int. If the function successfully finds the key for which
it is searching the return value contains the index in the array of
the element that matches the key.
If the function does not find the key for which it is searching the
return value contains a -1
3) Write a main program that initializes an array with data that is
out of order, calls the BubbleSort function to sort it and then
calls the BinarySearch function to find an element in the array.

More Related Content

Similar to 1 class linear and Binary search (3).ppt

advanced searching and sorting.pdf
advanced searching and sorting.pdfadvanced searching and sorting.pdf
advanced searching and sorting.pdfharamaya university
 
MODULE 5-Searching and-sorting
MODULE 5-Searching and-sortingMODULE 5-Searching and-sorting
MODULE 5-Searching and-sortingnikshaikh786
 
Searching and sorting by B kirron Reddi
Searching and sorting by B kirron ReddiSearching and sorting by B kirron Reddi
Searching and sorting by B kirron ReddiB.Kirron Reddi
 
data structures and algorithms Unit 3
data structures and algorithms Unit 3data structures and algorithms Unit 3
data structures and algorithms Unit 3infanciaj
 
Algorithm 8th lecture linear & binary search(2).pptx
Algorithm 8th lecture linear & binary search(2).pptxAlgorithm 8th lecture linear & binary search(2).pptx
Algorithm 8th lecture linear & binary search(2).pptxAftabali702240
 
Binary search Algorithm
Binary search AlgorithmBinary search Algorithm
Binary search AlgorithmFazalRehman79
 
Ocw chp6 2searchbinary
Ocw chp6 2searchbinaryOcw chp6 2searchbinary
Ocw chp6 2searchbinaryPrashant Rai
 
Data structures arrays
Data structures   arraysData structures   arrays
Data structures arraysmaamir farooq
 
Unit iv(dsc++)
Unit iv(dsc++)Unit iv(dsc++)
Unit iv(dsc++)Durga Devi
 
SEARCHING AND SORTING ALGORITHMS
SEARCHING AND SORTING ALGORITHMSSEARCHING AND SORTING ALGORITHMS
SEARCHING AND SORTING ALGORITHMSGokul Hari
 
Algorithm & data structures lec4&5
Algorithm & data structures lec4&5Algorithm & data structures lec4&5
Algorithm & data structures lec4&5Abdul Khan
 
Chapter 4: basic search algorithms data structure
Chapter 4: basic search algorithms data structureChapter 4: basic search algorithms data structure
Chapter 4: basic search algorithms data structureMahmoud Alfarra
 

Similar to 1 class linear and Binary search (3).ppt (20)

advanced searching and sorting.pdf
advanced searching and sorting.pdfadvanced searching and sorting.pdf
advanced searching and sorting.pdf
 
MODULE 5-Searching and-sorting
MODULE 5-Searching and-sortingMODULE 5-Searching and-sorting
MODULE 5-Searching and-sorting
 
Searching and sorting by B kirron Reddi
Searching and sorting by B kirron ReddiSearching and sorting by B kirron Reddi
Searching and sorting by B kirron Reddi
 
data structures and algorithms Unit 3
data structures and algorithms Unit 3data structures and algorithms Unit 3
data structures and algorithms Unit 3
 
Algorithm 8th lecture linear & binary search(2).pptx
Algorithm 8th lecture linear & binary search(2).pptxAlgorithm 8th lecture linear & binary search(2).pptx
Algorithm 8th lecture linear & binary search(2).pptx
 
Unit viii searching and hashing
Unit   viii searching and hashing Unit   viii searching and hashing
Unit viii searching and hashing
 
Binary search Algorithm
Binary search AlgorithmBinary search Algorithm
Binary search Algorithm
 
Ocw chp6 2searchbinary
Ocw chp6 2searchbinaryOcw chp6 2searchbinary
Ocw chp6 2searchbinary
 
Searching.ppt
Searching.pptSearching.ppt
Searching.ppt
 
Lecture3b searching
Lecture3b searchingLecture3b searching
Lecture3b searching
 
Searching
Searching Searching
Searching
 
Data structures arrays
Data structures   arraysData structures   arrays
Data structures arrays
 
Unit iv(dsc++)
Unit iv(dsc++)Unit iv(dsc++)
Unit iv(dsc++)
 
SEARCHING AND SORTING ALGORITHMS
SEARCHING AND SORTING ALGORITHMSSEARCHING AND SORTING ALGORITHMS
SEARCHING AND SORTING ALGORITHMS
 
Algorithm & data structures lec4&5
Algorithm & data structures lec4&5Algorithm & data structures lec4&5
Algorithm & data structures lec4&5
 
Chapter 4: basic search algorithms data structure
Chapter 4: basic search algorithms data structureChapter 4: basic search algorithms data structure
Chapter 4: basic search algorithms data structure
 
searching
searchingsearching
searching
 
Chapter3.pptx
Chapter3.pptxChapter3.pptx
Chapter3.pptx
 
Searching algorithms
Searching algorithmsSearching algorithms
Searching algorithms
 
Search Algprithms
Search AlgprithmsSearch Algprithms
Search Algprithms
 

Recently uploaded

OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...Soham Mondal
 
Biology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxBiology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxDeepakSakkari2
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINESIVASHANKAR N
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Dr.Costas Sachpazis
 
Analog to Digital and Digital to Analog Converter
Analog to Digital and Digital to Analog ConverterAnalog to Digital and Digital to Analog Converter
Analog to Digital and Digital to Analog ConverterAbhinavSharma374939
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024Mark Billinghurst
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSKurinjimalarL3
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxupamatechverse
 
Current Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLCurrent Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLDeelipZope
 
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...ZTE
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024hassan khalil
 
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...RajaP95
 
Internship report on mechanical engineering
Internship report on mechanical engineeringInternship report on mechanical engineering
Internship report on mechanical engineeringmalavadedarshan25
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxpurnimasatapathy1234
 
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).pptssuser5c9d4b1
 
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝soniya singh
 

Recently uploaded (20)

OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
 
Biology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxBiology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptx
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
 
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
 
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptxExploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
 
Analog to Digital and Digital to Analog Converter
Analog to Digital and Digital to Analog ConverterAnalog to Digital and Digital to Analog Converter
Analog to Digital and Digital to Analog Converter
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptx
 
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCRCall Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
 
Current Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLCurrent Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCL
 
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024
 
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
 
Internship report on mechanical engineering
Internship report on mechanical engineeringInternship report on mechanical engineering
Internship report on mechanical engineering
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptx
 
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
 
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
 

1 class linear and Binary search (3).ppt

  • 1. Unit 2 Example Linear and Binary Search Prof. D.R. Dhotre
  • 2. Searching Techniques  Linear search small arrays unsorted arrays  Binary search large arrays sorted arrays
  • 3. Linear Search Algorithm Start at first element of array. Compare value to value (key) for which you are searching Continue with next element of the array until you find a match or reach the last element in the array. Note: On the average you will have to compare the search key with half the elements in the array.
  • 4. Linear Search Code const int arraySize = 100; int a[arraySize] = {1, 100, 2, 66, 55, 44, 88, 77, 12, 23, 45, 9, 87}; int key = 88; bool found = false; for (int i = 0; i < arraySize; i++) { if (a[i] = = key) { cout << “Found it at array subscript “ << i << endl; found = true; break; } } if (! found) cout << “Could not find element “ << key << “ in array a” << endl;
  • 5. Binary Search Algorithm 1. May only be used on a sorted array. 2. Eliminates one half of the elements after each comparison. 3. Locate the middle of the array 4. Compare the value at that location with the search key. 5. If they are equal - done! 6. Otherwise, decide which half of the array contains the search key. 7. Repeat the search on that half of the array and ignore the other half. 8. The search continues until the key is matched or no elements remain to be searched.
  • 6. Binary Search Example 1 0 1 2 3 4 5 6 7 8 9 10 11 12 5 15 19 25 27 29 31 33 45 55 88 100 middle of the array compare a[6] and 19 19 is smaller than 29 so the next search will use the lower half of the array search key = 19 a
  • 7. Binary Search Pass 2 1 0 1 2 3 4 5 5 15 19 25 27 search key = 19 use this as the middle of the array Compare a[2] with 19 15 is smaller than 19 so use the top half for the next pass a
  • 8. Binary Search Pass 3 3 4 5 25 27 search key = 19 use this as the middle of the array Compare a[4] with 19 25 is bigger than 19 so use the bottom half a 19
  • 9. Binary Search Pass 4 3 search key = 19 use this as the middle of the array Compare a[3] with 19 Found!! a 19
  • 10. Binary Search Example 1 0 1 2 3 4 5 6 7 8 9 10 11 12 5 15 19 25 27 29 31 33 45 55 88 100 middle of the array compare a[6] and 18 18 is smaller than 29 so the next search will use the lower half of the array search key = 18 a
  • 11. Binary Search Pass 2 1 0 1 2 3 4 5 5 15 19 25 27 search key = 18 use this as the middle of the array Compare a[2] with 18 15 is smaller than 18 so use the top half for the next pass a
  • 12. Binary Search Pass 3 3 4 5 25 27 search key = 18 use this as the middle of the array Compare a[4] with 18 25 is bigger than 18 so use the bottom half a 19
  • 13. Binary Search Pass 4 3 search key = 18 use this as the middle of the array Compare a[3] with 18 Does not match and no more elements to compare. Not Found!! a 19
  • 14. const int arraySize = 13; int a[arraySize] = {1, 2, 9, 12, 23, 44, 45, 55, 66, 77, 87, 88, 100 }; int key = 23 , low = 0, middle, high = (arraySize - 1); while (low <= high) { middle = (low + high) / 2; if (key = = a[middle]) { cout << “Found element “ << key << “ at index “ << middle << endl; break; } else if (key < a [middle]) high = middle -1; // search low end of array else low = middle + 1; // search high end of array }
  • 15. Efficiency Searching an array of 1024 elements will take at most 10 passes to find a match or determine that the element does not exist in the array. 512, 256, 128, 64, 32, 16, 8, 4, 2, 1 An array of one billion elements takes a maximum of 30 comparisons. The bigger the array the better a binary search is as compared to a linear search
  • 16. Exercises 1) Write a function prototype for a function that does a bubble sort named BubbleSort. 2) Write a function prototype for a function that does a binary search named BinarySearch. The return value for BinarySearch is of type int. If the function successfully finds the key for which it is searching the return value contains the index in the array of the element that matches the key. If the function does not find the key for which it is searching the return value contains a -1 3) Write a main program that initializes an array with data that is out of order, calls the BubbleSort function to sort it and then calls the BinarySearch function to find an element in the array.