JS бинарный поиск

function BinarySearch(t,A) // t - искомый элемент, { // A - упорядоченный массив, в котором ищем. var i = 0, j = A.length, k; while (i < j) { k = Math.floor((i+j)/2); if (t <= A[k]) j = k; else i = k+1; } if (A[ i ] === t) return i; // На выходе индекс искомого элемента. else return -1; // Если искомого элемента нет в массиве, то -1. }

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.