• Call Us: +91 9437911966
  • Email Us: [email protected]
  • Home
  • C Examples
  • Server Side
    • PHP
    • Java
    • CodeIgniter
    • Laravel
    • JSP
    • JDBC
  • Frontend
    • HTML
    • CSS
    • Javascript
    • jQuery
    • AJAX
    • FPDF
    • TCPDF
  • APP & DB
    • Android
    • Cordova
    • SQL
    • Oracle
  • Project
Java Introduction System.out.println(): Program for input through console Program for addition Program for Subtraction Program for Multiplication Program for Division Program for modulus Program for increment Program for percentage Program for square Program for cube Program for square root Check Even or Odd Reverse of a number Find prime numbers Find factorial Program for palindrome Program for Diamond Star Program for Pascal Star Program for Fibonacci Series Find largest number Find Smallest number Find largest third number Find largest second number Program for Bubble Sort Program for Insertion Sort Program for Selection Sort Program for Linear Search Program for Binary Search Find IP address Open notepad through java program Find Armstrong Number Find mirror clock Comparing Value create Backup file Program for file content modifier Program for copying files Fahrenheit to Celsius create deadlock program Implement hashcode program Convert File to Zip Remove Duplicate Element From Array Java Matrix Addition Java Matrix Multiplication

Java program for Binary Search

  • Students Tutorial
   

bubble-sort.java

public class SmallestInArrayExample{  
public static int getSmallest(int[] a, int total){   
int temp;   
for (int i = 0; i < total; i++)    
        {   
            for (int j = i + 1; j < total; j++)    
            {   
                if (a[i] > a[j])    
                {   
                    temp = a[i];   
                    a[i] = a[j];   
                    a[j] = temp;   
                }   
            }   
        }   
       return a[0];   
}   
public static void main(String args[]){   
int a[]={1,2,4,6,3,2};   
int b[]={44,66,88,77,33,22,55};   
System.out.println("Smallest: "+getSmallest(a,6));   
System.out.println("Smallest: "+getSmallest(b,7));   
} } 

linear_search.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] < key ){  
		first = mid + 1;     
	  }else if ( arr[mid] == key ){  
		System.out.println("Element is found at index: " + mid);  
		break;  
	  }else{  
		 last = mid - 1;  
	  }  
	  mid = (first + last)/2;  
   }  
   if ( first > last ){  
	  System.out.println("Element is not found!");  
   }  
 }  
 public static void main(String args[]){  
		int arr[] = {1,2,3,4,5,6,7};  
		int key = 3;  
		int last=arr.length-1;  
		binarySearch(arr,0,last,key);     
 }  
}   

Student Tutorial is a online tutorial for web design and development. Here you can find easy example and explanation for each tutorial.
Copyright © 2016-2021 Student Tutorial

Server Side

  • PHP
  • Java
  • CodeIgniter
  • Laravel
  • JSP
  • JDBC

Frontend

  • HTML
  • CSS
  • Javascript
  • jQuery
  • AJAX
  • FPDF

App & Database

  • Android
  • Cordova
  • SQL
  • Oracle