1 Search Algorithms Sequential Search (Linear Search) Binary Search Rizwan Rehman Centre for...

24
1 Search Algorithms Sequential Search (Linear Search) Binary Search Rizwan Rehman Centre for Computer Studies Dibrugarh University

Transcript of 1 Search Algorithms Sequential Search (Linear Search) Binary Search Rizwan Rehman Centre for...

Page 1: 1 Search Algorithms Sequential Search (Linear Search) Binary Search Rizwan Rehman Centre for Computer Studies Dibrugarh University.

1

Search Algorithms

Sequential Search (Linear Search)Binary Search

Rizwan RehmanCentre for Computer Studies

Dibrugarh University

Page 2: 1 Search Algorithms Sequential Search (Linear Search) Binary Search Rizwan Rehman Centre for Computer Studies Dibrugarh University.

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. In this section, two commonly used approaches are discussed, linear search and binary search.

2

Page 3: 1 Search Algorithms Sequential Search (Linear Search) Binary Search Rizwan Rehman Centre for Computer Studies Dibrugarh University.

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.

3

Page 4: 1 Search Algorithms Sequential Search (Linear Search) Binary Search Rizwan Rehman Centre for Computer Studies Dibrugarh University.

4

Linear SearchO (n)

A Linear search of a list/array begins at the beginning of the list/array and continues until the item is found or the entire list/array has been searched

Page 5: 1 Search Algorithms Sequential Search (Linear Search) Binary Search Rizwan Rehman Centre for Computer Studies Dibrugarh University.

5

Linear Search

bool LinSearch(double x[ ], int n, double item){

for(int i=0;i<n;i++){

if(x[i]==item) return true;

else return false;

}

return false;

}

Page 6: 1 Search Algorithms Sequential Search (Linear Search) Binary Search Rizwan Rehman Centre for Computer Studies Dibrugarh University.

6

Search AlgorithmsSuppose that there are n elements in the array. The following expression gives the average number of comparisons:

It is known that

Therefore, the following expression gives the average number of comparisons made by the Linear search in the successful case:

Page 7: 1 Search Algorithms Sequential Search (Linear Search) Binary Search Rizwan Rehman Centre for Computer Studies Dibrugarh University.

7

Search Algorithms

Page 8: 1 Search Algorithms Sequential Search (Linear Search) Binary Search Rizwan Rehman Centre for Computer Studies Dibrugarh University.

Linear Search Tradeoffs 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

Page 9: 1 Search Algorithms Sequential Search (Linear Search) Binary Search Rizwan Rehman Centre for Computer Studies Dibrugarh University.

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

Page 10: 1 Search Algorithms Sequential Search (Linear Search) Binary Search Rizwan Rehman Centre for Computer Studies Dibrugarh University.

10

Binary SearchO(log2 n)

A binary search looks for an item in a list using a divide-and-conquer strategy

Page 11: 1 Search Algorithms Sequential Search (Linear Search) Binary Search Rizwan Rehman Centre for Computer Studies Dibrugarh University.

11

Binary Search– Binary search algorithm assumes that the items in the

array being searched are sorted

– The algorithm begins at the middle of the array in a binary search

– If the item for which we are searching is less than the item in the middle, we know that the item won’t be in the second half of the array

– Once again we examine the “middle” element

– The process continues with each comparison cutting in half the portion of the array where the item might be

Page 12: 1 Search Algorithms Sequential Search (Linear Search) Binary Search Rizwan Rehman Centre for Computer Studies Dibrugarh University.

12

Binary Search

Page 13: 1 Search Algorithms Sequential Search (Linear Search) Binary Search Rizwan Rehman Centre for Computer Studies Dibrugarh University.

13

Binary Search: middle element

left + right

2mid =

Page 14: 1 Search Algorithms Sequential Search (Linear Search) Binary Search Rizwan Rehman Centre for Computer Studies Dibrugarh University.

14

Binary Searchbool BinSearch(double list[ ], int n, double item, int&index){

int left=0;

int right=n-1;

int mid;

while(left<=right){

mid=(left+right)/2;

if(item> list [mid]){ left=mid+1; }

else if(item< list [mid]){right=mid-1;}

else{

item= list [mid];

index=mid;

return true; }

}// while

return false;

}

Page 15: 1 Search Algorithms Sequential Search (Linear Search) Binary Search Rizwan Rehman Centre for Computer Studies Dibrugarh University.

15

Binary Search: Example

Page 16: 1 Search Algorithms Sequential Search (Linear Search) Binary Search Rizwan Rehman Centre for Computer Studies Dibrugarh University.

16

Binary Search

Page 17: 1 Search Algorithms Sequential Search (Linear Search) Binary Search Rizwan Rehman Centre for Computer Studies Dibrugarh University.

17

Binary Search

Page 18: 1 Search Algorithms Sequential Search (Linear Search) Binary Search Rizwan Rehman Centre for Computer Studies Dibrugarh University.

18

Binary Search Tree

Page 19: 1 Search Algorithms Sequential Search (Linear Search) Binary Search Rizwan Rehman Centre for Computer Studies Dibrugarh University.

19

Binary Search TreeO(log2 n)

bool search(Node * root, int id) { bool found = false; if(root == NULL) return false; if(root->data == id) {

cout<<" The node is found "<<endl; found = true;

} if(!found) found = search(root->left, id); if(!found) found = search(root->right, id); return found;

}

Page 20: 1 Search Algorithms Sequential Search (Linear Search) Binary Search Rizwan Rehman Centre for Computer Studies Dibrugarh University.

20

Binary Search Tree

Page 21: 1 Search Algorithms Sequential Search (Linear Search) Binary Search Rizwan Rehman Centre for Computer Studies Dibrugarh University.

21

Binary Search Tree

Page 22: 1 Search Algorithms Sequential Search (Linear Search) Binary Search Rizwan Rehman Centre for Computer Studies Dibrugarh University.

22

Binary Search Tree

Page 23: 1 Search Algorithms Sequential Search (Linear Search) Binary Search Rizwan Rehman Centre for Computer Studies Dibrugarh University.

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!

23

Page 24: 1 Search Algorithms Sequential Search (Linear Search) Binary Search Rizwan Rehman Centre for Computer Studies Dibrugarh University.

Thank You

24