Professional Documents
Culture Documents
Analysis and Design of Algorithm: B. Tech. (CSE), VII Semester Bhagyashree Naruka
Analysis and Design of Algorithm: B. Tech. (CSE), VII Semester Bhagyashree Naruka
Algorithm
BinarySearch(S, k, low,high) S is sorted input, k is element you want to search If (low>high) then return Position(NULL) Else mid<- (low+ high)/2 if k=key(mid) then return Position(mid) else if k<key(mid) then return BinarySearch(S,k,low,mid-1) else return BinarySearch(S,k,mid+1,high)
Binary Search
Binary search. Given value and sorted array a[], find index i such that a[i] = value, or report that no such index exists.
6
0
13
1
14
2
25
3
33
4
43
5
51
6
53
7
64
8
72
9
84
10
93
11
95
12
96
13
97
14
lo
hi
Binary Search
Binary search. Given value and sorted array a[], find index i such that a[i] = value, or report that no such index exists.
6
0
13
1
14
2
25
3
33
4
43
5
51
6
53
7
64
8
72
9
84
10
93
11
95
12
96
13
97
14
lo
mid
hi
Binary Search
Binary search. Given value and sorted array a[], find index i such that a[i] = value, or report that no such index exists.
6
0
13
1
14
2
25
3
33
4
43
5
51
6
53
7
64
8
72
9
84
10
93
11
95
12
96
13
97
14
lo
hi
Binary Search
Binary search. Given value and sorted array a[], find index i such that a[i] = value, or report that no such index exists.
6
0
13
1
14
2
25
3
33
4
43
5
51
6
53
7
64
8
72
9
84
10
93
11
95
12
96
13
97
14
lo
mid
hi
Binary Search
Binary search. Given value and sorted array a[], find index i such that a[i] = value, or report that no such index exists.
6
0
13
1
14
2
25
3
33
4
43
5
51
6
53
7
64
8
72
9
84
10
93
11
95
12
96
13
97
14
lo
hi
Binary Search
Binary search. Given value and sorted array a[], find index i such that a[i] = value, or report that no such index exists.
6
0
13
1
14
2
25
3
33
4
43
5
51
6
53
7
64
8
72
9
84
10
93
11
95
12
96
13
97
14
lo
mid
hi
Binary Search
Binary search. Given value and sorted array a[], find index i such that a[i] = value, or report that no such index exists.
6
0
13
1
14
2
25
3
33
4
43
5
51
6
53
7
64
8
72
9
84
10
93
11
95
12
96
13
97
14
lo hi
Binary Search
Binary search. Given value and sorted array a[], find index i such that a[i] = value, or report that no such index exists.
6
0
13
1
14
2
25
3
33
4
43
5
51
6
53
7
64
8
72
9
84
10
93
11
95
12
96
13
97
14
lo hi mid
Binary Search
Binary search. Given value and sorted array a[], find index i such that a[i] = value, or report that no such index exists.
6
0
13
1
14
2
25
3
33
4
43
5
51
6
53
7
64
8
72
9
84
10
93
11
95
12
96
13
97
14
lo hi mid
Complexity of binary is O(logn) Every time input size is reduced by n/2 so T(n) = T(n/2)