Insert sort is a simple sorting algorithm that builds the final sorted array (or list) one item at a time. It is much less efficient on large lists than more advanced algorithms such as quicksort, heapsort, or merge sort. However, insert sort provides several advantages:
- Simple implementation
- Efficient for (quite) small data sets, much like other quadratic sorting algorithms.
- The time complexity is O(nk) when each element in the input is no more than k places away from its sorted position.
- Only requires a constant amount O(1) of additional memory space.
- Sort a list as it receives it.
For its FPGA implementation, a dedicated structure is designed as follows:
The Insert Sort primitive has an internal shift register array to sort the input stream. It takes five steps to finish the sort processing of a stream with a limited max sort number.
- Build a group of shift registers, and the number of shift register is the maximum sort number.
- Broadcasting the input value to every shift registers, then comparing size between the internal value of each shift register and the input value. For descending sort, run step 3. Otherwise, run step 4.
- For descending sort, you should build a internal ascending array. If the input value is larger than array[i], then right shift array[i]. If the input value is less than array[i] and larger than array[i+1], insert the input value to array[i+1];
- For ascending sort, you should build an internal descending array. If the input value is less than array[i], then right shift array[i]. If the input value is larger than array[i] and less than array[i+1], insert the input value to array[i+1];
- If the input stream is not empty, output the last value of the array, and then return to step 2. Otherwise, right shift the whole register array and output the last array value until the array is empty.