Advertisement
Guest User

Untitled

a guest
Mar 20th, 2019
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.74 KB | None | 0 0
  1. public static int binarySearch(List <String> sortedAL, String key, int lowIndex, int highIndex)
  2. {
  3. int middle = (lowIndex + highIndex) / 2;
  4.  
  5. if(highIndex < lowIndex)
  6. {
  7. //System.out.println(-1);
  8. return -1;
  9. }
  10.  
  11. if(key.equalsIgnoreCase(sortedAL.get(middle)))
  12. {
  13. return middle;
  14. }
  15.  
  16. /*
  17. str1.compareTo(str2)
  18.  
  19. * if s1 > s2, it returns positive number
  20. * if s1 < s2, it returns negative number
  21. * if s1 == s2, it returns 0
  22. */
  23.  
  24. else if(key.compareToIgnoreCase(sortedAL.get(middle)) < 0)
  25. {
  26. return binarySearch(sortedAL, key, lowIndex, middle - 1);
  27. }
  28.  
  29. else
  30. {
  31. return binarySearch(sortedAL, key, middle + 1, highIndex);
  32. }
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement