Presentation is loading. Please wait.

Presentation is loading. Please wait.

AL-HUSEEN BIN TALAL UNIVERSITY College of Engineering Department of Computer Engineering Algorithms and Data Structures Search Algorithms Course No.: 0511363.

Similar presentations


Presentation on theme: "AL-HUSEEN BIN TALAL UNIVERSITY College of Engineering Department of Computer Engineering Algorithms and Data Structures Search Algorithms Course No.: 0511363."— Presentation transcript:

1 AL-HUSEEN BIN TALAL UNIVERSITY College of Engineering Department of Computer Engineering Algorithms and Data Structures Search Algorithms Course No.: 0511363 Fall 2014

2 What is Searching Algorithm? Searching techniques enables you to find location of data item in a specified array of data items. by matching each data item with the desired data item one by one as in the sequential search, or matching each data item randomly in binary search.

3 Sequential Search Sequential Search (linear search): The search always starts at the first element in the list and continues until either the item is found in the list or the entire list is searched. Sequential search on linked lists and on array was covered in previous chapters

4 Sequential Search #include int sequential(int x[ ],int n,int key) { for(int i=0;i<n;i++) if(key==x[i]) return i; return -1; } main() { int m; int x[7]={9,7,8,2,4,3,6}; cin>>m; cout<<sequential(x,8,m)<<endl; }

5 Time complexity The complexity is dependent on the size of the list. The worst case for the search is if the key is not found. At that point the whole list has been traversed so if the list has n elements, the algorithm is O(n). The best case is also simple. The key is found in the first element so it is O(1). The average case is n/2. What this says is that on the average the key will be found in the first half of the list --not an unreasonable expectation.

6 Binary Search A binary search can be performed only on ordered lists The binary search algorithm uses the divide-and- conquer technique to search the list. First, the search item is compared with the middle element of the list. If the search item is found, the search terminates. If the search item is less than the middle element of the list, we restrict the search to the first half of the list; otherwise, we search the second half of the list. This is a powerful method.

7 Suppose that we want to determine whether 75 is in this list First, we compare 75 with the middle element in this list, list[5] (which is 39). Because 75 >list[5], we restrict our search to this list Example

8 #include int Binary_(int x[],int n,int key) { int Up=n-1, Lo=0, mid; while(Lo <= Up){ mid=(Lo+Up)/2; if(key==x[mid]) return mid; if(key<x[mid]) Up=mid-1; else Lo=mid+1; } return -1; } Binary Search Method main(){ int m; int x[7]={1,2,3,4,5,6,7}; cin>>m; cout<<Bi_(x,7,m)<<endl; }

9 Time complexity Given an array of 1023 elements, we can narrow the search to 511 items in one comparison. Another comparison, and we’re looking at only 255 elements. In fact, we can search the entire array in only 10 comparisons Thus the binary search is a O(lg n) algorithm, where n is the length of the list

10 Binary Search by Recursion #include int Binary(int x[], int L, int R, int Key){ if(L > R) return-1; else { int mid, index ; mid = (L + R) / 2; if( Key == x[mid] ) index=mid; else if( Key < x[mid]) index=Bi_(x, L, mid-1, Key) ; else index = Bi_(x, mid + 1, R, Key); return index; } main() { int m ; const int size = 6 ; int x[size]={ 10, 17, 45, 49, 55, 68} ; cin>>m; cout<<" The position of Key "<<m<<" is at position : " <<Bi_(x, 0, size-1, m)<<endl ; }


Download ppt "AL-HUSEEN BIN TALAL UNIVERSITY College of Engineering Department of Computer Engineering Algorithms and Data Structures Search Algorithms Course No.: 0511363."

Similar presentations


Ads by Google