fun binary_search(list, item) { var low = 0; var high = list.len() - 1; while low <= high { var mid = (low + high) / 2; var guess = list[mid]; // printf("low: {}, high: {}, mid: {}, guess: {}", low, high, mid, guess); if guess == item { return mid; } else if guess > item { high = mid - 1; } else { low = mid + 1; } } return nil; } var elements = []; var n = 1000; var t1 = clock(); for (var i = 0; i < n; i++) { elements.push(rand_int(0, n)); } elements.sort(); // printf("elements: {}", elements); var t2 = clock(); printf("time taken to create array: {} ms", t2 - t1); var search = elements[n / 2]; var t1 = clock(); var index = binary_search(elements, search); var t2 = clock(); if index == nil { printf("element {} not found", search); } else { printf("element {} is at index: {}", search, index); } printf("time taken: {} ms", t2 - t1);