Uploaded by fomaj14461

NCIR HDSDEV SEP Lecture 5 Searching Algorithms

advertisement
Algorithms and Advanced
programming
Yalemisew Abgaz
05/03/2022
National College of Ireland, School of Computing
1
Lecture 05 Searching Algorithms
• Searching Algorithms
• Sequential search
• Binary search
2
Sequential Search
• Searching is another interesting problem in Algorithms analysis. Given an array of
objects, searching algorithm tries to find an element if it exists in the array.
• Sequential search (aka Linear search) searches all the elements of the array
sequentially until the target element is found or all the elements are checked.
• Sequential search:
•
•
•
•
Starts from the beginning of the array
Moves from one item to another item
if the item is found at the ith index, it returns the index (or the value)
otherwise, keep searching until it reaches at the end
05/03/2022
National College of Ireland, School of Computing
3
Sequential Search
Example: Suppose we have an array of integer numbers
num =
12
index =
0
23
1
17
23
2
3
42
4
99
60
9
6
80
5
6
7
8
9
Example: Find the value 60 and return its index
index = 0
Step 1. read num[index]
Step 2. if num[index] == 60
found at index
Step 3. else
increment the current index
go to step 1
05/03/2022
index = 0,
index = 1,
index = 2,
index = 3,
index = 4,
index = 5,
index = 6,
num[0] = 12 != 60
num[1] = 23 != 60
num[2] = 17 != 60
num[3] = 23 != 60
num[4] = 42 != 60
num[5] = 99 != 60
num[6] = 60 != 60 Found here
National College of Ireland, School of Computing
4
Sequential Search Algorithm
public static int SequentialSearch(int arr[], int searchKey)
{
int n = arr.length;
for (int i = 0; i < n; i++)
{
if (arr[i] == searchKey)
return i;
}
return -1;
}
05/03/2022
National College of Ireland, School of Computing
5
Analysis of Sequential Search
• The best case scenario for an array containing n items is when the target is found
at the beginning.
• This requires only a single comparison
• The worst case scenario is when the target is not in the array
• This require n comparisons
• O(n)
• The average case scenario is when the target is in the middle
• This requires n/2 comparisons
• n/2  O(n)
05/03/2022
National College of Ireland, School of Computing
6
Binary Search
• Binary search works on elements that are sorted by the search key
• it searches a sorted array by repeatedly dividing the search interval in half.
• if the value of the target key is less than the middle value, it narrows the search to
the lower half.
• if the value of the target key is greater than the middle value, it narrows the
search to the upper half.
• repeat this process until all the values are found or the interval is empty
05/03/2022
National College of Ireland, School of Computing
7
Binary Search
Step 1: Example: find number 17 in num using binary search
num =
[0]
[1]
[2]
[3]
[4]
[5]
[6]
[7]
[8]
6 9 12 17 23 42 60 80 91
searchKey = 17
05/03/2022
National College of Ireland, School of Computing
8
Binary Search
Step 1: Find the middle value = [4]
[0]
num =
[1]
[2]
[3]
[4]
[5]
[6]
[7]
[8]
6 9 12 17 23 42 60 80 91
searchKey = 17
05/03/2022
National College of Ireland, School of Computing
9
Binary Search
Step 1: Find the middle value = [4]
[0]
num =
[1]
[2]
[3]
[4]
[5]
[6]
[7]
[8]
6 9 12 17 23 42 60 80 91
is 17 = the mid point value? No
05/03/2022
National College of Ireland, School of Computing
10
Binary Search
Step 1: Find the middle value = [4]
[0]
num =
[1]
[2]
[3]
[4]
[5]
[6]
[7]
[8]
6 9 12 17 23 42 60 80 91
is 17 < the mid point value? Yes
05/03/2022
National College of Ireland, School of Computing
11
Binary Search
Step 1: Find the middle value = [4]
[0]
num =
[1]
[2]
[3]
[4]
[5]
[6]
[7]
[8]
6 9 12 17 23 42 60 80 91
Now only consider values less than 23
searchKey = 17
05/03/2022
National College of Ireland, School of Computing
12
Binary Search
Step 1: Find the middle value = [1]
[0]
num =
05/03/2022
[1]
[2]
[3]
[4]
[5]
[6]
[7]
[8]
6 9 12 17 23 42 60 80 91
National College of Ireland, School of Computing
13
Binary Search
Step 1: Find the middle value = [1]
[0]
num =
[1]
[2]
[3]
[4]
[5]
[6]
[7]
[8]
6 9 12 17 23 42 60 80 91
is 17 = the mid point value? No
05/03/2022
National College of Ireland, School of Computing
14
Binary Search
Step 1: Find the middle value = [1]
[0]
num =
[1]
[2]
[3]
[4]
[5]
[6]
[7]
[8]
6 9 12 17 23 42 60 80 91
is 17 < the mid point value? No
Consider the right side
05/03/2022
National College of Ireland, School of Computing
15
Binary Search
Step 1: Find the middle value = [2]
[0]
num =
[1]
[2]
[3]
[4]
[5]
[6]
[7]
[8]
6 9 12 17 23 42 60 80 91
is 17 = the mid point value? No
05/03/2022
National College of Ireland, School of Computing
16
Binary Search
Step 1: Find the middle value = [2]
[0]
num =
[1]
[2]
[3]
[4]
[5]
[6]
[7]
[8]
6 9 12 17 23 42 60 80 91
is 17 < the mid point value? No
Consider the right side
05/03/2022
National College of Ireland, School of Computing
17
Binary Search
Step 1: Find the middle value = [3]
[0]
num =
[1]
[2]
[3]
[4]
[5]
[6]
[7]
[8]
6 9 12 17 23 42 60 80 91
is 17 = the mid point value? Yes
Found, Return num[3]
05/03/2022
National College of Ireland, School of Computing
18
Binary Search Algorithm
int binarySearch(int arr[], int l, int r, int searchKey)
{
if (r >= l) {
int mid = l + (r - l) / 2;
// If the element is present at the
// middle itself
if (arr[mid] == searchKey)
return mid;
// check if the search keys is on the left or the right
if (arr[mid] > searchKey)
return binarySearch(arr, l, mid - 1, searchKey);
// Notice the recursive call abve.
return binarySearch(arr, mid + 1, r, searchKey);
}
05/03/2022
National College of Ireland, School of Computing
19
Time complexity of Binary Search
• The Binary search divides the search space into two on every iteration
• That reminds us the logarithmic function
• The time complexity of a binary search is O(logn)
05/03/2022
National College of Ireland, School of Computing
20
Sources
• Data Structures and Algorithms in Java, 6th edition, Author: Michael T. Goodrich,
Roberto Tamassia, Michael H. Goldwasser
• Data Structures and Algorithms in Java, 2nd edition, Robert Lafore
• Introduction to Algorithms, 3rd edition, Thomas H. Cormen , Charles E. Leiserson
, Ronald L. Rivest , Clifford Stein
• Java code adopted from https://www.geeksforgeeks.org/binary-search/
• https://en.wikipedia.org/wiki/Linear_search
• https://runestone.academy/runestone/books/published/pythonds/SortSearch/T
heSequentialSearch.html
21
Download