Spaces:
Sleeping
Sleeping
| // Binary Search | |
| a1 = arr[1, 3, 5, 7, 9, 11, 19] | |
| key = 11 | |
| fun Binary_Search(<table> collection, target, size): | |
| low = 0 | |
| high = size - 1 | |
| while (low <= high): | |
| // Calculate middle index | |
| mid = (low + high) / 2 | |
| // Get value at the middle | |
| <mid, highlight, yellow> val = collection[mid] | |
| if (val == target): | |
| <mid, highlight, green> return mid | |
| end if | |
| if (val < target): | |
| <low, outline, yellow> low = mid + 1 | |
| end if | |
| if (val > target): | |
| <high, outline, yellow> high = mid - 1 | |
| end if | |
| end while | |
| <warn, "Not found"> return -1 | |
| end fun | |
| result = Binary_Search(a1, key, length(a1)) |