Binarno iskanje je iskalna tehnika, ki deluje na Pristop deli in vladaj . Uporablja se za iskanje katerega koli elementa v razvrščeni matriki. V primerjavi z linearnim je binarno iskanje veliko hitrejše s časovno kompleksnostjo O(logN), medtem ko linearno iskanje deluje pri časovni kompleksnosti O(N).
velikost vektorja c++
Primeri :
Input : arr[] = {1, 3, 5, 7, 8, 9}, x = 5 Output : Element found! Input : arr[] = {1, 3, 5, 7, 8, 9}, x = 6 Output : Element not found!> Opomba: Ob predpostavki, da je niz razvrščen.
To so naslednji načini za binarno iskanje v JavaScriptu:
Kazalo
Rekurzivni pristop:
- OSNOVNI POGOJ: Če je začetni indeks večji od končnega indeksa, vrne false.
- Izračunajte srednji indeks.
- Primerjaj srednji element s številom x. Če je enako, vrni true.
- Če je večji, pokličite isto funkcijo s končnim indeksom = middle-1 in ponovite 1. korak.
- Če je manjši, pokličite isto funkcijo z začetnim indeksom = middle+1 in ponovite 1. korak.
primer: Ta primer prikazuje uporabo zgoraj razloženega pristopa.
javascript
rezina polja java
let recursiveFunction =>function> (arr, x, start, end) {> >// Base Condition> >if> (start>konec)>return> false>;> >// Find the middle index> >let mid = Math.floor((start + end) / 2);> >// Compare mid with given key x> >if> (arr[mid] === x)>return> true>;> >// If element at mid is greater than x,> >// search in the left half of mid> >if> (arr[mid]>x)> >return> recursiveFunction(arr, x, start, mid - 1);> >else> >// If element at mid is smaller than x,> >// search in the right half of mid> >return> recursiveFunction(arr, x, mid + 1, end);> }> // Driver code> let arr = [1, 3, 5, 7, 8, 9];> let x = 5;> if> (recursiveFunction(arr, x, 0, arr.length - 1)) {> >console.log(>'Element found!'>);> }> else> { console.log(>'Element not found!'>); }> x = 6;> if> (recursiveFunction(arr, x, 0, arr.length - 1)) {> >console.log(>'Element found!'>);> }> else> { console.log(>'Element not found!'>); }> |
>
>Izhod
Element found! Element not found!>
Časovna zapletenost: O(logN)
Pomožni prostor: O(1)
Iterativni pristop:
Pri tem iterativnem pristopu namesto rekurzije uporabljamo zanko while in zanka teče, dokler ne doseže osnovnega pogoja, tj. začetek postane večji od konca.
primer: Ta primer prikazuje uporabo zgoraj razloženega pristopa.
javascript
normalne oblike
// Iterative function to implement Binary Search> let iterativeFunction =>function> (arr, x) {> >let start = 0, end = arr.length - 1;> >// Iterate while start not meets end> >while> (start <= end) {> >// Find the mid index> >let mid = Math.floor((start + end) / 2);> >// If element is present at> >// mid, return True> >if> (arr[mid] === x)>return> true>;> >// Else look in left or> >// right half accordingly> >else> if> (arr[mid] start = mid + 1; else end = mid - 1; } return false; } // Driver code let arr = [1, 3, 5, 7, 8, 9]; let x = 5; if (iterativeFunction(arr, x, 0, arr.length - 1)) { console.log('Element found!'); } else { console.log('Element not found!'); } x = 8; if (iterativeFunction(arr, x, 0, arr.length - 1)) { console.log('Element found!'); } else { console.log('Element not found!'); }> |
>
>
1 milijon v številkahIzhod
Element found! Element found!>
Časovna zapletenost: O(logN).
Pomožni prostor: O(1)