RosettaCodeData/Task/Binary-search/Vedit-macro-language/binary-search.vedit

31 lines
665 B
Plaintext

// Main program for testing BINARY_SEARCH
#3 = Get_Num("Value to search: ")
EOF
#2 = Cur_Line // hi
#1 = 1 // lo
Call("BINARY_SEARCH")
Message("Value ") Num_Type(#3, NOCR)
if (Return_Value < 1) {
Message(" not found\n")
} else {
Message(" found at index ") Num_Type(Return_Value)
}
return
:BINARY_SEARCH:
while (#1 <= #2) {
#12 = (#1 + #2) / 2
Goto_Line(#12)
#11 = Num_Eval()
if (#3 == #11) {
return(#12) // found
} else {
if (#3 < #11) {
#2 = #12-1
} else {
#1 = #12+1
}
}
}
return(0) // not found