Advertisement
Guest User

Untitled

a guest
Feb 20th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.01 KB | None | 0 0
  1. public class task22 {
  2. public static void main(String[] args) {
  3. int[] x = new int[100];
  4. for (int i = 0; i < 100; i++) {
  5. x[i] = i;
  6. }
  7. System.out.println("Index: " + new BinarySearch().preRank(x, 36));
  8. }
  9.  
  10. static class BinarySearch {
  11. int deep = 0;
  12.  
  13. int preRank(int[] x, int key) {
  14. if (null != x) {
  15. return rank(x, 0, x.length - 1, key);
  16. }
  17. return -1;
  18. }
  19.  
  20. private int rank(int[] a, int lo, int hi, int key) {
  21. if (lo <= hi) {
  22. deep++;
  23. for (int i = 0; i < deep; i++) {
  24. System.out.print("*");
  25. }
  26. System.out.println(" " + lo + " " + hi);
  27. int mid = lo + (hi - lo) / 2;
  28. if (a[mid] == key) return mid;
  29. else if (key < a[mid]) rank(a, lo, mid, key);
  30. else if (key > a[mid]) rank(a, mid, hi, key);
  31. }
  32. return -1;
  33. }
  34. }
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement