Interpolation sort (or histogram sort) is a sorting algorithm that uses an interpolation formula to divide and conquer. It is a variant of bucket sort. Data is assigned to buckets using an interpolation function: Interpolation ( x ) = ⌊ x − min max − min × ( ArraySize − 1 ) ⌋ {\displaystyle {\text{Interpolation}}(x)=\lfloor {\frac {x-{\text{min}}}{{\text{max}}-{\text{min}}}}\times ({\text{ArraySize}}-1)\rfloor } where min {\displaystyle {\text{min}}} and max {\displaystyle {\text{max}}} are the minimum and maximum values within the array, and the floor function is used. This function returns an array index to where element x {\displaystyle x} can be repositioned.
Algorithm
Interpolation sort uses an array of record bucket lengths corresponding to the original number column. The array prevents the space complexity from becoming O ( n 2 ) {\displaystyle O(n^{2})} due to memory stacking. The segmentation record of the length array can using secondary function dynamically declare and delete the memory space of the array. The space complexity required to control the recursive program is O ( 3 n ) {\displaystyle O(3n)} . Contains a two-dimensional array of dynamically allocated memories and an array of record lengths. However the execution complexity can still be maintained as an efficient sorting method of O ( n + k ) {\displaystyle O(n+k)} . The array of dynamically allocated memory can be implemented using an array object (such as in JavaScript) or more basically via a linked list, stack, queue, associative array, or tree structure. The type of data structure affects the speed of data access and thus the sorting time. When the values in the ordered array are uniformly distributed in an arithmetic progression, the order of interpolation sort is linear time, O ( n ) {\displaystyle O(n)} .
Interpolation sort algorithm Set a bucket length array to record the length of the unsorted bucket. Initialize into the original array length. [Main Sort] If the bucket length array is cleared, the sort is completed. Otherwise execute Divide function. [Divide function] Pop the bucket at the end of the bucket length array. Find the maximum and minimum values in the bucket. If the maximum value is equal to the minimum value, the bucket is sorted, so stop Divide. Set up a two-dimensional array as all empty buckets. Divide into the bucket according to the interpolation number. After dividing into the buckets, push the length of the buckets into the array of bucket length. And put the items back into the original array one by one from all the buckets that are not empty. Return to [Main Sort].
Histogram sort algorithm NIST describes the histogram sort as an efficient 3-pass refinement of a bucket sort algorithm.
The first pass counts the number of items for each bucket in an auxiliary array, and then makes a running total so each auxiliary entry is the number of preceding items. The second pass puts each item in its proper bucket according to the auxiliary entry for the key of that item. The last pass sorts each bucket.
Practice
Interpolation sort implementation JavaScript code:
Interpolation sort recursive method Worst-case space complexity: O ( n 2 ) {\displaystyle O(n^{2})}
Histogram sort implementation
Variants
Interpolation tag sort
Interpolation tag sort is a recursive variant of interpolation sort. After the array data is distributed into buckets via an interpolation function, each bucket recursively runs the original algorithm until the sorting is completed. To avoid stack overflow caused by recursion, use a Boolean data type tag array to operate the recursive function to release the memory. The extra memory space required is close to 2 n + ( n ) {\displaystyle 2n+(n)} bits. Contains a two-dimensional array of dynamically allocated memory and a Boolean data type tag array. Buckets can be implemented using a stack, queue, associative array, or tree structure. Like interpolation sort, interpolation tag sort runs in linear time O ( n ) {\displaystyle O(n)} when the values in the array to be sorted are evenly distributed. The bucket sort algorithm does not limit the sorting to the lower limit of O ( n l o g n ) {\displaystyle O(n\ log\ n)} . Interpolation tag sort average performance complexity is O ( n + k ) {\displaystyle O(n+k)} .
… excerpt ends here. Continue reading the full article.
