Данный отчёт сгенерирован 10.02.2023 18:21:31 UTC.
HTML версия (этот сайт) сгенерирован 10.02.2023 18:21:38 UTC.
Коммит: [bfeb65b9] add automatic zip creation 10.02.2023 18:21:31
HTML версия (этот сайт) сгенерирован 10.02.2023 18:21:38 UTC.
Коммит: [bfeb65b9] add automatic zip creation 10.02.2023 18:21:31
Задача: Двійковий Пошук
10.02.2023 18:21:31
Описание: Визначте функцію binarySearch – двійкового пошуку в масиві.
Функція приймає два параметри: ключ та масив
10.02.2023 18:21:31
Решений: 39
10.02.2023 18:21:31
Двійковий Пошук
10.02.2023 18:21:31
function binarySearch (key, arr){
return arr.indexOf(key)
}
function binarySearch(key, arr) {
let start = 0;
let end = arr.length - 1;
let mid = Math.ceil((start + end) / 2);;
let copyMid;
while(start < end){
mid = Math.ceil((start + end) / 2);
if(mid == copyMid)
mid = Math.floor((start + end) / 2);;
copyMid = mid;
if(arr[mid] < key){
start = mid;
}
else if(arr[mid] > key){
end = mid;
}
else if(arr[mid] === key){
return mid;
}
}
return -1;
}
function binarySearch (key, arr){return arr.includes(key)?arr.indexOf(key):-1}
function binarySearch (key, arr){
for(let i = 0;i< arr.length;i++){if(arr[i]==key){return i};}
return -1;
}
function binarySearch (key,arr,x) {
if (x >= arr.length) {
return -1;
}
if (arr[x] == key) {
return x;
}
return binarySearch(key, arr, x + 1);
}
function binarySearch (key, arr){
let start = 0;
let end = arr.length - 1;
while (start <= end) {
let middle = Math.floor((start + end) / 2);
if (arr[middle] === key) {
// found the key
return middle;
} else if (arr[middle] < key) {
// continue searching to the right
start = middle + 1;
} else {
// search searching to the left
end = middle - 1;
}
}
// key wasn't found
return -1;
}
function binarySearch (key, arr){
let start = 0;
let end = arr.length - 1;
while (start <= end) {
let middle = Math.floor((start + end) / 2);
if (arr[middle] === key) {
return middle;
} else if (arr[middle] < key) {
start = middle + 1;
} else {
end = middle - 1;
}
}
return -1;
}
function binarySearch(key, arr) {
let first=0;
let last=arr.length-1;
let p=-1;
let found=false;
let middle;
while(found===false && first<=last){
middle=Math.floor((first+last)/2);
if(arr[middle]==key){
found=true;
p=middle;
}
else if(arr[middle]>key){
last=middle-1;
}
else{
first=middle+1;
}
}
return p
}
function binarySearch(key, arr) {
for (let b=0, e=arr.length; e>=b; ){
let k = Math.floor((b+e)/2);
if (arr[k]==undefined) break;
if (arr[k] > key) e = k-1;
else if (arr[k] < key) b = k+1;
else return k;
}
return (-1);
}
function binarySearch (key, arr)
{
let begin = 0
let end = arr.length-1
while(begin<=end)
{
let s = Math.floor((begin+end)/2)
if(arr[s]==key)
{
return s
}
else if(arr[s]< key)
{
begin = s+1
}
else if(arr[s]>key)
{
end = s-1
}
}
return -1
}
function binarySearch (key, arr) {
let End = arr.length-1;
let Begin = 0;
while(Begin <= End){
let m = Math.floor((End-Begin)/2 + Begin)
if(key == arr[m])
return m
if(key > arr[m])
Begin = ++m
else
End = --m
}
return -1;
}
function binarySearch (key, arr) {
let first = 0;
let last = arr.length - 1;
while (first <= last){
let middle = Math.floor(first + (last - first)/2);
if (key == arr[middle]) return middle;
if (key > arr[middle]) first = ++middle;
else last = --middle;
}
return -1;
}
function binarySearch(k, a) {
let l = 0, r = a.length;
if(a[l] > k || a[r-1] < k)
return -1;
while(l < r) {
let m = Math.floor((l + r) / 2);
if(a[m] < k) l = m;
if(a[m] > k) r = m;
if(a[m] == k) return m;
}
if(a[l] == k)
return l;
return -1;
}
function binarySearch(key, arr) {
let begin = 0;
let end = arr.length;
if (arr[arr.length-1] < key || arr[0] > key) return -1;
while (begin < end){
if (arr[Math.floor((begin+end)/2)] == key) return Math.floor((begin+end)/2);
else if (arr[Math.floor((begin+end)/2)] <= key){
begin = Math.floor((begin+end)/2);
}
else end = Math.floor((begin+end)/2);
}
}
function binarySearch(key, arr)
{
let i = Math.floor(arr.length / 2);
if (arr[i] == key)
return i;
else if (arr[i] > key)
for (i; i >= 0; i--)
{
if (arr[i] == key)
return i;
}
else if (arr[i] < key)
for (i; i < arr.length; i++)
{
if (arr[i] == key)
return i;
}
return -1;
}
function binarySearch (key, arr){
let start = 0;
let end = arr.length - 1;
while (start <= end) {
let middle = Math.floor((start + end) / 2);
if (arr[middle] === key) {
return middle;
}
else if (arr[middle] < key) {
start = middle + 1;
}
else {
end = middle - 1;
}
}
return -1;
}
function binarySearch(key, arr) {
let length = Math.trunc((arr.length)/2);
let position = length;
while (position !=-1 && position< arr.length){
if(arr[position] == key){
return position;
}
if(length > 1)length = Math.trunc(length/2);
if(arr[position] > key){
position -= length;
}
else{
position += length;
}
}
return -1;
}
function binarySearch(key, arr) {
let low = 0;
let high = arr.length - 1;
while (low <= high) {
let mid = Math.floor((low + high) / 2);
if (arr[mid] < key) {
low = mid + 1;
} else if (arr[mid] > key) {
high = mid - 1;
} else {
return mid;
}
}
return -1;
}
let arr = [1, 2, 3, 4, 5];
let key = 3;
let result = binarySearch(key, arr);
console.log(result);
function binarySearch (key, arr) {
let s = 0, e = arr.length-1, m=0;
while(1) {
if (e >= s) {
m = Math.floor((e + s) / 2);
if (arr[m] == key) {
return m;
} else if (arr[m] > key) {
e = m - 1;
continue;
} else if (arr[m] < key) {
s = m + 1;
continue;
}
continue;
}
return -1;
}
}
function binarySearch (key,arr,x) {
if (x >= arr.length) {
return -1;
}
if (arr[x] === key) {
return x;
}
return binarySearch(key, arr, x+1 );
}
function binarySearch (key, arr) {
let first = 0;
let last = arr.length - 1;
while (first <= last){
let middle = Math.floor(first + (last - first)/2);
if (key == arr[middle]) return middle;
if (key > arr[middle]) first = ++middle;
else last = --middle;
}
return -1;
}
function binarySearch(value, list) {
let found = false;
let position = -1;
let index = 0;
while(!found && index < list.length) {
if(list[index] == value) {
found = true;
position = index;
} else {
index += 1;
}
}
return position;
}
function binarySearch (key, arr, left = 0, right = arr.length){
if (right === left) return -1
if (arr[Math.floor((right + left) / 2)] === key) return Math.floor((right + left) / 2)
else if (arr[Math.floor((right + left) / 2)] > key) return binarySearch(key, arr, left, Math.floor((right + left) / 2))
else if (arr[Math.floor((right + left) / 2)] < key) return binarySearch(key, arr, Math.floor((right + left) / 2) + 1, right)
}
function binarySearch (key, arr)
{
let left = 0;
let rigth = arr.length;
index = Math.floor(arr.slice(left,rigth).length/2)
for(let i =0;;i++)
{
if(arr.slice(left,rigth).length==0) return -1;
if(arr[index]==key) return index;
else if(arr[index]>key)
{
left = 0;
rigth = index;
index = Math.floor(arr.slice(left,rigth).length/2)
}
else if(arr[index]< key)
{
rigth = arr.slice(left,rigth).length
left = index;
index = index + Math.floor(arr.slice(left,rigth).length/2)
}
}
}
function binarySearch (key, arr){
let mid=Math.floor(arr.length/2)
let searchArea=arr
let searchMid=mid
while(mid< arr.length&&mid>0){
if(arr[mid]>key){
searchArea=searchArea.slice(0,searchMid)
searchMid=Math.floor(searchArea.length/2)
mid=mid-searchMid-searchArea.length%2
}else
if(arr[mid]< key){
searchArea=searchArea.slice(searchMid+1)
searchMid=Math.floor(searchArea.length/2)
mid=mid+1+searchMid
}
if(arr[mid]===key){
return mid
}
}
return -1
}
function binarySearch (key, arr){
let n = Math.floor(arr.length / 2);
if(arr[n] == key) return n;
for(let i = n - 1; i >= 0; i--){
if(arr[i] == key) return i
}
for(let i = n; i < arr.length; i++){
if(arr[i] == key) return i
}
return -1
}
function binarySearch (key, arr) {
let start = 0;
let end = arr.length-1;
while (start <= end) {
let mid = Math.floor((start + end)/2);
if (arr[mid] == key) {
return mid;
} else if (arr[mid] < key) {
start = mid + 1;
} else {
end = mid - 1;
}
}
return -1;
}
function binarySearch(key, arr) {
let start = 0;
let end = arr.length - 1;
function g(start, end) {
if (start > end) return -1;
let mid = Math.floor((start + end) / 2);
if (arr[mid] === key) return mid;
if (arr[mid] > key) return g(start, mid - 1);
else return g(mid + 1, end);
}
return g(start, end);
}
function binarySearch (key, arr) {
let end = arr.length-1;
let start = 0;
while(start <= end){
let m = Math.floor((end-start)/2 + start)
if(key == arr[m])
return m
if(key > arr[m])
start = ++m
else
end = --m
}
return -1;
}
function binarySearch(key, array) {
let left = 0;
let right = array.length - 1;
while (left <= right) {
let middle = Math.floor((left + right) / 2);
if (array[middle] === key) {
return middle;
} else if (array[middle] < key) {
left = middle + 1;
} else {
right = middle - 1;
}
}
return -1;
}
function binarySearch (key, arr){
function next(i) {
if (arr[i] == key) {
return i;
} else if (i == arr.length - 1) {
return -1;
} else {
return next(i + 1);
}
}
return next(0);
}
function binarySearch(key, arr,x=0){
if (x===arr.length){
return -1
}
if (arr[x]===key){
return x
}
x=x+1
return binarySearch(key,arr,x)
}
function binarySearch (key, arr){
let start = 0
let finish = arr.length - 1
while (start <= finish){
let mid = Math.floor((start + finish)/2)
if(arr[mid] == key){
return mid
}
if(arr[mid] > key){
finish = mid - 1
}
else{
start = mid + 1
}
}
return -1
}
function binarySearch (key, arr){
return arr.indexOf(key)
}
function binarySearch (key, arr) {
let end = arr.length-1;
let start = 0;
while(start <= end){
let m = Math.floor((end-start)/2 + start)
if(key == arr[m])
return m
if(key > arr[m])
start = ++m
else
end = --m
}
return -1;
}
function binarySearch (key, arr){
let start = 0;
let end = arr.length - 1;
while(start <= end){
let center = start + Math.floor((end - start) / 2);
if(arr[center] == key){
return center;
}
else if(arr[center] > key){
end = center - 1;
}
else if(arr[center] < key){
start = center + 1;
}
}
return -1;
}
function binarySearch(key, arr) {
let n = 0; // ліво
let m = arr.length - 1; // право
let g; // центр
while (n <= m) {
g = Math.round(m-n/2) + n;
if (key === arr[g]){
return g
}
else if (key < arr[g]) { // якщо меньше центра, то відкидаємо праву частину
m = g - 1;
}
else { // якщо більше
n = g + 1;
}
}
return -1
}
function binarySearch (key, arr){
let m = Math.floor(arr.length/2);
if(arr[m] > key){
for(let i = 0; i < m; i++){
if(arr[i] == key){
return i;
}
}
}else {
for(let i = m; i < arr.length; i++){
if(arr[i] == key){
return i;
}
}
}
return -1;
}
function binarySearch (key,arr,x) {
if (x >= arr.length) {
return -1;
}
if (arr[x] == key) {
return x;
}
return binarySearch(key, arr, x + 1);
}
return arr.indexOf(key)
}
let start = 0;
let end = arr.length - 1;
let mid = Math.ceil((start + end) / 2);;
let copyMid;
while(start < end){
mid = Math.ceil((start + end) / 2);
if(mid == copyMid)
mid = Math.floor((start + end) / 2);;
copyMid = mid;
if(arr[mid] < key){
start = mid;
}
else if(arr[mid] > key){
end = mid;
}
else if(arr[mid] === key){
return mid;
}
}
return -1;
}
for(let i = 0;i< arr.length;i++){if(arr[i]==key){return i};}
return -1;
}
if (x >= arr.length) {
return -1;
}
if (arr[x] == key) {
return x;
}
return binarySearch(key, arr, x + 1);
}
let start = 0;
let end = arr.length - 1;
while (start <= end) {
let middle = Math.floor((start + end) / 2);
if (arr[middle] === key) {
// found the key
return middle;
} else if (arr[middle] < key) {
// continue searching to the right
start = middle + 1;
} else {
// search searching to the left
end = middle - 1;
}
}
// key wasn't found
return -1;
}
let start = 0;
let end = arr.length - 1;
while (start <= end) {
let middle = Math.floor((start + end) / 2);
if (arr[middle] === key) {
return middle;
} else if (arr[middle] < key) {
start = middle + 1;
} else {
end = middle - 1;
}
}
return -1;
}
let first=0;
let last=arr.length-1;
let p=-1;
let found=false;
let middle;
while(found===false && first<=last){
middle=Math.floor((first+last)/2);
if(arr[middle]==key){
found=true;
p=middle;
}
else if(arr[middle]>key){
last=middle-1;
}
else{
first=middle+1;
}
}
return p
}
for (let b=0, e=arr.length; e>=b; ){
let k = Math.floor((b+e)/2);
if (arr[k]==undefined) break;
if (arr[k] > key) e = k-1;
else if (arr[k] < key) b = k+1;
else return k;
}
return (-1);
}
{
let begin = 0
let end = arr.length-1
while(begin<=end)
{
let s = Math.floor((begin+end)/2)
if(arr[s]==key)
{
return s
}
else if(arr[s]< key)
{
begin = s+1
}
else if(arr[s]>key)
{
end = s-1
}
}
return -1
}
let End = arr.length-1;
let Begin = 0;
while(Begin <= End){
let m = Math.floor((End-Begin)/2 + Begin)
if(key == arr[m])
return m
if(key > arr[m])
Begin = ++m
else
End = --m
}
return -1;
}
let first = 0;
let last = arr.length - 1;
while (first <= last){
let middle = Math.floor(first + (last - first)/2);
if (key == arr[middle]) return middle;
if (key > arr[middle]) first = ++middle;
else last = --middle;
}
return -1;
}
let l = 0, r = a.length;
if(a[l] > k || a[r-1] < k)
return -1;
while(l < r) {
let m = Math.floor((l + r) / 2);
if(a[m] < k) l = m;
if(a[m] > k) r = m;
if(a[m] == k) return m;
}
if(a[l] == k)
return l;
return -1;
}
let begin = 0;
let end = arr.length;
if (arr[arr.length-1] < key || arr[0] > key) return -1;
while (begin < end){
if (arr[Math.floor((begin+end)/2)] == key) return Math.floor((begin+end)/2);
else if (arr[Math.floor((begin+end)/2)] <= key){
begin = Math.floor((begin+end)/2);
}
else end = Math.floor((begin+end)/2);
}
}
{
let i = Math.floor(arr.length / 2);
if (arr[i] == key)
return i;
else if (arr[i] > key)
for (i; i >= 0; i--)
{
if (arr[i] == key)
return i;
}
else if (arr[i] < key)
for (i; i < arr.length; i++)
{
if (arr[i] == key)
return i;
}
return -1;
}
let start = 0;
let end = arr.length - 1;
while (start <= end) {
let middle = Math.floor((start + end) / 2);
if (arr[middle] === key) {
return middle;
}
else if (arr[middle] < key) {
start = middle + 1;
}
else {
end = middle - 1;
}
}
return -1;
}
let length = Math.trunc((arr.length)/2);
let position = length;
while (position !=-1 && position< arr.length){
if(arr[position] == key){
return position;
}
if(length > 1)length = Math.trunc(length/2);
if(arr[position] > key){
position -= length;
}
else{
position += length;
}
}
return -1;
}
let low = 0;
let high = arr.length - 1;
while (low <= high) {
let mid = Math.floor((low + high) / 2);
if (arr[mid] < key) {
low = mid + 1;
} else if (arr[mid] > key) {
high = mid - 1;
} else {
return mid;
}
}
return -1;
}
let arr = [1, 2, 3, 4, 5];
let key = 3;
let result = binarySearch(key, arr);
console.log(result);
let s = 0, e = arr.length-1, m=0;
while(1) {
if (e >= s) {
m = Math.floor((e + s) / 2);
if (arr[m] == key) {
return m;
} else if (arr[m] > key) {
e = m - 1;
continue;
} else if (arr[m] < key) {
s = m + 1;
continue;
}
continue;
}
return -1;
}
}
if (x >= arr.length) {
return -1;
}
if (arr[x] === key) {
return x;
}
return binarySearch(key, arr, x+1 );
}
let first = 0;
let last = arr.length - 1;
while (first <= last){
let middle = Math.floor(first + (last - first)/2);
if (key == arr[middle]) return middle;
if (key > arr[middle]) first = ++middle;
else last = --middle;
}
return -1;
}
let found = false;
let position = -1;
let index = 0;
while(!found && index < list.length) {
if(list[index] == value) {
found = true;
position = index;
} else {
index += 1;
}
}
return position;
}
if (right === left) return -1
if (arr[Math.floor((right + left) / 2)] === key) return Math.floor((right + left) / 2)
else if (arr[Math.floor((right + left) / 2)] > key) return binarySearch(key, arr, left, Math.floor((right + left) / 2))
else if (arr[Math.floor((right + left) / 2)] < key) return binarySearch(key, arr, Math.floor((right + left) / 2) + 1, right)
}
{
let left = 0;
let rigth = arr.length;
index = Math.floor(arr.slice(left,rigth).length/2)
for(let i =0;;i++)
{
if(arr.slice(left,rigth).length==0) return -1;
if(arr[index]==key) return index;
else if(arr[index]>key)
{
left = 0;
rigth = index;
index = Math.floor(arr.slice(left,rigth).length/2)
}
else if(arr[index]< key)
{
rigth = arr.slice(left,rigth).length
left = index;
index = index + Math.floor(arr.slice(left,rigth).length/2)
}
}
}
let mid=Math.floor(arr.length/2)
let searchArea=arr
let searchMid=mid
while(mid< arr.length&&mid>0){
if(arr[mid]>key){
searchArea=searchArea.slice(0,searchMid)
searchMid=Math.floor(searchArea.length/2)
mid=mid-searchMid-searchArea.length%2
}else
if(arr[mid]< key){
searchArea=searchArea.slice(searchMid+1)
searchMid=Math.floor(searchArea.length/2)
mid=mid+1+searchMid
}
if(arr[mid]===key){
return mid
}
}
return -1
}
let n = Math.floor(arr.length / 2);
if(arr[n] == key) return n;
for(let i = n - 1; i >= 0; i--){
if(arr[i] == key) return i
}
for(let i = n; i < arr.length; i++){
if(arr[i] == key) return i
}
return -1
}
let start = 0;
let end = arr.length-1;
while (start <= end) {
let mid = Math.floor((start + end)/2);
if (arr[mid] == key) {
return mid;
} else if (arr[mid] < key) {
start = mid + 1;
} else {
end = mid - 1;
}
}
return -1;
}
let start = 0;
let end = arr.length - 1;
function g(start, end) {
if (start > end) return -1;
let mid = Math.floor((start + end) / 2);
if (arr[mid] === key) return mid;
if (arr[mid] > key) return g(start, mid - 1);
else return g(mid + 1, end);
}
return g(start, end);
}
let end = arr.length-1;
let start = 0;
while(start <= end){
let m = Math.floor((end-start)/2 + start)
if(key == arr[m])
return m
if(key > arr[m])
start = ++m
else
end = --m
}
return -1;
}
let left = 0;
let right = array.length - 1;
while (left <= right) {
let middle = Math.floor((left + right) / 2);
if (array[middle] === key) {
return middle;
} else if (array[middle] < key) {
left = middle + 1;
} else {
right = middle - 1;
}
}
return -1;
}
function next(i) {
if (arr[i] == key) {
return i;
} else if (i == arr.length - 1) {
return -1;
} else {
return next(i + 1);
}
}
return next(0);
}
if (x===arr.length){
return -1
}
if (arr[x]===key){
return x
}
x=x+1
return binarySearch(key,arr,x)
}
let start = 0
let finish = arr.length - 1
while (start <= finish){
let mid = Math.floor((start + finish)/2)
if(arr[mid] == key){
return mid
}
if(arr[mid] > key){
finish = mid - 1
}
else{
start = mid + 1
}
}
return -1
}
return arr.indexOf(key)
}
let end = arr.length-1;
let start = 0;
while(start <= end){
let m = Math.floor((end-start)/2 + start)
if(key == arr[m])
return m
if(key > arr[m])
start = ++m
else
end = --m
}
return -1;
}
let start = 0;
let end = arr.length - 1;
while(start <= end){
let center = start + Math.floor((end - start) / 2);
if(arr[center] == key){
return center;
}
else if(arr[center] > key){
end = center - 1;
}
else if(arr[center] < key){
start = center + 1;
}
}
return -1;
}
let n = 0; // ліво
let m = arr.length - 1; // право
let g; // центр
while (n <= m) {
g = Math.round(m-n/2) + n;
if (key === arr[g]){
return g
}
else if (key < arr[g]) { // якщо меньше центра, то відкидаємо праву частину
m = g - 1;
}
else { // якщо більше
n = g + 1;
}
}
return -1
}
let m = Math.floor(arr.length/2);
if(arr[m] > key){
for(let i = 0; i < m; i++){
if(arr[i] == key){
return i;
}
}
}else {
for(let i = m; i < arr.length; i++){
if(arr[i] == key){
return i;
}
}
}
return -1;
}
if (x >= arr.length) {
return -1;
}
if (arr[x] == key) {
return x;
}
return binarySearch(key, arr, x + 1);
}