Dart Program to Implement Interpolation Search

Dart Program to Implement Interpolation Search

When you need to search for elements in a sorted dataset, interpolation search is an efficient and intelligent alternative to linear or binary search. Unlike binary search, which splits the search range in half, interpolation search estimates the probable position of the target based on its value relative to the first and last elements. This approach can significantly reduce the number of comparisons when the data is uniformly distributed. Learning interpolation search in Dart not only helps beginners grasp efficient search techniques but also strengthens algorithmic thinking.

Pluralsight Logo
Accelerate your tech career
with hands-on learning.
Whether you're a tech newbie or a total pro,
get the skills and confidence to land your next move.
Start 10-Day Free Trial

Interpolation search is often used in applications like finding a record in a database, looking up phone numbers, or searching in ordered datasets. Understanding this algorithm helps programmers create faster, more optimized applications, especially when dealing with large, sorted arrays. In this article, we will explore multiple ways to implement interpolation search in Dart, including iterative, recursive, and function-based methods, giving beginners practical examples to follow.

Program 1: Iterative Interpolation Search

This program demonstrates the basic iterative approach to interpolation search. It repeatedly estimates the position of the target based on the values at the boundaries and checks for the target element.

void main() {

  List<int> numbers = [10, 20, 30, 40, 50, 60, 70];
  int target = 40;
  int low = 0;
  int high = numbers.length - 1;
  bool found = false;

  while (low <= high && target >= numbers[low] && target <= numbers[high]) {

    int pos = low + (((high - low) ~/ (numbers[high] - numbers[low])) * (target - numbers[low]));

    if (numbers[pos] == target) {

      print("Element $target found at index $pos");
      found = true;
      break;

    }

    if (numbers[pos] < target) {
      low = pos + 1;
    } else {
      high = pos - 1;
    }

  }

  if (!found) {
    print("Element $target not found in the list");
  }

}

In this program, the search position is calculated using the interpolation formula. The estimated position often brings the search closer to the target compared to binary search. Beginners can see how this method adapts to the value of the target, making it efficient for uniformly distributed datasets.

Program 2: Recursive Interpolation Search

Recursion can also be applied to interpolation search, where the function calls itself with a smaller search range until it finds the target or determines it is not present.

int interpolationSearchRecursive(List<int> numbers, int target, int low, int high) {

  if (low > high || target < numbers[low] || target > numbers[high]) return -1;

  int pos = low + (((high - low) ~/ (numbers[high] - numbers[low])) * (target - numbers[low]));

  if (numbers[pos] == target) return pos;
  if (numbers[pos] < target) return interpolationSearchRecursive(numbers, target, pos + 1, high);

  return interpolationSearchRecursive(numbers, target, low, pos - 1);

}

void main() {

  List<int> numbers = [5, 15, 25, 35, 45, 55];
  int target = 35;
  int result = interpolationSearchRecursive(numbers, target, 0, numbers.length - 1);

  if (result != -1) {
    print("Element $target found at index $result");
  } else {
    print("Element $target not found in the list");
  }

}

The recursive approach breaks down the problem into smaller subproblems. Each call estimates a new position based on the current low and high boundaries, demonstrating how recursion can replace iteration while keeping the logic clear. Beginners can practice recursion while understanding interpolation search more deeply.

Program 3: Interpolation Search Using a Function (Iterative)

Wrapping interpolation search logic in a reusable function makes the code more organized and easy to apply in different scenarios.

int interpolationSearch(List<int> numbers, int target) {

  int low = 0;
  int high = numbers.length - 1;

  while (low <= high && target >= numbers[low] && target <= numbers[high]) {

    int pos = low + (((high - low) ~/ (numbers[high] - numbers[low])) * (target - numbers[low]));

    if (numbers[pos] == target) return pos;
    if (numbers[pos] < target) low = pos + 1;
    else high = pos - 1;

  }

  return -1;

}

