0% found this document useful (0 votes)
28 views12 pages

Analysis and Design of Algorithm: B. Tech. (CSE), VII Semester Bhagyashree Naruka

The document describes binary search, an algorithm for finding a target value within a sorted array. It works by repeatedly dividing the search interval in half and determining if the target is in the upper or lower half. This maintains the invariant that the target is between the minimum and maximum of the current search subarray. The complexity of binary search is O(log n) as each iteration cuts the problem size in half.

Uploaded by

Sourav Sharma
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views12 pages

Analysis and Design of Algorithm: B. Tech. (CSE), VII Semester Bhagyashree Naruka

The document describes binary search, an algorithm for finding a target value within a sorted array. It works by repeatedly dividing the search interval in half and determining if the target is in the upper or lower half. This maintains the invariant that the target is between the minimum and maximum of the current search subarray. The complexity of binary search is O(log n) as each iteration cuts the problem size in half.

Uploaded by

Sourav Sharma
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 12

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.

Invariant. Algorithm maintains a[lo] value a[hi].

Ex. Binary search for 33.

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.

Invariant. Algorithm maintains a[lo] value a[hi].

Ex. Binary search for 33.

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.

Invariant. Algorithm maintains a[lo] value a[hi].

Ex. Binary search for 33.

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.

Invariant. Algorithm maintains a[lo] value a[hi].

Ex. Binary search for 33.

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.

Invariant. Algorithm maintains a[lo] value a[hi].

Ex. Binary search for 33.

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.

Invariant. Algorithm maintains a[lo] value a[hi].

Ex. Binary search for 33.

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.

Invariant. Algorithm maintains a[lo] value a[hi].

Ex. Binary search for 33.

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.

Invariant. Algorithm maintains a[lo] value a[hi].

Ex. Binary search for 33.

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.

Invariant. Algorithm maintains a[lo] value a[hi].

Ex. Binary search for 33.

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)

You might also like