Binary search
public class Main{
public static void main(String[] args) {
double[] x = { -39, -3, 6, 10, 4, 9, 10 };
double value = 8;
int lower = 0, upper = x.length - 1;
while (lower <= upper) {
int middle = (lower + upper) / 2;
if (value > x[middle])
lower = middle + 1;
else if (value < x[middle])
upper = middle - 1;
else
break;
}
if (lower > upper)
System.out.println("Not found");
else
System.out.println("Found");
}
}
Output:
Not found
Home
Java Book
Runnable examples
Java Book
Runnable examples
Algorithm:
- Binary search
- Binary Search Insert
- Recursive Binary Search Implementation in Java
- Linear Searching double Arrays
- Bubble sort
- Heap sort
- Merge sort
- Fast Merge Sort
- Fast Quick Sort
- Simple version of quick sort
- Quicksort for sorting arrays
- Quick Sort Implementation with median-of-three partitioning and cutoff for small arrays
- Quick sort with median-of-three partitioning
- Insert sort
- Insert Sort for objects
- Selection sort
- Shell sort
- Fibonacci
- Hanoi puzzle
- Table of fahrenheit and celsius temperatures
- Growable integer array
- Linked List class
- Binary Tree
- Tree with generic user object