ℹ️ Select 'Choose Exercise', or randomize 'Next Random Exercise' in selected language.

Choose Exercise:
Timer 00:00
WPM --
Score --
Acc --
Correct chars --

Implement Binary Search with Recursive Helper

Dart

Goal -- WPM

Ready
Exercise Algorithm Area
1int binarySearch(List<int> sortedList, int target) {
2if (sortedList.isEmpty) {
3return -1; // Target not found in an empty list
4}
5return _binarySearchRecursive(sortedList, target, 0, sortedList.length - 1);
6}
7
8int _binarySearchRecursive(List<int> sortedList, int target, int low, int high) {
9// Base case: If the search space is invalid, the target is not in the list.
10if (low > high) {
11return -1;
12}
13
14// Calculate the middle index to avoid potential integer overflow.
15int mid = low + (high - low) ~/ 2;
16
17// If the middle element is the target, we found it.
18if (sortedList[mid] == target) {
19return mid;
20}
21// If the target is smaller than the middle element, search the left half.
22else if (target < sortedList[mid]) {
23return _binarySearchRecursive(sortedList, target, low, mid - 1);
24}
25// If the target is larger than the middle element, search the right half.
26else {
27return _binarySearchRecursive(sortedList, target, mid + 1, high);
28}
29}
Algorithm description viewbox

Implement Binary Search with Recursive Helper

Algorithm description:

This implementation performs a binary search on a sorted list of integers to find the index of a target value. Binary search is highly efficient for searching in large, ordered datasets, making it a fundamental algorithm in computer science. It's commonly used in database indexing and searching through sorted arrays.

Algorithm explanation:

The `binarySearch` function initiates a recursive search by calling `_binarySearchRecursive` with the initial search boundaries. The recursive helper function repeatedly divides the search interval in half. If the middle element matches the target, its index is returned. If the target is smaller, the search continues in the left sub-list; otherwise, it continues in the right sub-list. The recursion terminates when the target is found or when the search interval becomes empty (`low > high`), indicating the target is not present. The time complexity is O(log n) because the search space is halved in each step. The space complexity is O(log n) due to the recursion call stack. Edge cases include an empty list and the target being the first or last element.

Pseudocode:

FUNCTION binarySearch(sortedList, target):
  IF sortedList is empty THEN
    RETURN -1
  END IF
  RETURN _binarySearchRecursive(sortedList, target, 0, length of sortedList - 1)
END FUNCTION

FUNCTION _binarySearchRecursive(sortedList, target, low, high):
  IF low > high THEN
    RETURN -1
  END IF

  mid = low + (high - low) / 2

  IF sortedList[mid] == target THEN
    RETURN mid
  ELSE IF target < sortedList[mid] THEN
    RETURN _binarySearchRecursive(sortedList, target, low, mid - 1)
  ELSE
    RETURN _binarySearchRecursive(sortedList, target, mid + 1, high)
  END IF
END FUNCTION