Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * This method recursively calculates the
- * position of x in a vector.
- * DandC by subtraction with a=1,b=1,k=0 - O(n)
- * @param v Array with numbers
- * @param x Value that is being searched
- * @return The position of the value x in v
- */
- public int sequentialSearch2(int[]v, int x) {
- return searchBySubtraction(0, v, x);
- }
- private int searchBySubtraction(int i, int[]v, int x){
- if (i==v.length)
- return Integer.MIN_VALUE; //the element x does not exist
- else {
- if (v[i]==x)
- return i;
- else
- return searchBySubtraction(i+1, v, x);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement