CSE214: Algorithms
Searching Algorithms
Amir Sohel
Lecturer, CSE
Department of CSE, DIU
1
Today’s Contents
• Different Types of Algorithms Algorithm?
• Searching Algorithms
• Complexity Analysis of Searching Algorithms
• Questions for You!
2
Algorithms we are going to study
1. Searching Algorithms
2. Sorting Algorithms
3. Greedy Approaches
4. Dynamic Programming
5. Graph Algorithms
3
Searching Algorithms
4
Searching Algorithms
5
▪ Search for a target data from a data set.
▪ Searching Algorithms are designed to check for an
element or retrieve an element from any data structure
where it is stored.
▪ Two possible outcomes
▪ Target data is Found (Success)
▪ Target data is not Found (Failure)
Types of Searching Algorithms
6
▪ Based on the type of search operation, two popular
algorithms available:
1. Linear Search / Sequential Search
2. Binary Search
1. Linear Search
7
▪ It is also known as Sequential Search
▪ Linear search is a very basic and simple search algorithm.
▪ The data list or array is traversed sequentially and every
element is checked.
Simulation of Linear Search
8
Check each and every data in the list till the desired element
or value is found.
Suppose, we want to search 33 from the given array, Searching
will start from the first index and stop searching if the data is
found or the list is over.
Algorithm of Linear Search
9
Algorithm:
i = 1
while i < n && Z != X[i] do
i = i+1
if i < n then
FOUND
else
NOT FOUND
flag = FALSE
for i = 1 to n
if A[i] == key
flag = TRUE;
if flag == TRUE
FOUND
else
NOT FOUND
Complexity Analysis of Linear Search
10
Algorithm:
flag = FALSE
for i = 1 to n
if A[i] == key
flag = TRUE;
if flag == TRUE
FOUND
else
NOT FOUND
• Best case: O(1)
• Worst Case: O(n)
Features of Linear Search Algorithm
11
▪ It is used for unsorted and unordered small list of
elements.
▪ It has a time complexity of O(n), which means the time is
linearly dependent on the number of elements, which is
not bad, but not that good too.
▪ It has a very simple implementation.
2. Binary Search
12
▪ Binary Search is used with sorted array or list.
▪ In binary search, we follow the following steps:
– We start by comparing the element to be searched with the element in the
middle of the list/array.
– If we get a match, we return the index of the middle element.
– If we do not get a match, we check whether the element to be searched is less
or greater than in value than the middle element.
– If the element/number to be searched is greater in value than the middle
number, then we pick the elements on the right side of the middle element (as
the list/array is sorted, hence on the right, we will have all the numbers greater
than the middle number), and start again from the step 1.
– If the element/number to be searched is lesser in value than the middle number,
then we pick the elements on the left side of the middle element, and start again
from the step 1.
13
Basic Idea of Binary Search
14
15
16
Algorithm of Binary Search
17
Algorithm: low = 1 //[Start position]
high = n //[Last position]
flag = false
while low <= high and flag = = false do
mid = (l+h) / 2
Xm = A[mid]
case:
Xm < Z : low = mid + 1
Xm > Z : high = mid - 1
Xm = = Z : flag = true
if flag = = true
FOUND
else
NOT FOUND
18
low = 0
high = n-1
flag = false
while low < high and flag = = false do
mid = (low +high) / 2
Xm = A[mid]
case:
Xm < Z : low = mid
+ 1
Xm > Z : high = mid
- 1
Xm = = Z : flag = true
if flag = = true
FOUND
else
NOT FOUND
Step-1: mid = (low + high ) / 2 = (0 + 8) /2 = 4
A[mid] = 45 < 55
low = mid + 1 = 5
mid low
Step-2: mid = (low + high ) / 2 = (5 + 8) /2 = 6
A[mid] = 51 < 55
low = mid + 1 = 7
low
Step-3: mid = (low + high ) / 2 = (7 + 8) /2 = 7
A[mid] = 55 == 55
found
mid
mid
Search 55
Complexity Analysis of Binary Search
19
• Best case:
• Worst Case:
Features of Binary Search
20
▪ It is great to search through large sorted arrays.
▪ It has a time complexity of O(log n) which is a very good
time complexity.
▪ It has also a simple implementation.
21
22
Complexity Analysis of Two Algorithms
23
Algorithm Best case Worst case
Linear search O(1) O(N)
Binary search O(1) O(log N)
• If we have an array of length 70000000 but sorted, which
searching algorithm will work fast and why??
• Which Searching Algorithm is followed to search any data
from a Dictionary?
• Search item 75 using binary search algorithm from the
following list.
A= [22, 3, 127, 515, 163, 69, 75, 88, 96, 10]
24
Try to answer!
Search 30 from the list using Binary Search. Show
each step.
25
Try to answer!
10 15 21 25 30 40 51 60
26
Thank you!

