Content deleted Content added
→top: plainer words |
m →Pseudocode: : remove trailing ")" |
||
(5 intermediate revisions by 4 users not shown) | |||
Line 1:
{{Short description|Sorting algorithm}}
{{refimprove|date=October 2017}}
{{Infobox Algorithm
Line 10 ⟶ 11:
|optimal=?
}}
'''Library sort'''
<blockquote>Suppose a librarian were to store their 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 they find the correct space in the B section, they 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 they were to leave a space after every letter, as long as there was still space after B, they would only have to move a few books to make room for the new one. This is the basic principle of the Library Sort.</blockquote>
Line 17 ⟶ 18:
Like the insertion sort it is based on, library sort is a [[comparison sort]]; 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(n<sup>2</sup>). There is no full implementation given in the paper, nor the exact algorithms of important parts, such as insertion and rebalancing. Further information would be needed to discuss how the efficiency of library sort compares to that of other sorting methods in reality.
Compared to basic insertion sort, the drawback of library sort is that it requires extra space for the gaps. The amount and distribution of that space would depend on implementation. In the paper the size of the needed array is ''(1 + ε)n'',<ref name="definition" /> but with no further recommendations on how to choose ε. Moreover, it is neither adaptive nor stable. In order to warrant the
Another drawback is that it cannot be run as an [[online algorithm]], because it is not possible to randomly shuffle the input. If used without this shuffling, it could easily degenerate into quadratic behaviour.
One weakness of [[insertion sort]] is that it may require a high number of swap operations and be costly if memory write is expensive. Library sort may improve that somewhat in the insertion step, as fewer elements need to move to make room, but
==Implementation==
Line 38 ⟶ 39:
'''procedure''' rebalance(A, begin, end) '''is'''
r ← end
w ← end
'''while''' r ≥ begin '''do'''
A[w+1] ← gap▼
A[w] ← A[r]
r ← r − 1
w ← w − 2
Line 50 ⟶ 51:
S ← new array of n gaps
'''for''' i ← 1 to floor(log2(n
'''for'''
ins ← binarysearch(A[j], S, 2^i)
insert A[j] at S[ins]
Line 65 ⟶ 67:
{{sorting}}
[[Category:Comparison sorts]]
[[Category:Stable sorts]]
|