Algorithm Search binary In Java - Hirgelinta & amp; Tusaalooyinka

Tababarkaan wuxuu sharxi doonaa raadinta Binary & Raadinta Binary Recursive ee Java oo ay la socoto Algorithm-keeda, Hirgelinta, iyo Java Tusaalooyinka Koodhka Raadinta ee Binary-ga:

Baaritaan laba-geesoodka ah ee Java waa farsamo loo isticmaalo in lagu raadiyo qiime la beegsaday ama furaha ururinta. Waa farsamo adeegsata farsamada "qaybi oo guulaysata" si ay furaha u raadiso

Ururinta lagu raadinayo Binary search si loo raadiyo furaha waxay u baahan tahay in loo kala saaro siday u korto.

Sida caadiga ah, inta badan luuqadaha barnaamijka waxay taageeraan raadinta toosan, raadinta binary, iyo farsamooyinka Hashing ee loo isticmaalo raadinta xogta ururinta. Waxaan ku baran doonaa hashing casharradayada xiga.

>>> Search Binary In Java> Raadinta khadka tooska ah waa farsamo aasaasi ah. Farsamadan, arrayga ayaa si isdaba joog ah loo maro, shay walbana waxaa lala barbar dhigayaa furaha ilaa furaha laga helayo ama la gaarayo dhamaadka shaxanka.

Baaritaan toosan ayaa si dhif ah loo adeegsadaa codsiyada dhabta ah. Raadinta binary waa farsamada ugu badan ee la isticmaalo maadaama ay aad uga dhaqso badan tahay raadinta toosan

>Java waxay ku siinaysaa saddex siyaabood oo loo sameeyo raadinta binary: >

