Presentation is loading. Please wait.

Presentation is loading. Please wait.

Searching Searching: –Mainly used for: Fetching / Retrieving the Information such as, –Select query on a database. –Important thing is: Retrieval of Information.

Similar presentations


Presentation on theme: "Searching Searching: –Mainly used for: Fetching / Retrieving the Information such as, –Select query on a database. –Important thing is: Retrieval of Information."— Presentation transcript:

1 Searching Searching: –Mainly used for: Fetching / Retrieving the Information such as, –Select query on a database. –Important thing is: Retrieval of Information should be as quick as possible which can be only achieved if, –The number of comparisons are as minimum as possible.

2 Searching Classification Tree Search Non-Linear Search Sequential Search Binary Search Linear Search Graph Search Array Search Linked List Search Binary Tree Search AVL Tree Search

3 Sequential Search on Array 11 20 9 18 15 0 1 2 3 4 Array A Index 9 i = 0 While (i <= 4) Process (A[ i ]) i = i + 1 EndWhile Steps: If( A[ i ] == 9 ), then EndIf found = 1 location = i, found = 0, location = NULL If (found == 0), then Else EndIf Return(location) print “Search is unsuccessful.” print “Search is successful.” && (found == 0), do Question: Will it work for any array? i = L (i <= U) Question: Will it work for any search value? KEY = If (A[ i ] == KEY) Question: Is it the most optimized / efficient? L U

4 Sequential Search on Array Algorithm: –SequentialSearch. Input: –Array A with elements. –KEY: Element to be searched. Output: –On Success, appropriate message and return Index/Location of KEY in array A. –On Failure, appropriate message and return NULL. Data Structure: –Array A[L...U]

5 Algorithm: SequentialSearch Steps: i = L, found = 0, location = NULL While (i <= U) && (found == 0), do If( A[ i ] == KEY ), then found = 1 location = i EndIf i = i + 1 EndWhile If (found == 0), then print “Search is unsuccessful.” Else print “Search is successful.” EndIf Return(location) Stop

6 Analysis of Sequential Search Complexity Analysis: –Performance of any search algorithm depends on: Number of comparisons it has to do. –The lesser the comparisons, »Better is the performance. –The greater the number of comparisons, »Slower is the performance. –3 different cases: Case-1: –KEY matches with the first element. »Best case. Case-2: –KEY does not exist OR KEY matches with the last element. »Worse case. Case-3: –KEY is present at any location in the array. »Average case.

7 Analysis of Sequential Search Complexity Analysis: –Best Case: KEY is present in the array as the 1 st element. We will need only 1 comparison for searching. –Number of comparisons: »T(n) = 1.

8 Analysis of Sequential Search Complexity Analysis: –Worse Case: KEY does not exist OR KEY matches with the last element in the array. If the size of the array is n, –Then minimum number of comparisons needed to reach any conclusion is: »T(n) = n

9 Analysis of Sequential Search Complexity Analysis: –Average Case: KEY is present at any location in the array. Let p i be the probability that the key may be present at the i-th location. (1 <= i <= n) –Number of comparisons, i = 1 i = n p i * iT(n) =

10 Analysis of Sequential Search i = 1 i = n p i * iT(n) = For simplicity, let us assume that place of key is equally probable at every location. So p1 = p2 = p3 = p4 =... pn = 1/n Average Case i = 1 i = n iT(n) =(1/n) * (n (n+1) / 2)T(n) =(1/n) (n+1) / 2T(n) =

11 Analysis of Sequential Search Summary Case Number of Key Comparisons T(n) Best CaseT(n) = 1 Worse CaseT(n) = n Average CaseT(n) = (n+1) / 2

12 Sequential Search on Array KEY = 25 50 60 10 20 30 5 4 3 2 1 L U Array A What if the array elements are already sorted? KEY = 25 60 50 30 20 10 5 4 3 2 1 L U Array A Searching stops here. Searching stops here.

13 Searching Use of Sequential Search: –Works fine if the list is small. –Example: Find a name in a list of names of registered students. –Question: Will sequential search be efficient if you need to search the word ‘symbiosis’ in the dictionary? –Yes. »If you want to pass your time! Answer is obviously, –‘No’.

14 Searching Sequential Search: –Conclusion: If the array / list is not sorted: –No option. –We need to go for sequential searching. If the array is sorted: –Sequential search is not efficient. –We need to go for some other way of searching. »Binary Search.

15 Binary Search 15 10 25 11 35 12 45 13 55 14 65 15 75 16 Array A LU KEY = 55 Index (Page No.) We need to go to mid / middle of the array. Question: How to calculate mid of the array because index does not start from 1? mid = 10 / 2 = 5Does not work. mid = 16 / 2 = 8Does not work. mid = (10+16) / 2 = 13Works. mid = (L+U) / 2 Question: This works because there are odd number of elements. What if there are even number of elements in the array. 85 17 mid = (10+17) / 2 = 13.5 mid = (L+U) / 2mid = floor(13.5) = 13

