scalescale
Search Algorithms
Technical Writing(CS1305) Assignment
Aditya Kumar
20142028
MNNIT Allahabad
August 2015
Aditya Kumar 20142028 Search Algorithms
scalescale
Introduction
In computer science, a search algorithm is an algorithm
for finding an item with specified properties among a
collection of items which are coded into a computer
program.
The program then looks for clues to give us back exactly
what we want.
Aditya Kumar 20142028 Search Algorithms
scalescale
Introduction
In computer science, a search algorithm is an algorithm
for finding an item with specified properties among a
collection of items which are coded into a computer
program.
The program then looks for clues to give us back exactly
what we want.
We are going to cover two search algoritms in this
presentation: linear and binary.
Aditya Kumar 20142028 Search Algorithms
scalescale
Linear Search
Linear search is a method for finding a particular value in
a list that checks each element in sequence until the
desired element is found or the list is exhausted
Aditya Kumar 20142028 Search Algorithms
scalescale
Linear Search
Linear search is a method for finding a particular value in
a list that checks each element in sequence until the
desired element is found or the list is exhausted
It is the simplest search algorithm
A special case of brute-force search
Average time complexity: O(n)
Aditya Kumar 20142028 Search Algorithms
scalescale
Linear Search
continued
Figure: Graphical illustration of linear search
Aditya Kumar 20142028 Search Algorithms
scalescale
Linear Search
Pseudocode
Using Iteration
Require: list a, size of list n, desired item x
for i=0 to n-1 do
if a[i]=x then
return i
end if
i=i+1
end for
if i=n then
return false
end if
Aditya Kumar 20142028 Search Algorithms
scalescale
Linear Search
Pseudocode
Using Recursion
LinearSearch(list, size, value)
if size=0 then
return false
else if first item of list=value then
return location of first item
else
return LinearSearch(list, remaining size, value)
end if
Aditya Kumar 20142028 Search Algorithms
scalescale
Linear Search
Advantages and Disadvantages
Advantages
Easiest to understand and implement
No sorting required
Suitable for small list sizes
Aditya Kumar 20142028 Search Algorithms
scalescale
Linear Search
Advantages and Disadvantages
Advantages
Easiest to understand and implement
No sorting required
Suitable for small list sizes
Disadvantages
Time inefficient as compared to other algorithms
Not suitable for large-sized lists
Aditya Kumar 20142028 Search Algorithms
scalescale
Binary Search
Binary search algorithm finds the position of a target
value within a sorted array
Aditya Kumar 20142028 Search Algorithms
scalescale
Binary Search
Binary search algorithm finds the position of a target
value within a sorted array
It can be classified as a divide-and-conquer search
algorithm
Average time complexity: O(log n)
Aditya Kumar 20142028 Search Algorithms
scalescale
Binary Search
Overview
The algorithm begins by comparing the target value to
the value of the middle element of the sorted array
Aditya Kumar 20142028 Search Algorithms
scalescale
Binary Search
Overview
The algorithm begins by comparing the target value to
the value of the middle element of the sorted array
If they are equal the middle position is returned and the
search is finished
Aditya Kumar 20142028 Search Algorithms
scalescale
Binary Search
Overview
The algorithm begins by comparing the target value to
the value of the middle element of the sorted array
If they are equal the middle position is returned and the
search is finished
If the target value is less than the middle element’s value,
then the search continues on the lower half of the array;
or if the target value is greater than the middle element’s
value, then the search continues on the upper half of the
array
Aditya Kumar 20142028 Search Algorithms
scalescale
Binary Search
Overview
The algorithm begins by comparing the target value to
the value of the middle element of the sorted array
If they are equal the middle position is returned and the
search is finished
If the target value is less than the middle element’s value,
then the search continues on the lower half of the array;
or if the target value is greater than the middle element’s
value, then the search continues on the upper half of the
array
This process continues, eliminating half of the elements
until the value is found or the array is exhausted
Aditya Kumar 20142028 Search Algorithms
scalescale
Binary Search
continued
Figure: Graphical illustration of binary search
Aditya Kumar 20142028 Search Algorithms
scalescale
Binary Search
Pseudocode (using recursion)
BinarySearch(list[], min, max, key)
if max <min then
return false
else
mid = (max+min) / 2
if list[mid] >key then
return BinarySearch(list[], min, mid-1, key)
else if list[mid] <key then
return BinarySearch(list[], mid+1, max, key)
else
return mid
end if
end if
Aditya Kumar 20142028 Search Algorithms
scalescale
Binary Search
Pseudocode (using iteration)
BinarySearch(list[], min, max, key)
while min ≤ max do
mid = (max+min) / 2
if list[mid] >key then
max = mid-1
else if list[mid] <key then
min = mid+1
else
return mid
end if
end while
return false
Aditya Kumar 20142028 Search Algorithms
scalescale
Binary Search
Performance
With each test that fails to find a match, the search is
continued with one or other of the two sub-intervals, each
at most half the size
Aditya Kumar 20142028 Search Algorithms
scalescale
Binary Search
Performance
With each test that fails to find a match, the search is
continued with one or other of the two sub-intervals, each
at most half the size
If the original number of items is N then after the first
iteration there will be at most N/2 items remaining, then
at most N/4 items, and so on
Aditya Kumar 20142028 Search Algorithms
scalescale
Binary Search
Performance
With each test that fails to find a match, the search is
continued with one or other of the two sub-intervals, each
at most half the size
If the original number of items is N then after the first
iteration there will be at most N/2 items remaining, then
at most N/4 items, and so on
In the worst case, when the value is not in the list, the
algorithm must continue iterating until the list is empty;
this take at most floor(log2 N + 1) iterations, where
floor() rounds down its argument to an integer
Aditya Kumar 20142028 Search Algorithms
scalescale
Binary Search
Performance
With each test that fails to find a match, the search is
continued with one or other of the two sub-intervals, each
at most half the size
If the original number of items is N then after the first
iteration there will be at most N/2 items remaining, then
at most N/4 items, and so on
In the worst case, when the value is not in the list, the
algorithm must continue iterating until the list is empty;
this take at most floor(log2 N + 1) iterations, where
floor() rounds down its argument to an integer
Thus binary search runs in logarithmic time
Aditya Kumar 20142028 Search Algorithms
scalescale
Binary Search
Advantages and Disadvantages
Advantages
Excellent time efficiency
Suitable for large list sizes
Aditya Kumar 20142028 Search Algorithms
scalescale
Binary Search
Advantages and Disadvantages
Advantages
Excellent time efficiency
Suitable for large list sizes
Disadvantages
Can only be implemented on a sorted array
Not suitable for small sizes, where linear search would be
faster, as sorting takes up a considerable amount of time
Aditya Kumar 20142028 Search Algorithms
scalescale
Binary Search
Language Support
Many standard libraries provide a way to do a binary search:
C provides bsearch() in its standard library
C++’s STL provides the functions binary search(),
lower bound() and upper bound()
Java offers a set of overloaded binarySearch() static
methods in the class Arrays in the standard java.util
package for performing binary searches on Java arrays
Python provides the bisect module
Aditya Kumar 20142028 Search Algorithms
scalescale
End
Thank you
Aditya Kumar 20142028 Search Algorithms