void main() {

  List<int> numbers = [2, 4, 6, 8, 10, 12];
  int target = 8;
  int result = interpolationSearch(numbers, target);

  if (result != -1) {
    print("Element $target found at index $result");
  } else {
    print("Element $target not found in the list");
  }

}

Using a function makes the code reusable and easier to maintain. Beginners can call the function with different datasets and targets without rewriting the logic, which is a good programming practice.

Program 4: Interpolation Search on a Large Dataset

This program demonstrates the efficiency of interpolation search on a larger, uniformly distributed dataset, showing how quickly it can find the target element.

void main() {

  List<int> numbers = List.generate(100, (i) => i * 5); // 0, 5, 10, ..., 495
  int target = 245;
  int low = 0;
  int high = numbers.length - 1;
  bool found = false;

  while (low <= high && target >= numbers[low] && target <= numbers[high]) {

    int pos = low + (((high - low) ~/ (numbers[high] - numbers[low])) * (target - numbers[low]));

    if (numbers[pos] == target) {

      print("Element $target found at index $pos");
      found = true;
      break;

    }

    if (numbers[pos] < target) {
      low = pos + 1;
    } else {
      high = pos - 1;
    }

  }

  if (!found) {
    print("Element $target not found in the list");
  }

}

Even with 100 elements, interpolation search often finds the target faster than binary search when the dataset is uniformly distributed. This example helps beginners understand why interpolation search is particularly efficient in certain cases.

Program 5: Interpolation Search Using a Custom Dart Function with Error Handling

This program shows a complete function with error handling for empty lists or non-uniform data, making it safer for real-world applications.

int safeInterpolationSearch(List<int> numbers, int target) {

  if (numbers.isEmpty) return -1;

  int low = 0;
  int high = numbers.length - 1;

  while (low <= high && target >= numbers[low] && target <= numbers[high]) {

    if (numbers[high] == numbers[low]) break; // avoid division by zero

    int pos = low + (((high - low) ~/ (numbers[high] - numbers[low])) * (target - numbers[low]));

    if (numbers[pos] == target) return pos;
    if (numbers[pos] < target) low = pos + 1;
    else high = pos - 1;

  }

  return -1;

}

void main() {

  List<int> numbers = [10, 20, 30, 40, 50];
  int target = 30;
  int result = safeInterpolationSearch(numbers, target);

  if (result != -1) {
    print("Element $target found at index $result");
  } else {
    print("Element $target not found in the list");
  }

}

Adding error handling prevents issues like division by zero or searching in empty lists. Beginners can see how practical considerations are important when implementing algorithms in real-world applications.

Frequently Asked Questions (FAQ)

Interpolation search is powerful but can raise questions for beginners. Here are some common ones with clear answers.

Q1: What is interpolation search used for?
Interpolation search is used to find elements in a sorted, uniformly distributed dataset efficiently, often faster than binary search.

Q2: Can interpolation search work on unsorted data?
No, it requires a sorted dataset to calculate the estimated position correctly.

Q3: Is interpolation search always faster than binary search?
It is faster when data is uniformly distributed, but for skewed or random distributions, binary search may perform better.

Q4: Can recursion be used for interpolation search?
Yes, recursion can replace iteration by calling the function with a smaller range until the target is found.

Q5: Why is interpolation search important for beginners?
It teaches estimation-based searching, optimization, and how algorithms can adapt to data patterns, building foundational problem-solving skills.

Conclusion

Interpolation search is an advanced and efficient searching algorithm for sorted and uniformly distributed datasets. By implementing it using iterative, recursive, and function-based approaches in Dart, beginners can learn how to estimate positions intelligently and reduce search time.

Practicing these programs helps you understand arrays, loops, recursion, and real-world error handling. Start with simple datasets, experiment with recursion, and then try safe implementations to handle edge cases. The more you practice, the better your understanding of efficient searching techniques and algorithmic thinking will become, preparing you for larger, more complex applications.

Scroll to Top