Binary Search Algorithm: Implementation and Functioning - Prof. Marjory Baruch, Study notes of Computer Science

The implementation of the binary search algorithm as presented in hanly and koffman's textbook. The algorithm is explained step by step, with variables and their functions defined. This resource is ideal for computer science students studying algorithms and data structures.

Typology: Study notes

Pre 2010

Uploaded on 08/09/2009

koofers-user-f6a
koofers-user-f6a 🇺🇸

10 documents

1 / 1

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Binary Search Algorithm
p429 Hanly and Koffman
int binarysearch(int theList[],
int highIndex, int target)
1. Let bottom be the subscript of the initial
array element.
2. Let top be the subscript of the last array
element.
3. Let found be false.
4. Repeat as long as bottom isn't greater than
top and the target has not been found.
a. Let middle be the subscript of the
element halfway between bottom and
top.
b. if the element at middle is the target
i. Set found to true and index to
middle.
else if the element at middle is larger
than the target
ii. Let top be middle-1.
else
iii. Let bottom be middle + 1.
5. If the target was found
a. return the index .
else
b. return -1

Partial preview of the text

Download Binary Search Algorithm: Implementation and Functioning - Prof. Marjory Baruch and more Study notes Computer Science in PDF only on Docsity!

Binary Search Algorithm p429 Hanly and Koffman int binarysearch(int theList[], int highIndex, int target)

  1. Let bottom be the subscript of the initial array element.
  2. Let top be the subscript of the last array element.
  3. Let found be false.
  4. Repeat as long as bottom isn't greater than top and the target has not been found. a. Let middle be the subscript of the element halfway between bottom and top. b. if the element at middle is the target i. Set found to true and index to middle. else if the element at middle is larger than the target ii. Let top be middle - 1. else iii. Let bottom be middle + 1.
  5. If the target was found a. return the index. else b. return - 1