KEMBAR78
linear search and binary search | PDF
First Year BA (IT)
CT1120 Algorithms
Lecture 13
Dr. Zia Ush Shamszaman
z.shamszaman1@nuigalway.ie
1
School of Computer Science, College of Science and Engineering
17-01-2020
Information
•  Number of Lectures -12
–  When? Every Friday (12-1)
–  Where: AC201
17-01-2020 2
Overview
•  Flashback
•  Introduction
•  Linear Search
•  Binary Search
•  Feedback and Assessment
317-01-2020
Introduction
•  The definition of a search is the process of
looking for something.
•  In computer science
–  A search algorithm is an algorithm for finding
an item with specified properties among a
collection of items that coded into a computer
program.
17-01-2020 4
Linear Search (LS)
Linear Search involves checking all the
elements of the array (or any other
structure) one by one and in sequence
until the desired result is found.
17-01-2020 5
Graphical Illustration of LS
17-01-2020 6
Every item is checked but no match is found till the
end of the data collection
Graphical Illustration of LS
17-01-2020 7
Found a match at index 2
Linear Search Algorithm
•  Linear Search ( Array A, Value x)
•  Step 1: Set i to 1
•  Step 2: if i > n then go to step 7
•  Step 3: if A[i] = x then go to step 6
•  Step 4: Set i to i + 1
•  Step 5: Go to Step 2
•  Step 6: Print Element x Found at index i and go to step 8
•  Step 7: Print element not found
•  Step 8: Exit
17-01-2020 8
Pseudocode
•  procedure linear_search (list, value)
for each item in the list
if match item == value
return the item's location
end if
end for
end procedure
17-01-2020 9
Adv. & Disadv. Of LS
•  Advantages
–  Easiest to understand and implement
–  No sorting required
–  Suitable for small list sizes
–  Works fine for small number of elements
•  Disadvantages
–  Time inefficient as compared to other algorithms
–  Not suitable for large-sized lists
–  Search time increases with number of elements
17-01-2020 10
Binary Search (BS)
•  Binary Search is a Divide and Conquer algorithm
•  Binary search algorithm finds the position of a target value
within a sorted array
•  A more efficient approach than Linear Search because
Binary Search basically reduces the search space to half at
each step
17-01-2020 11
Binary Search
•  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;
•  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
17-01-2020 12
Graphical Illustration of BS
17-01-2020 13
Graphical Illustration of BS
17-01-2020 14
Graphical Illustration of BS
17-01-2020 15
Graphical Illustration of BS
17-01-2020 16
Graphical Illustration of BS
17-01-2020 17
Graphical Illustration of BS
17-01-2020 18
Binary Search
•  With each test that fails to and 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
17-01-2020 19
Pseudocode
17-01-2020 20
Procedure binary_search
A ← sorted array
n ← size of array
x ← value to be searched
Set lowerBound = 1
Set upperBound = n
while x not found
if upperBound < lowerBound
EXIT: x does not
exists.
set midPoint =
lowerBound + ( upperBound -lowerBound )/2
if A[midPoint] < x
set lowerBound = midPoint + 1
if A[midPoint] > x
set upperBound = midPoint - 1
if A[midPoint] = x
EXIT: x found at location midPoint
end while
end procedure
Binary Search Algorithm
•  Step 1 − Start searching data from middle of the list.
•  Step 2 − If it is a match, return the index of the item,
and exit.
•  Step 3 − If it is not a match, probe position.
•  Step 4 − Divide the list and find the new middle.
•  Step 5 − If data is greater than middle, search in
higher sub-list.
•  Step 6 − If data is smaller than middle, search in lower
sub-list.
•  Step 7 − Repeat until match.
17-01-2020 21
Next Lecture
•  Binary Search
•  Sorting
17-01-2020 22
References
•  https://www.tutorialspoint.com/
•  https://www.hackerearth.com/
•  www.khanacademy.org/computing/computer-science/algorithms
17-01-2020 23
Feedback & Assessment
17-01-2020 24

