1

I am trying to implement Linear Search Recursively using Javascript.

Given Array A = [1,2,3,4,5,6]

Function signature - something like this :

LinearSearchRecursively(ArrayGiven, x, startingValue) 

If value is found then return the index else return -1, but achieve it recursively.

Would appreciate if you can attach a running jsbin or jsfiddle.

4 Answers 4

2

You can do it using array destructuring to get the head and tail of your array.

You then compare the head, if it is equal to your value, you return the index so far, otherwise, you call the recursive function with the tail and the index incremented.

Your stop condition is when the array is empty, in which case you return -1.

Here I wrap the recursive function and its call with an outer function which has a nicer API, without the index.

function linearSearch(arr, value) {
  function linearSearchRec(list, idx) {
    if (!list.length) return -1;
    const [head, ...tail] = list;
    if (value === head) return idx;
    else return linearSearchRec(tail, idx + 1);
  }
  return linearSearchRec(arr, 0);
}

console.log(linearSearch([1,2,3,4,5,6], 1));
console.log(linearSearch([1,2,3,4,5,6], 4));
console.log(linearSearch([1,2,3,4,5,6], 10));

Sign up to request clarification or add additional context in comments.

Comments

1

You could change the signature of the function, because the call of the search function should be possible without taking an initial index for searching.

function linearSearchRecursively(a, x, i = 0) {
    if (i >= a.length) return -1;
    if (a[i] === x) return i;
    return linearSearchRecursively(a, x, i + 1);
}

console.log(linearSearchRecursively([1, 2, 3, 4, 5, 6, 7], 7));
console.log(linearSearchRecursively([1, 2, 3, 4, 5, 6, 7], 9));
console.log(linearSearchRecursively([], 7));

Another solution could be to use a destructuring for the array and check against the first element.

function linearSearchRecursively([a, ...rest], x, i = 0) {
    if (a === x) return i;
    if (!rest.length) return -1;
    return linearSearchRecursively(rest, x, i + 1);
}

console.log(linearSearchRecursively([1, 2, 3, 4, 5, 6, 7], 7));
console.log(linearSearchRecursively([1, 2, 3, 4, 5, 6, 7], 9));
console.log(linearSearchRecursively([], 7));

Comments

0

I would do something like this : here is jsbin link :

https://jsbin.com/lijitet/8/edit?js,console

/**
 * Linear Search : Recursion
 * Returns index if found -1 otherwise
 * Procedure LinearSearch(Array A, int x, int i)
 * n = A.length and i is starting position of array
 * if (A[i] === x) return i
 * if (i > n) LinearSearch(A, x, i++)
  * 
  */
 function LinearSearchRecursively(ArrayGiven, x, i) {
     const arrayLength = ArrayGiven.length;

     if (i > (arrayLength - 1)) {
       return -1;
     }
     if(ArrayGiven[i] === x) {
       return i;
     }
     return LinearSearchRecursively(ArrayGiven, x, i+1); 
 }

 // write your test cases here :
 const testArray = [ 1, 2, 3, 4, 5, 6, 7];

 console.log(`Missing Element : ${LinearSearchRecursively(testArray, 7, 0)}`);

Please feel free to add. thanks.

Comments

0
let i = 0 
let recursiveSearch = (array, element) => {     
    if(i >= array.length) return 'Not found'    
    if(array[i] == element) return i 
    else {    
        i = i+1
        return recursiveSearch(array, element)
    }
}

console.log(recursiveSearch([1,5,3,4],4)) // index 3
console.log(recursiveSearch([1,5,3,4,10,20],5)) //index 2
console.log(recursiveSearch([1,5,3,4,10,20],50)) // Not found

1 Comment

While this code may solve the question, including an explanation of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please edit your answer to add explanations and give an indication of what limitations and assumptions apply.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.