searching searching: –mainly used for: fetching / retrieving the information such as, –select...
TRANSCRIPT
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.
SearchingClassification
Tree Search
Non-Linear Search
Sequential Search
Binary Search
Linear Search
Graph Search
Array Search
Linked ListSearch
Binary TreeSearch
AVL Tree Search
Sequential Search on Array
11
20
9
18
15
0
1
2
3
4
Array A
Index
9i = 0While (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
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]
Algorithm: SequentialSearchSteps:i = L, found = 0, location = NULL
While (i <= U) && (found == 0), doIf( A[ i ] == KEY ), then
found = 1location = i
EndIfi = i + 1
EndWhile
If (found == 0), thenprint “Search is unsuccessful.”
Elseprint “Search is successful.”
EndIfReturn(location)Stop
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.
Analysis of Sequential Search
•Complexity Analysis:–Best Case:
•KEY is present in the array as the 1st element.•We will need only 1 comparison for searching.
–Number of comparisons:»T(n) = 1.
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
Analysis of Sequential Search
•Complexity Analysis:–Average Case:
•KEY is present at any location in the array.•Let pi be the probability that the key may be present at the i-th location. (1 <= i <= n)
–Number of comparisons,
i = 1
i = n
pi * iT(n) =
Analysis of Sequential Search
i = 1
i = n
pi * 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) =
Analysis of Sequential Search
Summary
CaseNumber of Key Comparisons
T(n)
Best Case T(n) = 1
Worse Case T(n) = n
Average Case T(n) = (n+1) / 2
Sequential Search on Array
KEY = 25
50
60
10
20
30
5
4
3
2
1L
U
Array A
What if the array elements are already sorted?
KEY = 25
60
50
30
20
10
5
4
3
2
1L
U
Array A
Searching stops here.
Searching stops here.
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’.
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.
Binary Search
15
10
25
11
35
12
45
13
55
14
65
15
75
16
Array A
L U
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 = 5 Does not work.
mid = 16 / 2 = 8 Does not work.
mid = (10+16) / 2 = 13 Works. 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) / 2 mid = floor(13.5) = 13
Binary Search
15
10
25
11
35
12
45
13
55
14
65
15
75
16
Array A
L U
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
Binary Search
15
10
25
11
35
12
45
13
55
14
65
15
75
16
Array A
L U
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 mid
L = mid + 1
Binary Search
15
10
25
11
35
12
45
13
55
14
65
15
75
16
Array A
L U
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]
midmid
U = mid - 1
Binary Search
15
10
25
11
35
12
45
13
55
14
65
15
75
16
Array A
L U
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
mid = floor((L+U)/2)) = floor((14+14)/2) = floor(28/2) = 14
mid
Binary Search
15
10
25
11
35
12
45
13
55
14
65
15
75
16
Array A
L U
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 ULLmid
KEY not found
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.
Steps of BinarySearchfound = 0, location = NULLWhile(L <= U) AND (found = 0), do
mid = floor((L+U)/2)If(KEY = A[mid]), then
found = 1location = mid
ElseIf(KEY > A[mid]), then
L = mid + 1 Else
U = mid – 1EndIf
EndIfEndWhileIf(found = 0)
print “Search Unsuccessful”Else
print “Search Successful”EndIfReturn(location)Stop
Tracing of Binary Search
Array A
Index
15
1
25
2
35
3
45
4
65
5
75
6
85
7L U
95
8
KEY = 75L = 1U = 8
15
1
25
2
35
3
45
4
65
5
75
6
85
7L U
95
8
L = 1U = 8mid = (floor(1+8)/2) = 4KEY > A[4]L = mid + 1 = 4 + 1 = 5
L = 5U = 8mid = (floor(5+8)/2) = 6KEY = A[6]found = 1location = 6
15
1
25
2
35
3
45
4
65
5
75
6
85
7L U
95
8
Input
Iteration-1
Iteration-2
mid
mid
Search Successful.
Tracing of Binary SearchArray A
Index
15
1
25
2
35
3
45
4
65
5
75
6
85
7L U
95
8
KEY = 55L = 1U = 8
15
1
25
2
35
3
45
4
65
5
75
6
85
7L U
95
8
L = 1U = 8mid = (floor(1+8)/2) = 4KEY > A[4]L = mid + 1 = 4 + 1 = 5
L = 5U = 8mid = (floor(5+8)/2) = 6KEY < A[6]U = mid – 1 = 6 – 1 = 5
15
1
25
2
35
3
45
4
65
5
75
6
85
7L U
95
8
Input
Iteration-1
Iteration-2
mid
mid
L = 5U = 5mid = (floor(5+5)/2) = 5KEY < A[5]U = mid – 1 = 5 – 1 = 4
15
1
25
2
35
3
45
4
65
5
75
6
85
7LU
95
8
Iteration-3
midSearch Unsuccessful.
Analysis of Binary Search
Summary
CaseNumber of Key Comparisons
T(n)
Best Case T(n) = 1
Worse Case T(n) = log2n
Average Case T(n) = log2n