Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2016
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.69 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package sdp_algorithm;
  7.  
  8. import java.util.Arrays;
  9.  
  10. /**
  11. *
  12. * @author azeez taiwo
  13. * @company iQubeLabs
  14. * @date 23/07/2016
  15. *
  16. */
  17. public class SearchAlgorithms {
  18.  
  19. /**
  20. * binary search, also known as half-interval search or logarithmic search
  21. * is a search algorithm that finds the position of a target value
  22. * within a sorted array.It compares the target value to the middle element of the array;
  23. * if they are unequal, the half in which the target cannot lie is
  24. * eliminated and the search continues on the remaining half until it is successful.
  25. *
  26. * Worst time complexity of 0(logN)
  27. * Best time complexity of 0(1)
  28. *
  29. * For more information, check out https://en.wikipedia.org/wiki/Binary_search_algorithm
  30. *
  31. * @param a array of integer elements
  32. * @param x value to search for
  33. * @return the index of the element that equals x or -1 incase no match was found
  34. */
  35. static int binarySearch(int[] a, int x) {
  36. Arrays.sort(a);
  37. return binarySearchHelper(a, 0, a.length - 1, x);
  38. }
  39. public static void main(String[] args) {
  40. int[] x = {9,5,6,2,11,6,8,7,112};
  41. System.out.println(interpolationSearch(x, 6));
  42. System.out.println(binarySearch(x, 6));
  43. System.out.println(linearSearch(x, 6));
  44.  
  45. }
  46.  
  47. static int binarySearchHelper(int[] a, int l, int h, int x) {
  48. if (l > h) {
  49. return -1;
  50. } else {
  51. int m = (l + h) / 2;
  52. if (a[m] == x) {
  53. return m;
  54. } else if (a[m] < x) {
  55. return binarySearchHelper(a, m + 1, h, x);
  56. }
  57. return binarySearchHelper(a, l, m - 1, x);
  58. }
  59. }
  60.  
  61.  
  62.  
  63. /**This method sequentially checks each element of the array
  64. * until a match is found.
  65. *
  66. * Best case performace of O(1)
  67. * Worst case performance of O(N).
  68. *
  69. * For more information, check out https://en.wikipedia.org/wiki/Linear_search
  70. *
  71. * @param a array of integer elements
  72. * @param x value to search for
  73. * @return the index of the element that equals x or -1 incase no match was found
  74. */
  75. static int linearSearch(int[] a, int x) {
  76. for (int i = 0; i < a.length; ++i) {
  77. if (a[i] == x) {
  78. return i;
  79. }
  80. }
  81. return -1;
  82. }
  83.  
  84. /**This algorithm searches for a given key value in an indexed array that has
  85. * been ordered by the values of the key
  86. *
  87. * Average runtime complexity for uniformly distributed elements of O(log(logN))
  88. * Worst case complexity of 0(N)
  89. *
  90. * For more information, check out https://en.wikipedia.org/wiki/Interpolation_search
  91. *
  92. * @param a array of integer elements
  93. * @param x value to search for
  94. * @return the index of the element that equals x or -1 incase no match was found
  95. */
  96. static int interpolationSearch(int[] a, int x) {
  97. Arrays.sort(a);
  98. int h = a.length - 1;
  99. int l = 0;
  100.  
  101. while (a[h] >= x && a[l] < x) {
  102. int num = h - l;
  103. int denom = a[h] - a[l];
  104. int i = (num / denom) * x + l;
  105. if (a[i] < x) {
  106. l = i + 1;
  107. } else if (a[i] > x) {
  108. h = i - 1;
  109. } else {
  110. l = i;
  111. }
  112. }
  113. if (a[l] == x) {
  114. return l;
  115. }
  116. return -1;
  117. }
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement