Download presentation
Presentation is loading. Please wait.
1
Searching: linear & binary
Lecture 02 Searching: linear & binary
2
How to solve a problem Hit and trial method Divide and conquer
Linear search Divide and conquer Recursion Dynamic programming (optimization problems) Longest common subsequence problem Superior Uni- DSA2014 BSSE
3
Hit and trial method: hitting on a problem randomly and see weather it is optimal solution or not
Linear search: one by one see all the elements/ items and find is it the value I am looking for. Divide-and-conquer: Divide a problem into number of sub problems. Conquer the problem by solving it recursively. Combine the solutions of sub problems for original problem Recursion: is an equation or inequality that describes a function in terms of its value on smaller inputs Dynamic Programming: applies where sub problems overlap. It does not repeatedly solve a common sub problem. It solves a sub problem once and saves answer and constructs an optimal solution from computed information LCS: comparing DNAs, file comparison (2 different versions of same file), screen display etc Superior Uni- DSA2014 BSSE
4
Linear search Linear search or sequential search is a method for finding particular value in a list that consists of checking every one of its elements, one at a time and in sequence, until the desired one is found. The linear search is very simple and works on unsorted arrays of elements Can be implemented as Forward search Recursive method Searching in reverse order etc Superior Uni- DSA2014 BSSE
5
Method of searching linear search intuitive approach
start at first item is it the one I am looking for? if not go to next item repeat until found or all items checked Array does not need to be sorted Superior Uni- DSA2014 BSSE
6
Linear search analysis
Linear search looks at every element until it finds a match The worst-case would be the element is not found or it is the last element in the array for an array of length n, n comparisons would be required Linear search is used for smaller number of elements Superior Uni- DSA2014 BSSE
7
Pseudo code Superior Uni- DSA2014 BSSE
8
Binary search Binary search is another searching algorithm, used to search a specific value (or index of value) from the sorted array. In binary search, we first compare the value to be searched with the item in the middle position of the array. If there's a match, we can return immediately. If the key is less than the middle key, then the item sought must lie in the lower half of the array; if it's greater than the item sought must lie in the upper half of the array Superior Uni- DSA2014 BSSE
9
Method of searching Binary search takes a sorted array of length n:
looks at the middle element of array, if it equals search key return array position -else- If middle element is greater than search key Changes upper bound of search area to [middle] – 1 Searches again looking at middle element in new bounds If middle element is less than search key Changes lower bound of search area to [middle] + 1 If the element is not found returns a value outside of the range, or NIL. Superior Uni- DSA2014 BSSE
10
Superior Uni- DSA2014 BSSE
11
Pseudo code Superior Uni- DSA2014 BSSE
12
Binary Search Binary search. Given value and sorted array a[], find index i such that a[i] = value, or report that no such index exists. Invariant. Algorithm maintains a[lo] value a[hi]. Ex. Binary search for 33. 6 13 14 25 33 43 51 53 64 72 84 93 95 96 97 1 2 3 4 5 6 7 8 9 10 11 12 13 14 lo hi Superior Uni- DSA2014 BSSE
13
Binary Search Binary search. Given value and sorted array a[], find index i such that a[i] = value, or report that no such index exists. Invariant. Algorithm maintains a[lo] value a[hi]. Ex. Binary search for 33. 6 13 14 25 33 43 51 53 64 72 84 93 95 96 97 1 2 3 4 5 6 7 8 9 10 11 12 13 14 lo mid hi Superior Uni- DSA2014 BSSE
14
Binary Search Binary search. Given value and sorted array a[], find index i such that a[i] = value, or report that no such index exists. Invariant. Algorithm maintains a[lo] value a[hi]. Ex. Binary search for 33. 6 13 14 25 33 43 51 53 64 72 84 93 95 96 97 1 2 3 4 5 6 7 8 9 10 11 12 13 14 lo hi Superior Uni- DSA2014 BSSE
15
Binary Search Binary search. Given value and sorted array a[], find index i such that a[i] = value, or report that no such index exists. Invariant. Algorithm maintains a[lo] value a[hi]. Ex. Binary search for 33. 6 13 14 25 33 43 51 53 64 72 84 93 95 96 97 1 2 3 4 5 6 7 8 9 10 11 12 13 14 lo mid hi Superior Uni- DSA2014 BSSE
16
Binary Search Binary search. Given value and sorted array a[], find index i such that a[i] = value, or report that no such index exists. Invariant. Algorithm maintains a[lo] value a[hi]. Ex. Binary search for 33. 6 13 14 25 33 43 51 53 64 72 84 93 95 96 97 1 2 3 4 5 6 7 8 9 10 11 12 13 14 lo hi Superior Uni- DSA2014 BSSE
17
Binary Search Binary search. Given value and sorted array a[], find index i such that a[i] = value, or report that no such index exists. Invariant. Algorithm maintains a[lo] value a[hi]. Ex. Binary search for 33. 6 13 14 25 33 43 51 53 64 72 84 93 95 96 97 1 2 3 4 5 6 7 8 9 10 11 12 13 14 lo mid hi Superior Uni- DSA2014 BSSE
18
Binary Search Binary search. Given value and sorted array a[], find index i such that a[i] = value, or report that no such index exists. Invariant. Algorithm maintains a[lo] value a[hi]. Ex. Binary search for 33. 6 13 14 25 33 43 51 53 64 72 84 93 95 96 97 1 2 3 4 5 6 7 8 9 10 11 12 13 14 lo hi Superior Uni- DSA2014 BSSE
19
Binary Search Binary search. Given value and sorted array a[], find index i such that a[i] = value, or report that no such index exists. Invariant. Algorithm maintains a[lo] value a[hi]. Ex. Binary search for 33. 6 13 14 25 33 43 51 53 64 72 84 93 95 96 97 1 2 3 4 5 6 7 8 9 10 11 12 13 14 lo hi mid Superior Uni- DSA2014 BSSE
20
Binary Search Binary search. Given value and sorted array a[], find index i such that a[i] = value, or report that no such index exists. Invariant. Algorithm maintains a[lo] value a[hi]. Ex. Binary search for 33. 6 13 14 25 33 43 51 53 64 72 84 93 95 96 97 1 2 3 4 5 6 7 8 9 10 11 12 13 14 lo hi mid Superior Uni- DSA2014 BSSE
21
Binary search analysis
Logarithmic: problem size reduced to half Superior Uni- DSA2014 BSSE
22
Binary or linear Superior Uni- DSA2014 BSSE
23
Binary or linear Superior Uni- DSA2014 BSSE
24
https://groups.yahoo.com/neo/groups/BSSE3_DSA_Superi orUni/info
Group URL orUni/info Superior Uni- DSA2014 BSSE
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.