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−minmax−min×(ArraySize−1)⌋where min and 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 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(n2) 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(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). 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).
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(n2)
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 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) 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 log n). Interpolation tag sort average performance complexity is O(n+k).
Interpolation tag sort algorithm Set a tag array equal to the original array size and initialize to a false value. [Main Sort] Determines whether all buckets of the original array have been sorted. If the sorting is not completed, the [Divide function] is executed. [Divide function] Find the maximum and minimum values in the bucket. If the maximum value is equal to the minimum value, the sorting is completed and the division is stopped. Set up a two-dimensional array as all the empty buckets. Divide into the bucket according to the interpolation number. After dividing into the bucket, mark the starting position of the bucket as a true value in the tag array. And put the items back into the original array one by one from all the buckets that are not empty. Return to [Main Sort].
Practice JavaScript code:
In-place interpolation tag sort
In-place interpolation tag sort is an in-place algorithm variant of interpolation sort. It sorts non-repeating consecutive integer series. It can achieve sorting by only N times of swapping by maintaining N bit tags, saving time and memory space. However, the array to be sorted must be either a continuous integer sequence or a non-repeating arithmetical progression. The factor column data must not be repeated. For example, sorting 0~100 can be sorted in one pass. The number of exchanges is: O(n), the calculation time complexity is: O(n), and the worst space complexity is O(n) bits. This algorithm only uses one additional tag array. This tag array has the same length as the original array and is of Boolean data type. For each unswapped element, the algorithm calculates a new interpolated position p and swaps the two elements in the array. The tag array is marked true at position p, and the algorithm is incremented until it reaches the last element, at which point the array is sorted. Algorithm process:
Set an equal number of tag arrays to initialize to false values. Visit the array when tag[i] is false, calculate the position corresponding to the interpolation=p. Swap a[i] and a[p], let tag[p] = true. The tour array is completed and the sorting is completed.
Practice JavaScript code:
… excerpt ends here. Continue reading the full article.