More Related Content

PPTX
Presentation
PDF
linear search and binary search
PPTX
Searching techniques
PDF
Searching and Sorting Techniques in Data Structure
PPT
Searching algorithms
PPTX
Linear search-and-binary-search
PPT
Ch05 Black Jack
PDF
Binary search algorithm
Presentation
linear search and binary search
Searching techniques
Searching and Sorting Techniques in Data Structure
Searching algorithms
Linear search-and-binary-search
Ch05 Black Jack
Binary search algorithm

What's hot (20)

PDF
searching
PPTX
Binary search python
PPT
Searching algorithm
PPTX
Binary search
PPSX
Algorithm and Programming (Searching)
PPTX
Dsa – data structure and algorithms sorting
PPTX
Searching
PPTX
Rahat &amp; juhith
ODP
Data operatons & searching and sorting algorithms
PPTX
Binary search
PPTX
Searching linear &amp; binary search
PPT
L10 sorting-searching
PPT
Binary Search
PPTX
Searching techniques in Data Structure And Algorithm
PPTX
Sequential & binary, linear search
PPTX
Search methods
PPTX
Standard algorithms
PPTX
Dsa – data structure and algorithms searching
PPT
Chap10
PDF
Binary Search - Design & Analysis of Algorithms
searching
Binary search python
Searching algorithm
Binary search
Algorithm and Programming (Searching)
Dsa – data structure and algorithms sorting
Searching
Rahat &amp; juhith
Data operatons & searching and sorting algorithms
Binary search
Searching linear &amp; binary search
L10 sorting-searching
Binary Search
Searching techniques in Data Structure And Algorithm
Sequential & binary, linear search
Search methods
Standard algorithms
Dsa – data structure and algorithms searching
Chap10
Binary Search - Design & Analysis of Algorithms
Ad

