Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class task22 {
- public static void main(String[] args) {
- int[] x = new int[100];
- for (int i = 0; i < 100; i++) {
- x[i] = i;
- }
- System.out.println("Index: " + new BinarySearch().preRank(x, 36));
- }
- static class BinarySearch {
- int deep = 0;
- int preRank(int[] x, int key) {
- if (null != x) {
- return rank(x, 0, x.length - 1, key);
- }
- return -1;
- }
- private int rank(int[] a, int lo, int hi, int key) {
- if (lo <= hi) {
- deep++;
- for (int i = 0; i < deep; i++) {
- System.out.print("*");
- }
- System.out.println(" " + lo + " " + hi);
- int mid = lo + (hi - lo) / 2;
- if (a[mid] == key) return mid;
- else if (key < a[mid]) rank(a, lo, mid, key);
- else if (key > a[mid]) rank(a, mid, hi, key);
- }
- return -1;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement