Clear, practical technology insights BSOD Code Lookup · Windows Error Code Lookup · Wi-Fi Troubleshooting · PC Troubleshooting Checklist

Interpolation Search Algorithm (Interpolation Search)

Understand Interpolation Search Algorithm (Interpolation Search) with clear explanations, practical examples, and useful tips. This updated guide covers the...

Table of Contents

Interpolation Search Algorithm (Interpolation Search) is easier to understand when the core ideas are paired with practical examples. The sections below explain the topic clearly, highlight useful steps, and point out details that can prevent common errors.

Interpolation Search (Interpolation Search) is an improved variant of Binary Search (Binary Search). In order for this search algorithm to work correctly, the data set must be sorted.

Binary Search has a great advantage of time complexity when compared to Linear Search. Linear Search has the worst case complexity of ? (n) while Binary Search is ? (log n).

There are a number of situations where the location of the data you want to find may already be known. For instance,, in the case of a phone book, if we want to find Huong's phone number, For instance,. In this case, Linear Search and Binary Search may be slow when performing a search, when we can directly jump to the memory space whose name begins with H stored.

In Binary Search, if the data to be searched is not found, the rest of the list is divided into two parts: the left part (containing the smaller value) and the right part (containing the larger value). The search process is then performed on one of these two sections.

Interpolation Search Algorithm (Interpolation Search) example image 1

Search interpolation searching for a specific element by calculating the probe position (Probe Position). Initially, the detector position is the position of the element in the middle of the data set.

Interpolation Search Algorithm (Interpolation Search) example image 2

If a connection is found, the index of the element is returned. To split the list into two parts, we use the following method:

 mid = Lo + ((Hi - Lo) / (A[Hi] - A[Lo])) * (X - A[Lo]) Trong ?ó: A = danh sách Lo = ch? m?c th?p nh?t c?a danh sách Hi = ch? m?c cao nh?t c?a danh sách A[n] = giá tr? ???c l?u gi? t?i ch? m?c n trong danh sách

If the element to be found has a value greater than the middle element, the element to be found will be in the sub-array to the right of the middle element and we will continue to calculate the detector position; otherwise, the element to be found will be in the sub-array to the left of the middle element. This process proceeds on sub-arrays until the size of the sub-array decreases to 0.

The Interpolation Search runtime complexity is ? (log (log n)) , while Binary Search is ? (log n) .

Interpolation Search algorithm

Because this is an improvement of the Binary Search algorithm, we will only mention the steps to find the index of the value to be searched using the detector position.

 B??c 1  : B?t ??u tìm ki?m d? li?u t? ph?n gi?a c?a danh sách B??c 2  : N?u ?ây là m?t so kh?p (m?t k?t n?i), thì tr? v? ch? m?c c?a ph?n t?, và thoát. B??c 3  : N?u không ph?i là m?t so kh?p, thì là v? trí dò. B??c 4  : Chia danh sách b?i s? d?ng phép tính tìm v? trí dò và tìm v? trí gi?a m?i. B??c 5  : N?u d? li?u c?n tìm l?n h?n giá tr? t?i v? trí gi?a, thì tìm ki?m trong m?ng con bên ph?i. B??c 6  : N?u d? li?u c?n tìm nh? h?n giá tr? t?i v? trí gi?a, thì tìm ki?m trong m?ng con bên trái B??c 7  : L?p l?i cho t?i khi tìm th?y so kh?p

Sample code for Interpolation Search algorithm

 A ? M ? ng N ? K í ch c ? c ? a A X ? Gi á tr ? c ? n t ì m h à m t ì m ki ? m n ? i suy Interpolation_Search () G á n Lo ? 0 G á n Mid ? - 1 G á n Hi ? N - 1 While X kh ô ng so kh ? p if Lo b ? ng Hi OR A [ Lo ] b ? ng A [ Hi ] EXIT : Th ? t b ? i , kh ô ng t ì m th ? y X k ? t th ú c if G á n Mid = Lo + (( Hi - Lo ) / ( A [ Hi ] - A [ Lo ])) * ( X - A [ Lo ]) if A [ Mid ] = X EXIT : Th à nh c ô ng , t ì m th ? y t ? i Mid else if A [ Mid ] < X Thi ? t l ? p Lo th à nh Mid + 1 else if A [ Mid ] > X Thi ? t l ? p Hi th à nh Mid - 1 k ? t th ú c if k ? t th ú c if K ? t th ú c While K ? t th ú c h à m 

Next lesson: Hash Table data structure

FAQ

Interpolation Search (Interpolation Search) is an improved variant of Binary Search (Binary Search). In order for this search algorithm to work correctly, the data set must be sorted.

In Binary Search, if the data to be searched is not found, the rest of the list is divided into two parts: the left part (containing the smaller value) and the right part (containing the larger value). The search process is then performed on one of these two.

Search interpolation searching for a specific element by calculating the probe position (Probe Position). Initially, the detector position is the position of the element in the middle of the data set.

Discussion

Reader Comments 0

Sign in with email or Google to join the discussion.