La búsqueda binaria se utiliza para buscar un elemento clave entre varios elementos. La búsqueda binaria es más rápida que la búsqueda lineal.
listas java
En caso de búsqueda binaria, los elementos de la matriz deben estar en orden ascendente. Si tiene una matriz sin ordenar, puede ordenar la matriz usando Arrays.sort(arr) método.
Ejemplo de búsqueda binaria en Java
Veamos un ejemplo de búsqueda binaria en 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'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>=first){ int mid = first + (last - first)/2; if (arr[mid] == key){ return mid; } if (arr[mid] > 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('Element is not found!'); else System.out.println('Element is found at index: '+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></=>
Ejemplo de búsqueda binaria en Java usando recursividad
Veamos un ejemplo de búsqueda binaria en java donde vamos a buscar un elemento de un array usando recursividad.
class BinarySearchExample1{ public static int binarySearch(int arr[], int first, int last, int key){ if (last>=first){ int mid = first + (last - first)/2; if (arr[mid] == key){ return mid; } if (arr[mid] > 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('Element is not found!'); else System.out.println('Element is found at index: '+result); } }Pruébalo ahora
Producción:
Element is found at index: 2
Ejemplo de búsqueda binaria en Java usando 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)>0)>=>