>>>
  • Isticmaalka Habka soo noqnoqda
  • Isticmaalka hab-nololeedka
  • Isticmaalka Arrays.binarySearch () habka.
  • > 10>

    > Casharradan, waxaan fulin doonnaa oo ka doodi doonnaa dhammaan kuwan 3 hababkaHabka raadinta, ururinta marar badan ayaa loo qaybiyaa kala badh iyo qaybta muhiimka ah ee lagu baadho qaybta bidix ama midig ee ururinta iyadoo ku xiran haddii furuhu ka yar yahay ama ka weyn yahay qaybta dhexe ee ururinta.

    5>Algorithm-ka fudud ee Binary Search waa sida soo socota: >

    >
      >
    1. Xisaabi qaybta dhexe ee ururinta.
    2. Is barbar dhig walxaha muhiimka ah iyo qaybta dhexe
    3. 8>Haddii furaha = element dhexe, markaas waxaan ku soo celinaynaa booska index dhexe ee furaha la helay.
    4. Haddii kale furaha > element dhexe, ka dibna furuhu wuxuu ku yaalaa qaybta saxda ah ee ururinta. Markaa ku celi tillaabooyinka 1 ilaa 3 xagga hoose (midig) kala badh ururinta.
    5. Furaha kale element dhexe, ka dibna furuhu wuxuu ku yaalaa qaybta sare ee ururinta. Markaa waxaad u baahan tahay inaad ku celiso raadinta binary qaybta sare.
    6. >
    >Sida aad ka arki karto tillaabooyinka kore, raadinta Binary, kala badh walxaha ku jira ururinta waa la iska indhatiray ka dib isbarbardhigga koowaad. 1>

    Ogsoonow in isku xigxiga tillaabooyinka ay hayaan soo noqnoqoshada iyo sidoo kale raadinta binary-ga ee soo noqnoqda.

    Aan tusaaleyno algorithm raadinta binary anagoo tusaale u adeegsanayna.

    Tusaale ahaan, soo qaado 10 curiye oo la kala soocay. >

    >

    Aan xisaabinno meesha dhexe ee shaxdu. >

    >

    Mid = 0+9/2 = 4

    >

    #1) Furaha = 21

    Marka hore, waxaanu is barbar dhigi doonaa qiimaha furaha element [mid] waxaanan ogaanay in curiyaha uu qiimahiisu yahaydhexe = 21.

    >Sidaas darteed waxaynu ku helaynaa furahaas = [mid]. Markaa furaha waxaa laga helay booska 4 ee shaxda.> #2) Furaha = 25>

    >

    Waxaan marka hore is barbar dhignay furaha qiimaha ilaa bartamaha. Sida (21 & lt; 25), waxaanu si toos ah u raadin doonaa furaha qaybta sare ee shaxanka.

    >

    Hadda mar labaad waxaanu heli doonaa badhtamaha qaybta sare ee isku xirka is barbar dhig curiyaha muhiimka ah iyo curiyaha dhexe. Markaa (25 == 25), markaa waxaanu furaha ka helnay goobta [mid] = 6.

    Sidaas awgeed waxaanu si isdaba joog ah u kala qaybinnaa shaxanka, anagoo is barbar dhignay qaybta muhiimka ah iyo badhtanka, waxaanu go'aansanay badhkii raadi furaha. Raadinta binary waa mid aad u hufan marka la eego waqtiga iyo saxda ah sidoo kale aad ayuu u dhaqso badan yahay.

    > 11> Hirgelinta raadinta Binary Java

    Anoo adeegsanayna algorithm-ka sare, aan ku hirgelino barnaamijka raadinta Binary ee Java anagoo adeegsanayna hab ku celcelin. Barnaamijkan, waxaanu soo qaadanaynaa tusaale array oo aanu ku samaynayna raadinta binary array-kan.

    import java.util.*; class Main{ public static void main(String args[]){ int numArray[] = {5,10,15,20,25,30,35}; System.out.println("The input array: " + Arrays.toString(numArray)); //key to be searched int key = 20; System.out.println("\nKey to be searched=" + key); //set first to first index int first = 0; //set last to last elements in array int last=numArray.length-1; //calculate mid of the array int mid = (first + last)/2; //while first and last do not overlap while( first = last ){ //if the mid  key, then key to be searched is in the first half of array if ( numArray[mid]  last ){ System.out.println("Element is not found!"); } } } 

    Output: >

    The input array: [5, 10, 15, 20] . waxay tusinaysaa hab raaca raadinta Binary. Marka hore, array ayaa lagu dhawaaqaa, ka dibna furaha la raadinayo ayaa la qeexaa.

    Marka la xisaabiyo bartamaha shaxanka, furaha waxaa la barbar dhigayaa qaybta dhexe. Markaa waxay ku xidhan tahay iyo in kalefuraha waa ka yar yahay ama ka weyn yahay furaha, furaha waxaa laga baarayaa qeybta hoose ama sare ee array sida ay u kala horreeyaan.

    Recursive Binary Search In Java

    Waxa kale oo aad samayn kartaa raadinta binary iyadoo la isticmaalayo farsamada soo noqnoqda. Halkan, habka raadinta binary-ga waxaa loogu yeeraa si isdaba joog ah ilaa furaha laga helo ama liiska oo dhan uu dhammaado.

    Barnaamijka fulinaya raadinta binary-ga ee soo noqnoqda waxaa lagu bixiyaa hoos: > 1>

    import java.util.*; class Main{ //recursive method for binary search public static int binary_Search(int intArray[], int low, int high, int key){ //if array is in order then perform binary search on the array if (high>=low){ //calculate mid int mid = low + (high - low)/2; //if key =intArray[mid] return mid if (intArray[mid] == key){ return mid; } //if intArray[mid] > key then key is in left half of array if (intArray[mid] > key){ return binary_Search(intArray, low, mid-1, key);//recursively search for key }else //key is in right half of the array { return binary_Search(intArray, mid+1, high, key);//recursively search for key } } return -1; } public static void main(String args[]){ //define array and key int intArray[] = {1,11,21,31,41,51,61,71,81,91}; System.out.println("Input List: " + Arrays.toString(intArray)); int key = 31; System.out.println("\nThe key to be searched:" + key); int high=intArray.length-1; //call binary search method int result = binary_Search(intArray,0,high,key); //print the result if (result == -1) System.out.println("\nKey not found in given list!"); else System.out.println("\nKey is found at location: "+result + " in the list"); } } 

    Wax soo saarka: >

    >

    Liiska galitaanka: [1, 11, 21, 31, 41, 51, 61, 71, 81, 91

    Furaha la raadinayo :

    > Furaha waxaa laga helay goobta: 3 ee liiska>>Isticmaalka habka Arrays.binarySearch ().

    Fasalka Arrays ee Java waxa uu bixiyaa habka 'binarySearch ()' kaas oo sameeya raadinta binary-ga Array-ga la bixiyay. Habkani waxa uu ka dhigayaa shaxanka iyo furaha in la baadho dood ahaan waxana uu soo celinayaa booska furaha ee shaxanka. Haddii furaha aan la helin, markaa habka ayaa soo noqda -1.

    >

    >Tusaalaha hoose wuxuu fuliyaa habka Arrays.binarySearch () >

    import java.util.Arrays; class Main{ public static void main(String args[]){ //define an array int intArray[] = {10,20,30,40,50,60,70,80,90}; System.out.println("The input Array : " + Arrays.toString(intArray)); //define the key to be searched int key = 50; System.out.println("\nThe key to be searched:" + key); //call binarySearch method on the given array with key to be searched int result = Arrays.binarySearch(intArray,key); //print the return result if (result  0) System.out.println("\nKey is not found in the array!"); else System.out.println("\nKey is found at index: "+result + " in the array."); } } 

    > Wax soo saar: >

    Array-ga gelinta Furaha waxaa laga helaa tusmada: 4 ee shaxda ? >

    >

    Jawab: Baadi-goobka binary-ga waxaa badanaa lagu sameeyaa iyadoo loo qaybinayo kala badh. Haddii furaha la raadinayo uu ka weyn yahay qaybta dhexe,ka dibna qaybta sare ee shaxanka ayaa la baadhi doonaa iyada oo la sii kala qaybinayo lana baadhayo qaybta hoose ilaa laga helayo furaha

    Si la mid ah, haddii uu furuhu ka yar yahay qaybta dhexe, furaha waxa laga baadhayaa xagga hoose. kala badh shaxanka

    Q #2) Halkee laga isticmaalaa raadinta binary?. Kala soocida xogta ku jirta barnaamijyada software gaar ahaan marka meesha xusuusta ay kooban tahay oo xaddidan tahay. > Q #3) Waa maxay O ee ugu weyn ee raadinta binary?>>

    >Jawaab : Waqtiga kakanaanta raadinta binary waa O (logn) halkaasoo n ay tahay tirada curiyeyaasha ku jira array. Kakanaanta booska goob raadinta binary waa O (1)

    Q #4 Maadaama raadinta binary ay tusaale u tahay istiraatijiyadda qaybi-iyo-guuleysiga waxaana lagu hirgelin karaa iyadoo la isticmaalayo dib-u-soo-noqosho. Waxaan u kala qaybin karnaa shaxanka kala badh oo u wac isla habka si aan u samayno raadinta binary marar badan. Jawab: Algorithm raadinta binary waxay isticmaashaa xeelad qaybi-iyo-guulaysi taasoo marar badan u kala gooysa shaxanka kala badh ama laba qaybood. Sidaas awgeed waxaa loogu magac daray raadinta binary.

    > Gabagabo

    Raadinta binary waa farsamada inta badan lagu isticmaalo Java. Shuruuda raadinta binary in la sameeyo waa in xogta loo kala soocaa siday u korto.

    Baaritaan binary ah ayaa noqon karala hirgeliyey iyadoo la isticmaalayo hab ku celcelin ah ama soo noqnoqonaysa. Fasalka Arrays ee Java waxa kale oo uu bixiyaa habka 'binarySearch' kaas oo sameeya raadinta binary ee Array.

    > > Casharradayada xiga, waxaan ku sahamin doonaa Farsamooyin kala duwan oo Java ah. 23>
    Dabool ilaa sare