Library sort
Encyclopedia
Library sort, or gapped insertion sort is a sorting algorithm
that uses an insertion sort
, but with gaps in the array to accelerate subsequent insertions. The name comes from an analogy:
The algorithm was proposed by Michael A. Bender, Martín Farach-Colton, and Miguel Mosteiro in 2006.
Like the insertion sort it is based on, library sort is a stable comparison sort
and can be run as an online algorithm
; however, it was shown to have a high probability of running in O(n log n) time (comparable to quicksort), rather than an insertion sort's O(n2). Its implementation is very similar to a skip list
. The drawback to using the library sort is that it requires extra space for the gaps.
Sorting algorithm
In computer science, a sorting algorithm is an algorithm that puts elements of a list in a certain order. The most-used orders are numerical order and lexicographical order...
that uses an insertion sort
Insertion sort
Insertion sort is a simple sorting algorithm: a comparison sort in which the sorted array is built one entry at a time. It is much less efficient on large lists than more advanced algorithms such as quicksort, heapsort, or merge sort...
, but with gaps in the array to accelerate subsequent insertions. The name comes from an analogy:
Suppose a librarian were to store his books alphabetically on a long shelf, starting with the As at the left end, and continuing to the right along the shelf with no spaces between the books until the end of the Zs. If the librarian acquired a new book that belongs to the B section, once he finds the correct space in the B section, he will have to move every book over, from the middle of the Bs all the way down to the Zs in order to make room for the new book. This is an insertion sort. However, if he were to leave a space after every letter, as long as there was still space after B, he would only have to move a few books to make room for the new one. This is the basic principle of the Library Sort.
The algorithm was proposed by Michael A. Bender, Martín Farach-Colton, and Miguel Mosteiro in 2006.
Like the insertion sort it is based on, library sort is a stable comparison sort
Comparison sort
A comparison sort is a type of sorting algorithm that only reads the list elements through a single abstract comparison operation that determines which of two elements should occur first in the final sorted list...
and can be run as an online algorithm
Online algorithm
In computer science, an online algorithm is one that can process its input piece-by-piece in a serial fashion, i.e., in the order that the input is fed to the algorithm, without having the entire input available from the start. In contrast, an offline algorithm is given the whole problem data from...
; however, it was shown to have a high probability of running in O(n log n) time (comparable to quicksort), rather than an insertion sort's O(n2). Its implementation is very similar to a skip list
Skip list
A skip list is a data structure for storing a sorted list of items using a hierarchy of linked lists that connect increasingly sparse subsequences of the items...
. The drawback to using the library sort is that it requires extra space for the gaps.