Viewers also liked (17)

PDF
DISMATH_Part2
PDF
Plenary Speaker slides at the 2016 International Workshop on Biodesign Automa...
PDF
Elementary Landscape Decomposition of Combinatorial Optimization Problems
PDF
Paper introduction to Combinatorial Optimization on Graphs of Bounded Treewidth
PDF
DENSA:An effective negative selection algorithm with flexible boundaries for ...
PPTX
Local search algorithms
PPTX
Search algorithms for discrete optimization
PDF
DISMATH_Intro_Admin
DOC
Data structures question paper anna university
PDF
2 lectures 16 17-informed search algorithms ch 4.3
PPTX
Data structures and algorithms
PPT
Linear Search & Binary Search
PDF
Linear search algorithm
PDF
Chemical Reactions
PPTX
Data Structure
PPTX
The Search Landscape in 2017
PPT
Landscape Design and Principles
DISMATH_Part2
Plenary Speaker slides at the 2016 International Workshop on Biodesign Automa...
Elementary Landscape Decomposition of Combinatorial Optimization Problems
Paper introduction to Combinatorial Optimization on Graphs of Bounded Treewidth
DENSA:An effective negative selection algorithm with flexible boundaries for ...
Local search algorithms
Search algorithms for discrete optimization
DISMATH_Intro_Admin
Data structures question paper anna university
2 lectures 16 17-informed search algorithms ch 4.3
Data structures and algorithms
Linear Search & Binary Search
Linear search algorithm
Chemical Reactions
Data Structure
The Search Landscape in 2017
Landscape Design and Principles
Ad

Similar to Search Algprithms (20)

