Download presentation
Presentation is loading. Please wait.
Published byKory Higgins Modified over 9 years ago
1
1
2
Searching The basic characteristics of any searching algorithm is that searching should be efficient, it should have less number of computations involved into it, as well as the space occupied by such a technique should be less. Various searching techniques are: Linear search Binary search
3
Linear Search / Sequential Search It is a method for finding a particular value in a list that checks each element in sequence until the desired element is found or the list is exhausted. The list need not be ordered. Linear search is usually very simple to implement, and is practical when the list has only a few elements, or when performing a single search in an unordered list.
4
Linear Search / Sequential Search Function LINEAR_SEARCH (K, N, X) : Given an unordered vector K consisting of N + 1 (N ≥ 1) elements, this algorithms searches the vector for a particular element having the value X. Vector element K[N + 1] serves a sentinel element and receives the value of X prior to the search. The function returns the index of the vector element if the search is successful, and returns zero otherwise. 1. [Initialize search?] 1. I 1 2. K [ N + 1] X 2. [Search the vector] 1. Repeat while K[I] ≠ X 1. I I + 1 3. [Successful search?] 1. If I = N + 1 2. Then Write (‘UNSUCCESSFUL SEARCH’) 1. Return (0) 3. Else Write (‘SUCCESSFUL SEARCH’) 1. Return (0)
5
Analysis of Linear Search / Sequential Search For a list with n items, the best case is when the value is equal to the first element of the list, in which case only one comparison is needed. O(1) The worst case is when the value is not in the list (or occurs only once at the end of the list), in which case n comparisons are needed. O(n) The average case is when the value is equal to the middle element of the list, so n / 2 comparisons are needed. O(n)
6
Linear Search / Sequential Search
7
Binary Search A binary search or half-interval search algorithm finds the position of a specified input value (the search "key") within an array sorted by key value. For binary search, the array should be arranged in ascending or descending order. In each step, the algorithm compares the search key value with the key value of the middle element of the array. If the keys match, then a matching element has been found and its index, or position, is returned. Otherwise, if the search key is less than the middle element's key, then the algorithm repeats its action on the sub-array to the left of the middle element or, if the search key is greater, on the sub-array to the right. If the remaining array to be searched is empty, then the key cannot be found in the array and a special "not found" indication is returned. A binary search halves the number of items to check with each iteration, so locating an item (or determining its absence) takes logarithmic time. It requires the array to be in sorted order.
8
Binary Search (Iterative) Function BINARY_SEARCH (K, N, X) : Given an unordered vector K consisting of N elements in ascending order, this algorithms searches the structure for a given element having the value X. The variables LOW, MIDDLE and HIGH denote the lower, middle and upper limits of the search interval, respectively. The function returns the index of the vector element if the search is successful, and returns zero otherwise. 1. [Initialize] 1. LOW 1 2. HIGH N 2. [Perform search] 1. Repeat thru step 4 while LOW ≤ HIGH 3. [Obtain index of midpoint of interval 1. MIDDLE (LOW + HIGH) / 2 4. [Compare] 1. If X < K[MIDDLE] 2. Then HIGH MIDDLE – 1 3. Else 1. If X > K [MIDDLE] 2. Then LOW MIDDLE + 1 3. Else Write (‘SUCCESSFUL SEARCH’) 1. Return (MIDDLE) 5.[Unsuccessful search] 1.Write (‘UNSUCCESSFUL SEARCH’) 2.Return (0)
9
Binary Search (Recursive) Function BINARY_SEARCH_R (P, Q, K, X) : Given the bounds of a subtable P and Q and a vector K, this algorithm recursively searches for the given element whose value is stored in X. MIDDLE and LOC are integer variables. 1. [Search the vector K between P and Q for value X] 1. If P > Q 2. Then LOC 0 3. Else MIDDLE (P + Q) / 2 1. If X < K[MIDDLE] 2. Then LOC BINARY_SEARCH_R(P, MIDDLE – 1, K, X) 3. Else If X > K[MIDDLE] 1. Then LOC BINARY_SEARCH_R(MIDDLE + 1, Q, K, X) 2. Else LOC MIDDLE 2. [Finished] 1. Return (LOC)
10
Analysis of Binary Search To analyze the binary search algorithm, we need to recall that each comparison eliminates about half of the remaining items from consideration. What is the maximum number of comparisons this algorithm will require to check the entire list? If we start with n items, about n / 2 items will be left after the first comparison. After the second comparison, there will be about n / 4. Then n / 8, n / 16, and so on. How many times can we split the list? When we split the list enough times, we end up with a list that has just one item. Either that is the item we are looking for or it is not. Either way, we are done. The number of comparisons necessary to get to this point is i where n / 2 i = 1. Solving for i gives us i = log n. The maximum number of comparisons is logarithmic with respect to the number of items in the list. Therefore, the binary search is O ( log n ).
11
Analysis of Binary Search The numbers of comparisons for the recursive and iterative versions of Binary Search are the same, if comparison counting is relaxed slightly. For Recursive Binary Search, count each pass through the if-then-else block as one comparison. For Iterative Binary Search, count each pass through the while block as one comparison. Best case - O (1) comparisons In the best case, the item X is the middle in the array A. A constant number of comparisions (actually just 1) are required. Worst case - O (log n) comparsions In the worst case, the item X does not exist in the array A at all. Through each recursion or iteration of Binary Search, the size of the admissible range is halved. This halving can be done (log n ) times. Thus, (log n ) comparisons are required. Average case - O (log n) comparsions To find the average case, take the sum over all elements of the product of number of comparsions required to find each element and the probability of searching for that element. To simplify the analysis, assume that no item which is not in A will be searched for, and that the probabilities of searching for each element are uniform. The space requirements for the recursive and iterative versions of binary search are different. Iterative Binary Search requires only a constant amount of space, while Recursive Binary Search requires space proportional to the number of comparisons to maintain the recursion stack.
12
Binary Search
13
ANY QUESTIONS?? 13
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.