linear search and binary search

  • 1.
    First Year BA(IT) CT1120 Algorithms Lecture 13 Dr. Zia Ush Shamszaman z.shamszaman1@nuigalway.ie 1 School of Computer Science, College of Science and Engineering 17-01-2020
  • 2.
    Information •  Number ofLectures -12 –  When? Every Friday (12-1) –  Where: AC201 17-01-2020 2
  • 3.
    Overview •  Flashback •  Introduction • Linear Search •  Binary Search •  Feedback and Assessment 317-01-2020
  • 4.
    Introduction •  The definitionof a search is the process of looking for something. •  In computer science –  A search algorithm is an algorithm for finding an item with specified properties among a collection of items that coded into a computer program. 17-01-2020 4
  • 5.
    Linear Search (LS) LinearSearch involves checking all the elements of the array (or any other structure) one by one and in sequence until the desired result is found. 17-01-2020 5
  • 6.
    Graphical Illustration ofLS 17-01-2020 6 Every item is checked but no match is found till the end of the data collection
  • 7.
    Graphical Illustration ofLS 17-01-2020 7 Found a match at index 2
  • 8.
    Linear Search Algorithm • Linear Search ( Array A, Value x) •  Step 1: Set i to 1 •  Step 2: if i > n then go to step 7 •  Step 3: if A[i] = x then go to step 6 •  Step 4: Set i to i + 1 •  Step 5: Go to Step 2 •  Step 6: Print Element x Found at index i and go to step 8 •  Step 7: Print element not found •  Step 8: Exit 17-01-2020 8
  • 9.
    Pseudocode •  procedure linear_search(list, value) for each item in the list if match item == value return the item's location end if end for end procedure 17-01-2020 9
  • 10.
    Adv. & Disadv.Of LS •  Advantages –  Easiest to understand and implement –  No sorting required –  Suitable for small list sizes –  Works fine for small number of elements •  Disadvantages –  Time inefficient as compared to other algorithms –  Not suitable for large-sized lists –  Search time increases with number of elements 17-01-2020 10
  • 11.
    Binary Search (BS) • Binary Search is a Divide and Conquer algorithm •  Binary search algorithm finds the position of a target value within a sorted array •  A more efficient approach than Linear Search because Binary Search basically reduces the search space to half at each step 17-01-2020 11
  • 12.
    Binary Search •  Thealgorithm 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; •  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 17-01-2020 12
  • 13.
    Graphical Illustration ofBS 17-01-2020 13
  • 14.
    Graphical Illustration ofBS 17-01-2020 14
  • 15.
    Graphical Illustration ofBS 17-01-2020 15
  • 16.
    Graphical Illustration ofBS 17-01-2020 16
  • 17.
    Graphical Illustration ofBS 17-01-2020 17
  • 18.
    Graphical Illustration ofBS 17-01-2020 18
  • 19.
    Binary Search •  Witheach test that fails to and 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 17-01-2020 19
  • 20.
    Pseudocode 17-01-2020 20 Procedure binary_search A← sorted array n ← size of array x ← value to be searched Set lowerBound = 1 Set upperBound = n while x not found if upperBound < lowerBound EXIT: x does not exists. set midPoint = lowerBound + ( upperBound -lowerBound )/2 if A[midPoint] < x set lowerBound = midPoint + 1 if A[midPoint] > x set upperBound = midPoint - 1 if A[midPoint] = x EXIT: x found at location midPoint end while end procedure
  • 21.
    Binary Search Algorithm • Step 1 − Start searching data from middle of the list. •  Step 2 − If it is a match, return the index of the item, and exit. •  Step 3 − If it is not a match, probe position. •  Step 4 − Divide the list and find the new middle. •  Step 5 − If data is greater than middle, search in higher sub-list. •  Step 6 − If data is smaller than middle, search in lower sub-list. •  Step 7 − Repeat until match. 17-01-2020 21
  • 22.
    Next Lecture •  BinarySearch •  Sorting 17-01-2020 22
  • 23.
    References •  https://www.tutorialspoint.com/ •  https://www.hackerearth.com/ • www.khanacademy.org/computing/computer-science/algorithms 17-01-2020 23
  • 24.