PPTX
Searching & Algorithms IN DATA STRUCTURES
PPTX
Analysis of Algorithm - Binary Search.pptx
PPTX
Lecture_Oct26.pptx
PPT
Chapter 11 ds
PPTX
data_structure_Chapter two_computer.pptx
PDF
IRJET- A Survey on Different Searching Algorithms
PPTX
Data Structures Algorithms - Week 3c - Basic Searching Algorithms.pptx
PPT
cs702 ppt.ppt
PPT
Searching
PPTX
Binary Search.pptx
PPTX
Amitdsppt
PDF
L1803016468
PDF
slidesgo-mastering-binary-search-efficient-algorithms-for-fast-data-retrieval...
PPTX
Binary.pptx
PPTX
Binary Search
PPTX
Chapter #3 (Searchinmmmg ALgorithm).pptx
DOCX
MODULE 5-Searching and-sorting
PDF
PPTX
Searching in Data Structure
PDF
BINARY SEARCH - A - DATA STRUCTURE AND ALGORITHM.pdf
Searching & Algorithms IN DATA STRUCTURES
Analysis of Algorithm - Binary Search.pptx
Lecture_Oct26.pptx
Chapter 11 ds
data_structure_Chapter two_computer.pptx
IRJET- A Survey on Different Searching Algorithms
Data Structures Algorithms - Week 3c - Basic Searching Algorithms.pptx
cs702 ppt.ppt
Searching
Binary Search.pptx
Amitdsppt
L1803016468
slidesgo-mastering-binary-search-efficient-algorithms-for-fast-data-retrieval...
Binary.pptx
Binary Search
Chapter #3 (Searchinmmmg ALgorithm).pptx
MODULE 5-Searching and-sorting
Searching in Data Structure
BINARY SEARCH - A - DATA STRUCTURE AND ALGORITHM.pdf

More from Shivam Singh (7)

PDF
Stacks
PDF
PPTX
What If Microsoft sold diapers!? MS Diapers!
PDF
Binary Search Tree
PDF
Graph Theory
PDF
Linked List
PDF
Sorting Algorithms
Stacks
What If Microsoft sold diapers!? MS Diapers!
Binary Search Tree
Graph Theory
Linked List
Sorting Algorithms

Recently uploaded (20)

PDF
ASPEN PLUS USER GUIDE - PROCESS SIMULATIONS
PPTX
MAD Unit - 3 User Interface and Data Management (Diploma IT)
PPTX
INTERNET OF THINGS - EMBEDDED SYSTEMS AND INTERNET OF THINGS
PDF
Project_Mgmt_Institute_-Marc Marc Marc .pdf
PDF
MLpara ingenieira CIVIL, meca Y AMBIENTAL
PDF
Micro 4 New.ppt.pdf a servay of cells and microorganism
PPTX
Wireless sensor networks (WSN) SRM unit 2
DOCX
An investigation of the use of recycled crumb rubber as a partial replacement...
PPT
Programmable Logic Controller PLC and Industrial Automation
PDF
AIGA 012_04 Cleaning of equipment for oxygen service_reformat Jan 12.pdf
PPT
UNIT-I Machine Learning Essentials for 2nd years
PDF
Principles of operation, construction, theory, advantages and disadvantages, ...
PDF
[jvmmeetup] next-gen integration with apache camel and quarkus.pdf
PPTX
WN UNIT-II CH4_MKaruna_BapatlaEngineeringCollege.pptx
PDF
Micro 3 New.ppt.pdf tools the laboratory the method
PPTX
Solar energy pdf of gitam songa hemant k
PDF
UEFA_Embodied_Carbon_Emissions_Football_Infrastructure.pdf
PPTX
Micro1New.ppt.pptx the main themes if micro
PPTX
SE unit 1.pptx aaahshdhajdviwhsiehebeiwheiebeiev
PPTX
BBOC407 BIOLOGY FOR ENGINEERS (CS) - MODULE 1 PART 1.pptx
ASPEN PLUS USER GUIDE - PROCESS SIMULATIONS
MAD Unit - 3 User Interface and Data Management (Diploma IT)
INTERNET OF THINGS - EMBEDDED SYSTEMS AND INTERNET OF THINGS
Project_Mgmt_Institute_-Marc Marc Marc .pdf
MLpara ingenieira CIVIL, meca Y AMBIENTAL
Micro 4 New.ppt.pdf a servay of cells and microorganism
Wireless sensor networks (WSN) SRM unit 2
An investigation of the use of recycled crumb rubber as a partial replacement...
Programmable Logic Controller PLC and Industrial Automation
AIGA 012_04 Cleaning of equipment for oxygen service_reformat Jan 12.pdf
UNIT-I Machine Learning Essentials for 2nd years
Principles of operation, construction, theory, advantages and disadvantages, ...
[jvmmeetup] next-gen integration with apache camel and quarkus.pdf
WN UNIT-II CH4_MKaruna_BapatlaEngineeringCollege.pptx
Micro 3 New.ppt.pdf tools the laboratory the method
Solar energy pdf of gitam songa hemant k
UEFA_Embodied_Carbon_Emissions_Football_Infrastructure.pdf
Micro1New.ppt.pptx the main themes if micro
SE unit 1.pptx aaahshdhajdviwhsiehebeiwheiebeiev
BBOC407 BIOLOGY FOR ENGINEERS (CS) - MODULE 1 PART 1.pptx

