logo

البحث الثنائي في جافا

يستخدم البحث الثنائي للبحث عن عنصر رئيسي من عناصر متعددة. البحث الثنائي أسرع من البحث الخطي.

استثناء رمي جافا

في حالة البحث الثنائي، يجب أن تكون عناصر المصفوفة مرتبة تصاعديًا. إذا كان لديك مصفوفة غير مصنفة، فيمكنك فرز المصفوفة باستخدام المصفوفات.الفرز(arr) طريقة.

مثال البحث الثنائي في جافا

دعونا نرى مثالاً للبحث الثنائي في Java.

 class BinarySearchExample{ public static void binarySearch(int arr[], int first, int last, int key){ int mid = (first + last)/2; while( first <= last ){ if ( arr[mid] system.out.println('element is not found!'); } public static void main(string args[]){ int arr[]="{10,20,30,40,50};" key="30;" binarysearch(arr,0,last,key); < pre> <span> Test it Now </span> <p>Output:</p> <pre> Element is found at index: 2 </pre> <h2>Binary Search Example in Java using Recursion</h2> <p>Let&apos;s see an example of binary search in java where we are going to search an element from an array using recursion.</p> <pre> class BinarySearchExample1{ public static int binarySearch(int arr[], int first, int last, int key){ if (last&gt;=first){ int mid = first + (last - first)/2; if (arr[mid] == key){ return mid; } if (arr[mid] &gt; key){ return binarySearch(arr, first, mid-1, key);//search in left subarray }else{ return binarySearch(arr, mid+1, last, key);//search in right subarray } } return -1; } public static void main(String args[]){ int arr[] = {10,20,30,40,50}; int key = 30; int last=arr.length-1; int result = binarySearch(arr,0,last,key); if (result == -1) System.out.println(&apos;Element is not found!&apos;); else System.out.println(&apos;Element is found at index: &apos;+result); } } </pre> <span> Test it Now </span> <p>Output:</p> <pre> Element is found at index: 2 </pre> <h2>Binary Search Example in Java using Arrays.binarySearch()</h2> <pre> import java.util.Arrays; class BinarySearchExample2{ public static void main(String args[]){ int arr[] = {10,20,30,40,50}; int key = 30; int result = Arrays.binarySearch(arr,key); if (result <0) system.out.println('element is not found!'); else found at index: '+result); } < pre> <span> Test it Now </span> <p>Output:</p> <pre> Element is found at index: 2 </pre></0)></pre></=>

مثال للبحث الثنائي في Java باستخدام العودية

دعونا نرى مثالاً للبحث الثنائي في جافا حيث سنقوم بالبحث عن عنصر من مصفوفة باستخدام العودية.

 class BinarySearchExample1{ public static int binarySearch(int arr[], int first, int last, int key){ if (last&gt;=first){ int mid = first + (last - first)/2; if (arr[mid] == key){ return mid; } if (arr[mid] &gt; key){ return binarySearch(arr, first, mid-1, key);//search in left subarray }else{ return binarySearch(arr, mid+1, last, key);//search in right subarray } } return -1; } public static void main(String args[]){ int arr[] = {10,20,30,40,50}; int key = 30; int last=arr.length-1; int result = binarySearch(arr,0,last,key); if (result == -1) System.out.println(&apos;Element is not found!&apos;); else System.out.println(&apos;Element is found at index: &apos;+result); } } 
اختبره الآن

انتاج:

 Element is found at index: 2 

مثال للبحث الثنائي في Java باستخدام Arrays.binarySearch()

 import java.util.Arrays; class BinarySearchExample2{ public static void main(String args[]){ int arr[] = {10,20,30,40,50}; int key = 30; int result = Arrays.binarySearch(arr,key); if (result <0) system.out.println(\'element is not found!\'); else found at index: \'+result); } < pre> <span> Test it Now </span> <p>Output:</p> <pre> Element is found at index: 2 </pre></0)>