java ( 1460527 ) - java ( 1460536 ) stack: com.thealgorithms.searches.BinarySearch.main(BinarySearch.java:62)
package com.thealgorithms.searches;
import com.thealgorithms.devutils.searches.SearchAlgorithm;
import java.util.Arrays;
import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;
import java.util.stream.IntStream;
/**
* Binary search is one of the most popular algorithms The algorithm finds the
* position of a target value within a sorted array
*
*
* Worst-case performance O(log n) Best-case performance O(1) Average
* performance O(log n) Worst-case space complexity O(1)
*
* @author Varun Upadhyay (https://github.com/varunu28)
* @author Podshivalov Nikita (https://github.com/nikitap492)
* @see SearchAlgorithm
* @see IterativeBinarySearch
*/
class BinarySearch implements SearchAlgorithm {
/**
* @param array is an array where the element should be found
* @param key is an element which should be found
* @param is any comparable type
* @return index of the element
*/
@Override
public > int find(T[] array, T key) {
return search(array, key, 0, array.length - 1);
}
/**
* This method implements the Generic Binary Search
*
* @param array The array to make the binary search
* @param key The number you are looking for
* @param left The lower bound
* @param right The upper bound
* @return the location of the key
*/
private > int search(T[] array, T key, int left, int right) {
if (right < left) {
return -1; // this means that the key not found
}
// find median
int median = (left + right) >>> 1;
int comp = key.compareTo(array[median]);
if (comp == 0) {
return median;
} else if (comp < 0) {
return search(array, key, left, median - 1);
} else {
return search(array, key, median + 1, right);
}
}
// Driver Program
public static void main(String[] args) {
// Just generate data
Random r = ThreadLocalRandom.current();
int size = 100;
int maxElement = 100000;
Integer[] integers = IntStream.generate(() -> r.nextInt(maxElement)).limit(size).sorted().boxed().toArray(Integer[] ::new);
// The element that should be found
int shouldBeFound = integers[r.nextInt(size - 1)];
BinarySearch search = new BinarySearch();
int atIndex = search.find(integers, shouldBeFound);
System.out.printf("Should be found: %d. Found %d at index %d. An array length %d%n", shouldBeFound, integers[atIndex], atIndex, size);
int toCheck = Arrays.binarySearch(integers, shouldBeFound);
System.out.printf("Found by system method at an index: %d. Is equal: %b%n", toCheck, toCheck == atIndex);
}
}
No. | From | Name | Value |
---|---|---|---|
1 | 62 | args | [Ljava.lang.String;@4e25154f |
END | 0 | 0 | 0 |
Process Filter | Thread Filter |
---|---|
1460527 java | 1460536 java |
No. | PN | PID | TID | TN | Message |
---|---|---|---|---|---|
1 | java | 1460527 | 1460536 | java | Should be found: |
2 | java | 1460527 | 1460536 | java | 30428 |
3 | java | 1460527 | 1460536 | java | . Found |
4 | java | 1460527 | 1460536 | java | 30428 |
5 | java | 1460527 | 1460536 | java | at index |
6 | java | 1460527 | 1460536 | java | 33 |
7 | java | 1460527 | 1460536 | java | . An array length |
8 | java | 1460527 | 1460536 | java | 100 |
9 | java | 1460527 | 1460536 | java | |
10 | java | 1460527 | 1460536 | java | Found by system method at an index: |
11 | java | 1460527 | 1460536 | java | 33 |
12 | java | 1460527 | 1460536 | java | . Is equal: |
13 | java | 1460527 | 1460536 | java | true |
14 | java | 1460527 | 1460536 | java | |
END | 0 | 0 | 0 | 0 | 0 |
Functions and Shortcuts
No. | Function | Shortcuts | Description |
---|---|---|---|
1 | GB | Alt + LEFT, Alt + A | Go Backward |
2 | GF | Alt + RIGHT, Alt + D | Go Foreward |
3 | PPE | Alt + UP, Alt + W | Previous Process End |
4 | NPS | Alt + DOWN, Alt + S | Next Process Start |
5 | PB | Ctrl + LEFT, Ctrl + A | current Process Backward |
6 | PF | Ctrl + RIGHT, Ctrl + D | current Process Foreward |
7 | PPTE | Ctrl + UP, Ctrl + W | go to current Process's Previous Thread's End |
8 | PNTS | Ctrl + DOWN, Ctrl + S | go to current Process's Next Thread's Start |
9 | TB | LEFT, A | current Thread Backward |
10 | TF | RIGHT, D | current Thread Foreward |
11 | LU | UP, W | go Line Up of current code block in current thread |
12 | LD | DOWN, S | go Line Down of current code block in current thread |
13 | LP | Shift + UP, Shift + W | go to the occurrence of current line in Previous Loop |
14 | LD | Shift + DOWN, Shift + S | go to the occurrence of current line in Next Loop |
15 | BS | Home | go to code Block Start |
16 | BE | End | go to code Block End |
Project: | Alg-Java |
Update: | 20240824 |
Commit: | a7cd97d7 |
Source Code: | searches.BinarySearch |
BuildTool: | Java17 |
Compiler: | Java17 |
Runtime: | Openjdk17 |
System: | MySystemD |
Kernel: | Linux5.10.211 |
Cpu: | Intel:Corei7-7700K |
Machine: | AwesomeMachine |