Interpolation Search in CIn this article, we will discuss the Interpolation search in C with its cases, examples, and outputs. The Interpolation Search is preferable to Binary Search when the values in a sorted array are evenly distributed. A discrete set of known data points is interpolated to create new data points that fall within its range. Binary search always checks the element in the middle. On the other hand, the value of the key being searched will determine where the interpolation search goes. For instance, the Interpolation search will probably begin its search towards the end side if the key value is closer to the last element. If an element is present in the array, we must write a C program that uses the Interpolation Search Algorithm to determine its position inside the array of integers. Cases of Interpolation Search:There are several cases of Interpolation Search. These cases are as follows: 1. Average Case:Interpolation searches often perform (log(log n)) comparisons, where n is the total number of elements. Example: The intended result will be Position 6 if the input array contains the following data: 1, 2, 3, 4, 5, 6, and the element to be searched is element 6. The time complexity of the average case is O(log(log n)). 2. Best Case:Interpolation search does just one comparison and returns the position if the object being sought is a middle element of an array. Example: The expected output will be Position 3 if the input array contains the data "2, 4, 6, 8, 10," and the element to be searched is "6".The time complexity in the best case is O(1). Example Code: Here is the program code for the C program that uses an array of integers to implement interpolation search. Using the GCC compiler, the program is successfully compiled and tested. Also presented below is the output of the program. Dispute Resolution
Code: Output: Test case 1: Enter total elements (num < 200) :5 Enter 5 Elements in ascending order: 1 2 3 4 5 Search For : 4 Element 4 found at position 4 Test case 2: Enter total elements (num < 200) :6 Enter 5 Elements in ascending order: 1 2 3 4 5 6 Search For: 2 Element 2 found at position 2 Program Description
Next Topicstrdup() function in C
|