Search Algprithms

  • 1. scalescale Search Algorithms Technical Writing(CS1305) Assignment Aditya Kumar 20142028 MNNIT Allahabad August 2015 Aditya Kumar 20142028 Search Algorithms
  • 2. scalescale Introduction In computer science, a search algorithm is an algorithm for finding an item with specified properties among a collection of items which are coded into a computer program. The program then looks for clues to give us back exactly what we want. Aditya Kumar 20142028 Search Algorithms
  • 3. scalescale Introduction In computer science, a search algorithm is an algorithm for finding an item with specified properties among a collection of items which are coded into a computer program. The program then looks for clues to give us back exactly what we want. We are going to cover two search algoritms in this presentation: linear and binary. Aditya Kumar 20142028 Search Algorithms
  • 4. scalescale Linear Search Linear search is a method for finding a particular value in a list that checks each element in sequence until the desired element is found or the list is exhausted Aditya Kumar 20142028 Search Algorithms
  • 5. scalescale Linear Search Linear search is a method for finding a particular value in a list that checks each element in sequence until the desired element is found or the list is exhausted It is the simplest search algorithm A special case of brute-force search Average time complexity: O(n) Aditya Kumar 20142028 Search Algorithms
  • 6. scalescale Linear Search continued Figure: Graphical illustration of linear search Aditya Kumar 20142028 Search Algorithms
  • 7. scalescale Linear Search Pseudocode Using Iteration Require: list a, size of list n, desired item x for i=0 to n-1 do if a[i]=x then return i end if i=i+1 end for if i=n then return false end if Aditya Kumar 20142028 Search Algorithms
  • 8. scalescale Linear Search Pseudocode Using Recursion LinearSearch(list, size, value) if size=0 then return false else if first item of list=value then return location of first item else return LinearSearch(list, remaining size, value) end if Aditya Kumar 20142028 Search Algorithms
  • 9. scalescale Linear Search Advantages and Disadvantages Advantages Easiest to understand and implement No sorting required Suitable for small list sizes Aditya Kumar 20142028 Search Algorithms
  • 10. scalescale Linear Search Advantages and Disadvantages Advantages Easiest to understand and implement No sorting required Suitable for small list sizes Disadvantages Time inefficient as compared to other algorithms Not suitable for large-sized lists Aditya Kumar 20142028 Search Algorithms
  • 11. scalescale Binary Search Binary search algorithm finds the position of a target value within a sorted array Aditya Kumar 20142028 Search Algorithms
  • 12. scalescale Binary Search Binary search algorithm finds the position of a target value within a sorted array It can be classified as a divide-and-conquer search algorithm Average time complexity: O(log n) Aditya Kumar 20142028 Search Algorithms
  • 13. scalescale Binary Search Overview The algorithm begins by comparing the target value to the value of the middle element of the sorted array Aditya Kumar 20142028 Search Algorithms
  • 14. scalescale Binary Search Overview The algorithm begins by comparing the target value to the value of the middle element of the sorted array If they are equal the middle position is returned and the search is finished Aditya Kumar 20142028 Search Algorithms
  • 15. scalescale Binary Search Overview The algorithm begins by comparing the target value to the value of the middle element of the sorted array If they are equal the middle position is returned and the search is finished If the target value is less than the middle element’s value, then the search continues on the lower half of the array; or if the target value is greater than the middle element’s value, then the search continues on the upper half of the array Aditya Kumar 20142028 Search Algorithms
  • 16. scalescale Binary Search Overview The algorithm begins by comparing the target value to the value of the middle element of the sorted array If they are equal the middle position is returned and the search is finished If the target value is less than the middle element’s value, then the search continues on the lower half of the array; or if the target value is greater than the middle element’s value, then the search continues on the upper half of the array This process continues, eliminating half of the elements until the value is found or the array is exhausted Aditya Kumar 20142028 Search Algorithms
  • 17. scalescale Binary Search continued Figure: Graphical illustration of binary search Aditya Kumar 20142028 Search Algorithms
  • 18. scalescale Binary Search Pseudocode (using recursion) BinarySearch(list[], min, max, key) if max <min then return false else mid = (max+min) / 2 if list[mid] >key then return BinarySearch(list[], min, mid-1, key) else if list[mid] <key then return BinarySearch(list[], mid+1, max, key) else return mid end if end if Aditya Kumar 20142028 Search Algorithms
  • 19. scalescale Binary Search Pseudocode (using iteration) BinarySearch(list[], min, max, key) while min ≤ max do mid = (max+min) / 2 if list[mid] >key then max = mid-1 else if list[mid] <key then min = mid+1 else return mid end if end while return false Aditya Kumar 20142028 Search Algorithms
  • 20. scalescale Binary Search Performance With each test that fails to find a match, the search is continued with one or other of the two sub-intervals, each at most half the size Aditya Kumar 20142028 Search Algorithms
  • 21. scalescale Binary Search Performance With each test that fails to find a match, the search is continued with one or other of the two sub-intervals, each at most half the size If the original number of items is N then after the first iteration there will be at most N/2 items remaining, then at most N/4 items, and so on Aditya Kumar 20142028 Search Algorithms
  • 22. scalescale Binary Search Performance With each test that fails to find a match, the search is continued with one or other of the two sub-intervals, each at most half the size If the original number of items is N then after the first iteration there will be at most N/2 items remaining, then at most N/4 items, and so on In the worst case, when the value is not in the list, the algorithm must continue iterating until the list is empty; this take at most floor(log2 N + 1) iterations, where floor() rounds down its argument to an integer Aditya Kumar 20142028 Search Algorithms
  • 23. scalescale Binary Search Performance With each test that fails to find a match, the search is continued with one or other of the two sub-intervals, each at most half the size If the original number of items is N then after the first iteration there will be at most N/2 items remaining, then at most N/4 items, and so on In the worst case, when the value is not in the list, the algorithm must continue iterating until the list is empty; this take at most floor(log2 N + 1) iterations, where floor() rounds down its argument to an integer Thus binary search runs in logarithmic time Aditya Kumar 20142028 Search Algorithms
  • 24. scalescale Binary Search Advantages and Disadvantages Advantages Excellent time efficiency Suitable for large list sizes Aditya Kumar 20142028 Search Algorithms
  • 25. scalescale Binary Search Advantages and Disadvantages Advantages Excellent time efficiency Suitable for large list sizes Disadvantages Can only be implemented on a sorted array Not suitable for small sizes, where linear search would be faster, as sorting takes up a considerable amount of time Aditya Kumar 20142028 Search Algorithms
  • 26. scalescale Binary Search Language Support Many standard libraries provide a way to do a binary search: C provides bsearch() in its standard library C++’s STL provides the functions binary search(), lower bound() and upper bound() Java offers a set of overloaded binarySearch() static methods in the class Arrays in the standard java.util package for performing binary searches on Java arrays Python provides the bisect module Aditya Kumar 20142028 Search Algorithms
  • 27. scalescale End Thank you Aditya Kumar 20142028 Search Algorithms