Searching Algorithms - Foundations of Algorithms

  • 1.
    CSE214: Algorithms Searching Algorithms AmirSohel Lecturer, CSE Department of CSE, DIU 1
  • 2.
    Today’s Contents • DifferentTypes of Algorithms Algorithm? • Searching Algorithms • Complexity Analysis of Searching Algorithms • Questions for You! 2
  • 3.
    Algorithms we aregoing to study 1. Searching Algorithms 2. Sorting Algorithms 3. Greedy Approaches 4. Dynamic Programming 5. Graph Algorithms 3
  • 4.
  • 5.
    Searching Algorithms 5 ▪ Searchfor a target data from a data set. ▪ Searching Algorithms are designed to check for an element or retrieve an element from any data structure where it is stored. ▪ Two possible outcomes ▪ Target data is Found (Success) ▪ Target data is not Found (Failure)
  • 6.
    Types of SearchingAlgorithms 6 ▪ Based on the type of search operation, two popular algorithms available: 1. Linear Search / Sequential Search 2. Binary Search
  • 7.
    1. Linear Search 7 ▪It is also known as Sequential Search ▪ Linear search is a very basic and simple search algorithm. ▪ The data list or array is traversed sequentially and every element is checked.
  • 8.
    Simulation of LinearSearch 8 Check each and every data in the list till the desired element or value is found. Suppose, we want to search 33 from the given array, Searching will start from the first index and stop searching if the data is found or the list is over.
  • 9.
    Algorithm of LinearSearch 9 Algorithm: i = 1 while i < n && Z != X[i] do i = i+1 if i < n then FOUND else NOT FOUND flag = FALSE for i = 1 to n if A[i] == key flag = TRUE; if flag == TRUE FOUND else NOT FOUND
  • 10.
    Complexity Analysis ofLinear Search 10 Algorithm: flag = FALSE for i = 1 to n if A[i] == key flag = TRUE; if flag == TRUE FOUND else NOT FOUND • Best case: O(1) • Worst Case: O(n)
  • 11.
    Features of LinearSearch Algorithm 11 ▪ It is used for unsorted and unordered small list of elements. ▪ It has a time complexity of O(n), which means the time is linearly dependent on the number of elements, which is not bad, but not that good too. ▪ It has a very simple implementation.
  • 12.
    2. Binary Search 12 ▪Binary Search is used with sorted array or list. ▪ In binary search, we follow the following steps: – We start by comparing the element to be searched with the element in the middle of the list/array. – If we get a match, we return the index of the middle element. – If we do not get a match, we check whether the element to be searched is less or greater than in value than the middle element. – If the element/number to be searched is greater in value than the middle number, then we pick the elements on the right side of the middle element (as the list/array is sorted, hence on the right, we will have all the numbers greater than the middle number), and start again from the step 1. – If the element/number to be searched is lesser in value than the middle number, then we pick the elements on the left side of the middle element, and start again from the step 1.
  • 13.
    13 Basic Idea ofBinary Search
  • 14.
  • 15.
  • 16.
  • 17.
    Algorithm of BinarySearch 17 Algorithm: low = 1 //[Start position] high = n //[Last position] flag = false while low <= high and flag = = false do mid = (l+h) / 2 Xm = A[mid] case: Xm < Z : low = mid + 1 Xm > Z : high = mid - 1 Xm = = Z : flag = true if flag = = true FOUND else NOT FOUND
  • 18.
    18 low = 0 high= n-1 flag = false while low < high and flag = = false do mid = (low +high) / 2 Xm = A[mid] case: Xm < Z : low = mid + 1 Xm > Z : high = mid - 1 Xm = = Z : flag = true if flag = = true FOUND else NOT FOUND Step-1: mid = (low + high ) / 2 = (0 + 8) /2 = 4 A[mid] = 45 < 55 low = mid + 1 = 5 mid low Step-2: mid = (low + high ) / 2 = (5 + 8) /2 = 6 A[mid] = 51 < 55 low = mid + 1 = 7 low Step-3: mid = (low + high ) / 2 = (7 + 8) /2 = 7 A[mid] = 55 == 55 found mid mid Search 55
  • 19.
    Complexity Analysis ofBinary Search 19 • Best case: • Worst Case:
  • 20.
    Features of BinarySearch 20 ▪ It is great to search through large sorted arrays. ▪ It has a time complexity of O(log n) which is a very good time complexity. ▪ It has also a simple implementation.
  • 21.
  • 22.
  • 23.
    Complexity Analysis ofTwo Algorithms 23 Algorithm Best case Worst case Linear search O(1) O(N) Binary search O(1) O(log N)
  • 24.
    • If wehave an array of length 70000000 but sorted, which searching algorithm will work fast and why?? • Which Searching Algorithm is followed to search any data from a Dictionary? • Search item 75 using binary search algorithm from the following list. A= [22, 3, 127, 515, 163, 69, 75, 88, 96, 10] 24 Try to answer!
  • 25.
    Search 30 fromthe list using Binary Search. Show each step. 25 Try to answer! 10 15 21 25 30 40 51 60
  • 26.