Dvejetainė paieška yra paieškos technika, kuri veikia „Skaldyk ir valdyk“ metodas . Jis naudojamas ieškant bet kurio elemento surūšiuotame masyve. Palyginti su linijine, dvejetainė paieška yra daug greitesnė, kai laiko sudėtingumas yra O(logN), o tiesinė paieška veikia esant O(N) laiko sudėtingumui.
pervardyti linux kataloge
Pavyzdžiai :
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!> Pastaba: Darant prielaidą, kad masyvas yra surūšiuotas.
Tai yra šie būdai, kaip atlikti dvejetainę paiešką „JavaScript“:
Turinys
Rekursyvus metodas:
- PAGRINDINĖ BŪKLĖ: Jei pradžios indeksas yra didesnis nei pabaigos indeksas, grąžinama klaidinga.
- Apskaičiuokite vidurinį indeksą.
- Palyginkite vidurinį elementą su skaičiumi x. Jei lygi, grįš tiesa.
- Jei didesnė, iškvieskite tą pačią funkciją, kurios pabaigos indeksas = Middle-1, ir pakartokite 1 veiksmą.
- Jei mažesnė, iškvieskite tą pačią funkciją pradiniu indeksu = vidurys+1 ir pakartokite 1 veiksmą.
Pavyzdys: Šiame pavyzdyje parodytas aukščiau paaiškinto metodo naudojimas.
javascript
let recursiveFunction =>function> (arr, x, start, end) {> >// Base Condition> >if> (start>pabaiga)>>;> >// 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!'>); }> |
istorija java
>
>Išvestis
Element found! Element not found!>
Laiko sudėtingumas: O(logN)
Pagalbinė erdvė: O(1)
Iteratyvus metodas:
Taikant šį iteracinį metodą, vietoj rekursijos naudojame ciklą while ir ciklas veikia tol, kol pasiekia bazinę sąlygą, t. y. pradžia tampa didesnė už pabaigą.
Pavyzdys: Šiame pavyzdyje parodytas aukščiau paaiškinto metodo naudojimas.
javascript
Java inicijavimo masyvas
// 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!'); }> |
>
>Išvestis
Element found! Element found!>
Laiko sudėtingumas: O(logN).
Pagalbinė erdvė: O(1)