Docsity

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

Toggle sidebar
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