Computer Science Engineering (CSE) Exam  >  Computer Science Engineering (CSE) Questions  >  What is the space complexity of the following... Start Learning for Free
What is the space complexity of the following binary search algorithm?
int BinSearch(int a[], int n, int data)
{
int low = 0;
int high = n-1;
while (low <= high)
{
mid = low + (high-low)/2;
if(a[mid] == data)
return mid;
else if(a[mid] < data)
low = mid + 1;
else
high = mid - 1;
}
return -1;
}
  • a)
    O(n)
  • b)
    O(n2)
  • c)
    O(log n)
  • d)
    O(1)
Correct answer is option 'D'. Can you explain this answer?
Most Upvoted Answer
What is the space complexity of the following binary search algorithm?...
Binary search is also called a half-interval search. It compares the required value with a middle element.
Generally, space complexity is the extra space that we are needed in the algorithm.
In a binary search, we have just a comparison between the array element and data element to be searched.
Since no extra space is needed: Space complexity: O(1)
Important Points:
In space complexity, an array that contains the original data is not included because it is already given and we have not added any extra space apart from that.
The time complexity of binary search: O(log n) 
Free Test
Community Answer
What is the space complexity of the following binary search algorithm?...


Space Complexity of Binary Search Algorithm

Explanation:

- The space complexity of an algorithm is the amount of memory space required by the algorithm to execute and store the data values.

Binary Search Algorithm:

- In the given binary search algorithm, the only extra space used is for the variables `low`, `high`, `mid`, and `data`.

Variables:
- low, high, mid: These variables are used to keep track of the indices in the array during the search process. They each take up constant space.
- data: This variable stores the value that is being searched for in the array. It also takes up constant space.

Space Complexity:
- Since the space used by the algorithm is constant regardless of the size of the input array, the space complexity of the binary search algorithm is O(1) (constant space complexity).
Explore Courses for Computer Science Engineering (CSE) exam

Similar Computer Science Engineering (CSE) Doubts

Question Description
What is the space complexity of the following binary search algorithm?int BinSearch(int a[], int n, int data){int low = 0;int high = n-1;while (low <= high){mid = low + (high-low)/2;if(a[mid] == data)return mid;else if(a[mid] < data)low = mid + 1;elsehigh = mid - 1;}return -1;}a)O(n)b)O(n2)c)O(log n)d)O(1)Correct answer is option 'D'. Can you explain this answer? for Computer Science Engineering (CSE) 2025 is part of Computer Science Engineering (CSE) preparation. The Question and answers have been prepared according to the Computer Science Engineering (CSE) exam syllabus. Information about What is the space complexity of the following binary search algorithm?int BinSearch(int a[], int n, int data){int low = 0;int high = n-1;while (low <= high){mid = low + (high-low)/2;if(a[mid] == data)return mid;else if(a[mid] < data)low = mid + 1;elsehigh = mid - 1;}return -1;}a)O(n)b)O(n2)c)O(log n)d)O(1)Correct answer is option 'D'. Can you explain this answer? covers all topics & solutions for Computer Science Engineering (CSE) 2025 Exam. Find important definitions, questions, meanings, examples, exercises and tests below for What is the space complexity of the following binary search algorithm?int BinSearch(int a[], int n, int data){int low = 0;int high = n-1;while (low <= high){mid = low + (high-low)/2;if(a[mid] == data)return mid;else if(a[mid] < data)low = mid + 1;elsehigh = mid - 1;}return -1;}a)O(n)b)O(n2)c)O(log n)d)O(1)Correct answer is option 'D'. Can you explain this answer?.
Solutions for What is the space complexity of the following binary search algorithm?int BinSearch(int a[], int n, int data){int low = 0;int high = n-1;while (low <= high){mid = low + (high-low)/2;if(a[mid] == data)return mid;else if(a[mid] < data)low = mid + 1;elsehigh = mid - 1;}return -1;}a)O(n)b)O(n2)c)O(log n)d)O(1)Correct answer is option 'D'. Can you explain this answer? in English & in Hindi are available as part of our courses for Computer Science Engineering (CSE). Download more important topics, notes, lectures and mock test series for Computer Science Engineering (CSE) Exam by signing up for free.
Here you can find the meaning of What is the space complexity of the following binary search algorithm?int BinSearch(int a[], int n, int data){int low = 0;int high = n-1;while (low <= high){mid = low + (high-low)/2;if(a[mid] == data)return mid;else if(a[mid] < data)low = mid + 1;elsehigh = mid - 1;}return -1;}a)O(n)b)O(n2)c)O(log n)d)O(1)Correct answer is option 'D'. Can you explain this answer? defined & explained in the simplest way possible. Besides giving the explanation of What is the space complexity of the following binary search algorithm?int BinSearch(int a[], int n, int data){int low = 0;int high = n-1;while (low <= high){mid = low + (high-low)/2;if(a[mid] == data)return mid;else if(a[mid] < data)low = mid + 1;elsehigh = mid - 1;}return -1;}a)O(n)b)O(n2)c)O(log n)d)O(1)Correct answer is option 'D'. Can you explain this answer?, a detailed solution for What is the space complexity of the following binary search algorithm?int BinSearch(int a[], int n, int data){int low = 0;int high = n-1;while (low <= high){mid = low + (high-low)/2;if(a[mid] == data)return mid;else if(a[mid] < data)low = mid + 1;elsehigh = mid - 1;}return -1;}a)O(n)b)O(n2)c)O(log n)d)O(1)Correct answer is option 'D'. Can you explain this answer? has been provided alongside types of What is the space complexity of the following binary search algorithm?int BinSearch(int a[], int n, int data){int low = 0;int high = n-1;while (low <= high){mid = low + (high-low)/2;if(a[mid] == data)return mid;else if(a[mid] < data)low = mid + 1;elsehigh = mid - 1;}return -1;}a)O(n)b)O(n2)c)O(log n)d)O(1)Correct answer is option 'D'. Can you explain this answer? theory, EduRev gives you an ample number of questions to practice What is the space complexity of the following binary search algorithm?int BinSearch(int a[], int n, int data){int low = 0;int high = n-1;while (low <= high){mid = low + (high-low)/2;if(a[mid] == data)return mid;else if(a[mid] < data)low = mid + 1;elsehigh = mid - 1;}return -1;}a)O(n)b)O(n2)c)O(log n)d)O(1)Correct answer is option 'D'. Can you explain this answer? tests, examples and also practice Computer Science Engineering (CSE) tests.
Explore Courses for Computer Science Engineering (CSE) exam
Signup to solve all Doubts
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev