Vous êtes sur la page 1sur 9

1.

4 Binary Search

Algorithms, 4th Edition

Robert Sedgewick and Kevin Wayne

Copyright 20022011

June 25, 2011 1:17:59 PM

Binary search Goal. Given a sorted array and a key, find index of the key in the array? Successful search. 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 Goal. Given a sorted array and a key, find index of the key in the array? Successful search. 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 Goal. Given a sorted array and a key, find index of the key in the array? Successful search. 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 Goal. Given a sorted array and a key, find index of the key in the array? Successful search. Binary search for 33.

lo = hi

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

mid return 4

Binary search Goal. Given a sorted array and a key, find index of the key in the array? Unsuccessful search. Binary search for 34.

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 Goal. Given a sorted array and a key, find index of the key in the array? Unsuccessful search. Binary search for 34.

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 Goal. Given a sorted array and a key, find index of the key in the array? Unsuccessful search. Binary search for 34.

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 Goal. Given a sorted array and a key, find index of the key in the array? Unsuccessful search. Binary search for 34.

lo = hi

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

mid return -1

Vous aimerez peut-être aussi