Advertisement
Guest User

Untitled

a guest
Feb 13th, 2016
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.57 KB | None | 0 0
  1. /**
  2. * This method recursively calculates the
  3. * position of x in a vector.
  4. * DandC by subtraction with a=1,b=1,k=0 - O(n)
  5. * @param v Array with numbers
  6. * @param x Value that is being searched
  7. * @return The position of the value x in v
  8. */
  9. public int sequentialSearch2(int[]v, int x) {
  10. return searchBySubtraction(0, v, x);
  11. }
  12. private int searchBySubtraction(int i, int[]v, int x){
  13. if (i==v.length)
  14. return Integer.MIN_VALUE; //the element x does not exist
  15. else {
  16. if (v[i]==x)
  17. return i;
  18. else
  19. return searchBySubtraction(i+1, v, x);
  20. }
  21. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement