File size: 799 Bytes
70f40ac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// Binary Search

a1 = arr[1, 3, 5, 7, 9, 11, 19]
key = 11

fun Binary_Search(@init<table> collection, target, size): 
    low = 0
    high = size - 1

    while (low <= high):
        // Calculate middle index
        mid = (low + high) / 2
        
        // Get value at the middle
        @mark<mid, highlight, yellow> val = collection[mid]
        
        if (val == target):
            @mark<mid, highlight, green> return mid
        end if
        
        if (val < target):
            @mark<low, outline, yellow> low = mid + 1
        end if
        
        if (val > target):
            @mark<high, outline, yellow> high = mid - 1
        end if
    end while

    @log<warn, "Not found"> return -1
end fun

result = Binary_Search(a1, key, length(a1))