Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 36

Searching Algorithms

Sequential Search
Binary Search
Binary Search Tree (BST)
The Problem

Fig. 1 Searching is an every day


occurrence. 2
Searching Arrays

Searching is the process of looking for a specific element in an array;


for example, discovering whether a certain score is included in a list of
scores. Searching, like sorting, is a common task in computer
programming. There are many algorithms and data structures devoted
to searching.

3
Linear Search

The linear search approach compares the key element, key, with each
element in the array list[]. The method continues to do so until the key
matches an element in the list or the list is exhausted without a match
being found. If a match is made, the linear search returns the index of
the element in the array that matches the key. If no match is found,
the search returns -1.

4
Searching Algorithms (Cont’d)

• Suppose that you want to determine whether 27 is in the list

• This search is successful!

5
Searching Algorithms (Cont’d)

Let’s now search for 10

This is an unsuccessful search

6
Sequential Search(Linear Search)

void main() {
int A[8] = { 1, 5, 6, 7, 9, 10, 17, 30};
int x, n, index;
cout << "Enter search element: ";
cin >> x;
index = -1;
for(n=0; n<8 && A[n]<=x; n++)
if(A[n] == x)
index = n;
if(index==-1)
cout << "Not found!!" << endl;
else
cout << "Found at: " << index << endl;
}

7
Searching

Linear Search is not very efficient.


In the worst case, the target we are searching could
be placed at the very end of the array, requiring
that we search every single element in the array.
On average, the target might be somewhere in the
middle of the array, requiring that we search half of
all the elements in the array.
For example, if we had a 1000 element array:

worst case: we must search 1000 elements

on average: we search about 500 elements

8
Binary Search

Binary Search is a more efficient option for searching


arrays.
There are two tricks to using Binary Search:

First, the array must be sorted. (Before you use
Binary Search, you might first sort the array.)

Second, Binary Search works by dividing the search
area in half after each comparison (more on this
soon.)
Binary Search is used very commonly in computer
science, and there is a related concept called Binary
Search Trees.

If you take an Algorithms course, you will definitely
spend time researching Binary Search Trees.

9
Binary Search Pseudo Code

1. Create a sorted array of sample data.


2. Prompt user for a search target.
3. Locate the middle of the array.
4. Compare the middle element with the search
target:
a) If the search key is equal to the middle
element, we have a match! Exit Loop
b) If the search key is less than the middle
element, search the first half of the array
(back to Step 4)
c) If the search key is larger than the middle
element, search the second half of the array
(back to Step 4)

10
Binary Search Efficiency

Binary Search is very efficient.


For example, if you have 1024 elements in you array,
in the worst case, binary search only requires 10
comparisons.

Linear Search Worst Case: 1024

Binary Search Worst Case: 10
If you have 1 billion elements, binary search only
requires 30 comparisons!
Linear Search Worst Case: 1 billion
Binary Search Worst Case: 30 !

11
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 15

low hi

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 15

low mid hi

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 15

low hi

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 15

low mid hi

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 15

low hi

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 15

low mid hi

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 15

low
hi

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 15

low
hi
mid

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 15

low
hi
mid

20
Binary Search

void main() {
int A[8] = {1, 5, 6, 7, 9, 10, 17, 30};
int x, lower, middle, upper;
cout << "Enter search element: ";
cin >> x;
low = 0;
hi = 7; // array size - 1
mid = (low + hi)/2;
while(A[mid]!=x && low<hi){
if(x<A[mid])
hi = mid - 1;
else
low = mid + 1;
mid = (low + hi)/2;
}
if(A[mid]!=x)
cout << "Not found!!" << endl;
else
cout << "Found at: " << mid << endl;
}

21
Binary Trees, Binary Search Trees
Binary Trees

A tree in which no node can have more than two


children

Generic
binary tree

The depth of an “average” binary tree is considerably smaller


than N, even though in the worst case, the depth can be as large
as N – 1.

Worst-case
binary tree

23
Node Struct of Binary Tree

Possible operations on the Binary Tree ADT



Parent, left_child, right_child, sibling, root, etc
Implementation

Because a binary tree has at most two children, we can keep direct
pointers to them

24
Binary Search Trees (BST)

A data structure for efficient searching, inser-tion and deletion


Binary search tree property

For every node X

All the keys in its left
subtree are smaller than
the key value in X

All the keys in its right
subtree are larger than the
key value in X

25
Binary Search Trees

A binary search tree Not a binary search tree

26
Binary Search Trees

The same set of keys may have different BSTs

Average depth of a node is O(log N)


Maximum depth of a node is O(N)

27
Searching BST

If we are searching for 15, then we are done.


If we are searching for a key < 15, then we should search in the left
subtree.
If we are searching for a key > 15, then we should search in the right
subtree.

28
29
Searching (Find)

Find X: return a pointer to the node that has key X, or NULL if there
is no such node
BinaryNode * BinarySearchTree::Find(const float &x,
BinaryNode *t) const
{
if (t == NULL)
return NULL;
else if (x < t->element)
return Find(x, t->left);
else if (t->element < x)
return Find(x, t->right);
else
return t; // match
}

Time complexity: O(height of the tree)

30
findMin/ findMax

Goal: return the node containing the smallest (largest) key in the tree
Algorithm: Start at the root and go left (right) as long as there is a left
(right) child. The stopping point is the smallest (largest) element
BinaryNode * BinarySearchTree::FindMin(BinaryNode *t) const
{
if (t == NULL)
return NULL;
if (t->left == NULL)
return t;
return FindMin(t->left);
}

Time complexity = O(height of the tree)

31
Insertion

Proceed down the tree as you would with a find


If X is found, do nothing (or update something)
Otherwise, insert X at the last spot on the path traversed

Time complexity = O(height of the tree)

32
Deletion

When we delete a node, we need to consider how we take care of the


children of the deleted node.

This has to be done such that the property of the search tree is
maintained.

33
Deletion under Different Cases

Case 1: the node is a leaf



Delete it immediately
Case 2: the node has one child

Adjust a pointer from the parent to bypass that node

34
Deletion Case 3

Case 3: the node has 2 children



Replace the key of that node with the minimum element at the right
subtree

Delete that minimum element
– Has either no child or only right child because if it has a left
child, that left child would be smaller and would have been
chosen. So invoke case 1 or 2.

Time complexity = O(height of the tree)

35
Q&A

36

You might also like