U ovom ćemo članku raspravljati o algoritmu binarnog pretraživanja. Pretraživanje je proces pronalaženja određenog elementa na popisu. Ako je element prisutan na popisu, tada se proces naziva uspješnim, a proces vraća lokaciju tog elementa. U suprotnom, pretraga se naziva neuspješnom.
Linearno pretraživanje i binarno pretraživanje dvije su popularne tehnike pretraživanja. Ovdje ćemo raspravljati o algoritmu binarnog pretraživanja.
Binarno pretraživanje je tehnika pretraživanja koja učinkovito radi na sortiranim popisima. Dakle, da bismo pretražili element na nekom popisu koristeći tehniku binarnog pretraživanja, moramo osigurati da je popis sortiran.
Binarno pretraživanje slijedi pristup podijeli pa vladaj u kojem je popis podijeljen na dvije polovice, a stavka se uspoređuje sa srednjim elementom popisa. Ako se tada pronađe podudaranje, vraća se lokacija srednjeg elementa. U suprotnom, tražimo bilo koje od poluvremena ovisno o rezultatu utakmice.
NAPOMENA: Binarno pretraživanje može se implementirati na sortirane elemente niza. Ako elementi popisa nisu posloženi na sortirani način, prvo ih moramo sortirati.
Pogledajmo sada algoritam binarnog pretraživanja.
Algoritam
Binary_Search(a, lower_bound, upper_bound, val) // 'a' is the given array, 'lower_bound' is the index of the first array element, 'upper_bound' is the index of the last array element, 'val' is the value to search Step 1: set beg = lower_bound, end = upper_bound, pos = - 1 Step 2: repeat steps 3 and 4 while beg val set end = mid - 1 else set beg = mid + 1 [end of if] [end of loop] Step 5: if pos = -1 print 'value is not present in the array' [end of if] Step 6: exit
Rad binarnog pretraživanja
Sada, da vidimo kako radi algoritam binarnog pretraživanja.
Da bismo razumjeli rad algoritma binarnog pretraživanja, uzmimo sortirani niz. Bit će lako razumjeti rad binarnog pretraživanja na primjeru.
Postoje dvije metode za implementaciju algoritma binarnog pretraživanja -
- Iterativna metoda
- Rekurzivna metoda
Rekurzivna metoda binarnog pretraživanja slijedi pristup podijeli pa vladaj.
Neka su elementi niza -
Neka je element za pretraživanje, K = 56
Moramo koristiti donju formulu za izračun sredina niza -
mid = (beg + end)/2
Dakle, u zadanom nizu -
moliti = 0
kraj = 8
sredina = (0 + 8)/2 = 4. Dakle, 4 je sredina niza.
Sada je pronađen element za pretraživanje. Dakle, algoritam će vratiti indeks elementa koji se podudara.
Složenost binarnog pretraživanja
Pogledajmo sada vremensku složenost binarnog pretraživanja u najboljem, prosječnom i najgorem slučaju. Također ćemo vidjeti prostornu složenost binarnog pretraživanja.
1. Vremenska složenost
Slučaj | Vremenska složenost |
---|---|
Najbolji slučaj | O(1) |
Prosječan slučaj | O (prijava) |
Najgori slučaj | O (prijava) |
2. Složenost prostora
Složenost prostora | O(1) |
- Prostorna složenost binarnog pretraživanja je O(1).
Implementacija binarnog pretraživanja
Pogledajmo sada programe binarnog pretraživanja u različitim programskim jezicima.
Program: Napišite program za implementaciju binarnog pretraživanja u C jeziku.
#include int binarySearch(int a[], int beg, int end, int val) { int mid; if(end >= beg) { mid = (beg + end)/2; /* if the item to be searched is present at middle */ if(a[mid] == val) { return mid+1; } /* if the item to be searched is smaller than middle, then it can only be in left subarray */ else if(a[mid] <val) { return binarysearch(a, mid+1, end, val); } * if the item to be searched is greater than middle, then it can only in right subarray else beg, mid-1, -1; int main() a[]="{11," 14, 25, 30, 40, 41, 52, 57, 70}; given array val="40;" value n="sizeof(a)" sizeof(a[0]); size of res="binarySearch(a," 0, n-1, store result printf('the elements are - '); for (int i="0;" < n; i++) printf('%d ', a[i]); printf(' element %d', (res="=" -1) not present array'); at %d position array', res); 0; pre> <p> <strong>Output</strong> </p> <img src="//techcodeview.com/img/ds-tutorial/10/binary-search-algorithm-5.webp" alt="Binary Search Algorithm"> <p> <strong>Program:</strong> Write a program to implement Binary search in C++.</p> <pre> #include using namespace std; int binarySearch(int a[], int beg, int end, int val) { int mid; if(end >= beg) { mid = (beg + end)/2; /* if the item to be searched is present at middle */ if(a[mid] == val) { return mid+1; } /* if the item to be searched is smaller than middle, then it can only be in left subarray */ else if(a[mid] <val) { return binarysearch(a, mid+1, end, val); } * if the item to be searched is greater than middle, then it can only in right subarray else beg, mid-1, -1; int main() a[]="{10," 12, 24, 29, 39, 40, 51, 56, 70}; given array val="51;" value n="sizeof(a)" sizeof(a[0]); size of res="binarySearch(a," 0, n-1, store result cout<<'the elements are - '; for (int i="0;" < n; i++) cout< <a[i]<<' cout<<' element '<<val; (res="=" -1) not present array'; at '<<res<<' position 0; pre> <p> <strong>Output</strong> </p> <img src="//techcodeview.com/img/ds-tutorial/10/binary-search-algorithm-6.webp" alt="Binary Search Algorithm"> <p> <strong>Program:</strong> Write a program to implement Binary search in C#.</p> <pre> using System; class BinarySearch { static int binarySearch(int[] a, int beg, int end, int val) { int mid; if(end >= beg) { mid = (beg + end)/2; if(a[mid] == val) { return mid+1; /* if the item to be searched is present at middle */ } /* if the item to be searched is smaller than middle, then it can only be in left subarray */ else if(a[mid] <val) { return binarysearch(a, mid+1, end, val); } * if the item to be searched is greater than middle, then it can only in right subarray else beg, mid-1, -1; static void main() int[] a="{9," 11, 23, 28, 38, 45, 50, 56, 70}; given array int val="70;" value n="a.Length;" size of res="binarySearch(a," 0, n-1, store result console.write('the elements are - '); for (int i="0;" < n; i++) console.write(a[i] + ' console.writeline(); console.writeline('element (res="=" -1) not present array'); at position pre> <p> <strong>Output</strong> </p> <img src="//techcodeview.com/img/ds-tutorial/10/binary-search-algorithm-7.webp" alt="Binary Search Algorithm"> <p> <strong>Program:</strong> Write a program to implement Binary search in Java.</p> <pre> class BinarySearch { static int binarySearch(int a[], int beg, int end, int val) { int mid; if(end >= beg) { mid = (beg + end)/2; if(a[mid] == val) { return mid+1; /* if the item to be searched is present at middle */ } /* if the item to be searched is smaller than middle, then it can only be in left subarray */ else if(a[mid] <val) { return binarysearch(a, mid+1, end, val); } * if the item to be searched is greater than middle, then it can only in right subarray else beg, mid-1, -1; public static void main(string args[]) int a[]="{8," 10, 22, 27, 37, 44, 49, 55, 69}; given array val="37;" value n="a.length;" size of res="binarySearch(a," 0, n-1, store result system.out.print('the elements are: '); for (int i="0;" < n; i++) system.out.print(a[i] + ' system.out.println(); system.out.println('element is: (res="=" -1) not present array'); at position pre> <p> <strong>Output</strong> </p> <img src="//techcodeview.com/img/ds-tutorial/10/binary-search-algorithm-8.webp" alt="Binary Search Algorithm"> <p> <strong>Program:</strong> Write a program to implement Binary search in PHP.</p> <pre> = $beg) { $mid = floor(($beg + $end)/2); if($a[$mid] == $val) { return $mid+1; /* if the item to be searched is present at middle */ } /* if the item to be searched is smaller than middle, then it can only be in left subarray */ else if($a[$mid] <$val) { return binarysearch($a, $mid+1, $end, $val); } * if the item to be searched is greater than middle, then it can only in right subarray else $beg, $mid-1, -1; $a="array(7," 9, 21, 26, 36, 43, 48, 54, 68); given array $val="68;" value $n="sizeof($a);" size of $res="binarySearch($a," 0, $n-1, store result echo 'the elements are: '; for ($i="0;" $i < $n; $i++) ' , $a[$i]; <br>' , 'Element to be searched is: ' , $val; if ($res == -1) echo ' <br>' , 'Element is not present in the array'; else echo ' <br>' , 'Element is present at ' , $res , ' position of array'; ?> </$val)></pre> <p> <strong>Output</strong> </p> <img src="//techcodeview.com/img/ds-tutorial/10/binary-search-algorithm-9.webp" alt="Binary Search Algorithm"> <p>So, that's all about the article. Hope the article will be helpful and informative to you.</p> <hr></val)></pre></val)></pre></val)></pre></val)>
Izlaz
Dakle, to je sve o članku. Nadamo se da će vam članak biti koristan i informativan.