16 Binary Search 15 10 25 11 35 12 45 13 55 14 65 15 75 16 Array A LU KEY = 45 Index (Page No.) mid = floor((L+U)/2)) Is A[mid] = KEY? Yes. Searching stops here. mid mid = floor((10+16)/2) mid = floor(26/2) mid = 13

17 Binary Search 15 10 25 11 35 12 45 13 55 14 65 15 75 16 Array A LU KEY = 65 Index (Page No.) mid = floor((L+U)/2)) = floor((10+16)/2) = floor(26/2) = 13 Is A[mid] = KEY? No.Is KEY > A[mid] OR KEY < A[mid] mid = floor((L+U)/2)) = floor((14+16)/2) = floor(30/2) = 15 KEY > A[mid] mid L = mid + 1

18 Binary Search 15 10 25 11 35 12 45 13 55 14 65 15 75 16 Array A LU KEY = 25 Index (Page No.) mid = floor((L+U)/2)) = floor((10+16)/2) = floor(26/2) = 13 Is A[mid] = KEY? No.Is KEY > A[mid] OR KEY < A[mid] mid = floor((L+U)/2)) = floor((10+12)/2) = floor(22/2) = 11 KEY < A[mid] mid U = mid - 1

19 Binary Search 15 10 25 11 35 12 45 13 55 14 65 15 75 16 Array A LU KEY = 55 Index (Page No.) mid = floor((L+U)/2)) = floor((10+16)/2) = floor(26/2) = 13 mid = floor((L+U)/2)) = floor((14+16)/2) = floor(30/2) = 15 mid mid = floor((L+U)/2)) = floor((14+14)/2) = floor(28/2) = 14 mid

20 Binary Search 15 10 25 11 35 12 45 13 55 14 65 15 75 16 Array A LU KEY = 40 Index (Page No.) mid = floor((L+U)/2)) = floor((10+16)/2) = floor(26/2) = 13 mid = floor((L+U)/2)) = floor((10+12)/2) = floor(22/2) = 11 mid mid = floor((L+U)/2)) = floor((12+12)/2) = floor(24/2) = 12 mid UL L KEY not found

21 Binary Search Algorithm: –BinarySearch Input: –KEY: Value to be searched. Output: –LOCATION: Index of the KEY if successful, otherwise NULL. Data Structure: –Array A[L...U] sorted in Ascending order.

22 Steps of BinarySearch found = 0, location = NULL While(L <= U) AND (found = 0), do mid = floor((L+U)/2) If(KEY = A[mid]), then found = 1 location = mid Else If(KEY > A[mid]), then L = mid + 1 Else U = mid – 1 EndIf EndWhile If(found = 0) print “Search Unsuccessful” Else print “Search Successful” EndIf Return(location) Stop

23 Tracing of Binary Search Array A Index 15 1 25 2 35 3 45 4 65 5 75 6 85 7 LU 95 8 KEY = 75 L = 1 U = 8 15 1 25 2 35 3 45 4 65 5 75 6 85 7 L U 95 8 L = 1 U = 8 mid = (floor(1+8)/2) = 4 KEY > A[4] L = mid + 1 = 4 + 1 = 5 L = 5 U = 8 mid = (floor(5+8)/2) = 6 KEY = A[6] found = 1 location = 6 15 1 25 2 35 3 45 4 65 5 75 6 85 7 L U 95 8 Input Iteration-1 Iteration-2 mid Search Successful.

24 Tracing of Binary Search Array A Index 15 1 25 2 35 3 45 4 65 5 75 6 85 7 LU 95 8 KEY = 55 L = 1 U = 8 15 1 25 2 35 3 45 4 65 5 75 6 85 7 L U 95 8 L = 1 U = 8 mid = (floor(1+8)/2) = 4 KEY > A[4] L = mid + 1 = 4 + 1 = 5 L = 5 U = 8 mid = (floor(5+8)/2) = 6 KEY < A[6] U = mid – 1 = 6 – 1 = 5 15 1 25 2 35 3 45 4 65 5 75 6 85 7 LU 95 8 Input Iteration-1 Iteration-2 mid L = 5 U = 5 mid = (floor(5+5)/2) = 5 KEY < A[5] U = mid – 1 = 5 – 1 = 4 15 1 25 2 35 3 45 4 65 5 75 6 85 7 LU 95 8 Iteration-3 mid Search Unsuccessful.

25 Analysis of Binary Search Summary Case Number of Key Comparisons T(n) Best CaseT(n) = 1 Worse CaseT(n) = log 2 n Average CaseT(n) = log 2 n

26 Sequential v/s Binary Search CaseSequential SearchBinary Search Best CaseT(n) = 1 Worse CaseT(n) = nT(n) = log 2 n Average CaseT(n) = (n+1) / 2T(n) = log 2 n


Download ppt "Searching Searching: –Mainly used for: Fetching / Retrieving the Information such as, –Select query on a database. –Important thing is: Retrieval of Information."

Similar presentations


Ads by Google