0

1.recursive binary search (with target position returned)

function recursiveBinarySearch(list, target) {
  const binarySearchHelper = (list, target, left, right) => {
    let mid = Math.floor((left + right) / 2);

    //not found
    if (left > right) {
      return -1;
    }

    if (list[mid] === target) {
      return mid;
    } else if (list[mid] > target) {
      return binarySearchHelper(list, target, left, mid - 1);
    } else if (list[mid] < target) {
      return binarySearchHelper(list, target, mid + 1, right);
    }
  };

  return binarySearchHelper(list, target, 0, list.length - 1);
}

2.recursive binary search (no target position returned, only boolean)

function recursiveBinarySearch(list, target) {
  const binarySearchHelper = (list, target) => {
    let mid = Math.floor((list.length - 1) / 2);

    //not found
    if (list.length <= 0) {
      return false;
    }

    //found or recursive
    if (list[mid] === target) {
      return true;
    } else if (list[mid] < target) {
      return binarySearchHelper(list.slice(mid + 1), target);
    } else if (list[mid] > target) {
      return binarySearchHelper(list.slice(0, mid), target);
    }
  };

  return binarySearchHelper(list, target);
}

I have a hard time understanding space complexity.

What are the space complexity of these two algorithms?

I think 2 has space complexity of O(log n) because on each function call of recursive binary search it to create a new list of size n/2, n/4... and so on (correct me if I am wrong). What about 1.recursive binary search (with target position returned)?

jethro-dev
  • 249
  • 1
  • 5
  • 12
  • No, they are not the same. `.slice()` incurs a cost. – Pointy Feb 17 '22 at 01:36
  • n/2 is already O(n), not O(log n). n/2 + n/4 + n/8 + ... is still O(n) – jabaa Feb 17 '22 at 01:45
  • But I saw from many resources, they say the space complexity of 2 is O(log n)? As it keeps creating new lists of half of the size of the last list created. If I am wrong, can you explain further, thanks. – jethro-dev Feb 17 '22 at 01:48
  • Creating the first copy with size n/2 has already space complexity O(n). It's a linear relation. You need n/2 memory. f(x) = x/2 is a linear function and O(n) is a linear complexity. Your resources are wrong. – jabaa Feb 17 '22 at 01:49
  • The first algorithm requires O(log n) additional memory. You have log n steps and in each step you need a constant number of variables. I'm using the term additional memory, because obviously the initial array already requires O(n) memory. – jabaa Feb 17 '22 at 01:54

1 Answers1

0

No, the space complexity is not same (ignoring O(n) memory for the initial array)

The first algorithm has a space complexity of O(log n). You have log n steps and in each step you need a constant number of variables

The second algorithm has a space complexity of O(n). Creating the first copy with size n/2 has already space complexity O(n). You have log n steps. Step 1 requires n/2 memory, step 2 requires n/4, step 3 requires n/8, ... n/2 + n/4 + n/8 + ... <= n is still O(n).

jabaa
  • 5,844
  • 3
  • 9
  • 30
  • Thank you for the explaination. The resources I watching is https://www.youtube.com/watch?v=8hly31xKli0&t=1338s. In 1:49:42, it said the space complexity is O(log n), and he is using ***2. recursive binary search (no target position returned, only boolean)***, is he wrong? Or am I missing something? – jethro-dev Feb 17 '22 at 02:06
  • @obiito01 That statement is wrong. Even with tail call optimization the necessary required additional space for the first step is n/2 and that's O(n). I watched the video for about 10 minutes and I wouldn't recommend it. What are the other _"many resources"_? This video is a good example why you should learn from books and not from YouTube videos. – jabaa Feb 17 '22 at 02:20