Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions java prog
Original file line number Diff line number Diff line change
@@ -1,29 +1,28 @@
import java.util.Scanner;

// Binary Search in Java

class Main {
int binarySearch(int array[], int element, int low, int high) {

// Repeat until the pointers low and high meet each other
while (low <= high) {

// get index of mid element
int mid = low + (high - low) / 2
int mid = low + (high - low) / 2;

// if element to be searched is the mid element
if (array[mid] == element)
return mid
return mid;

// if element is less than mid element
// search only the left side of mid
if (array[mid] < element)
low = mid + 1
low = mid + 1;

// if element is greater than mid element
// search only the right side of mid
else
high = mid - 1
high = mid - 1;
}

return -1;
Expand Down