Content deleted Content added
→Algorithm: clean up, typos fixed: a empty → an empty using AWB |
m →Pseudocode: : remove trailing ")" |
||
(42 intermediate revisions by 24 users not shown) | |||
Line 1:
{{Short description|Sorting algorithm}}
{{refimprove|date=October 2017}}
{{Infobox Algorithm
|class=[[Sorting algorithm]]
Line 4 ⟶ 6:
|data=[[Array data structure|Array]]
|time=<math>O(n^2)</math>
|best-time=<math>O(n\log n)</math>
|average-time=<math>O(n\log n)</math>
|space=<math>O(n)</math>
|optimal=?
}}
'''Library sort'''
<blockquote>Suppose a librarian were to store
The algorithm was proposed by [[Michael A. Bender]], [[Martín Farach-Colton]], and [[Miguel Mosteiro]] in 2004<ref>{{cite arXiv |eprint=cs/0407003 |title=Insertion Sort is O(n log n) |date=1 July 2004 |last1=Bender |first1=Michael A. |last2=Farach-Colton |first2=Martín |authorlink2=Martin Farach-Colton |last3=Mosteiro |first3=Miguel A.}}</ref> and was published in 2006.<ref name="definition">{{cite journal | journal=Theory of Computing Systems | volume=39 | issue=3 | pages=391–397 | date=June 2006 | last1=Bender | first1=Michael A. | last2=Farach-Colton | first2=Martín | authorlink2=Martin Farach-Colton | last3=Mosteiro | first3=Miguel A. | title=Insertion Sort is O(n log n) | doi=10.1007/s00224-005-1237-z | url=http://csis.pace.edu/~mmosteiro/pub/paperToCS06.pdf | arxiv=cs/0407003 | s2cid=14701669 | access-date=2017-09-07 | archive-url=https://web.archive.org/web/20170908070035/http://csis.pace.edu/~mmosteiro/pub/paperToCS06.pdf | archive-date=2017-09-08 | url-status=dead }}</ref>
Like the insertion sort it is based on, library sort is a
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
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 also adds an extra cost in the rebalancing step. In addition, locality of reference will be poor compared to [[mergesort]], as each insertion from a random data set may access memory that is no longer in cache, especially with large data sets.
==Implementation==
===Algorithm ===
Let us say we have an array of n elements. We choose the gap we intend to give. Then we would have a final array of size (1 + ε)n. The algorithm works in log n rounds. In each round we insert as many elements as there are in the final array already, before re-balancing the array. For finding the position of inserting, we apply Binary Search in the final array and then swap the following elements till we hit an empty space. Once the round is over, we re-balance the final array by inserting spaces between each element.
Following are three important steps of the algorithm:
# '''Binary Search''': Finding the position of insertion by applying binary search within the already inserted elements. This can be done by linearly moving towards left or right side of the array if you hit an empty space in the middle element.
# '''Insertion''': Inserting the element in the position found and swapping the following elements by 1 position till an empty space is hit. This is done in logarithmic time, with high probability.
# '''Re-Balancing''': Inserting spaces between each pair of elements in the array. The cost of rebalancing is linear in the number of elements already inserted. As these lengths increase with the powers of 2 for each round, the total cost of rebalancing is also linear.
==
'''procedure''' rebalance(A, begin, end) '''is'''
r ← end
w ← end × 2
'''while''' r ≥ begin '''do'''
A[w] ← A[r]
A[w-1] ← gap
r ← r − 1
w ← w − 2
'''procedure''' sort(A) '''is'''
n ← length(A)
S ← new array of n gaps
'''for''' i ← 1 to floor(log2(n-1)) '''do'''
rebalance(S, 1, 2^(i-1))
'''for''' j ← 2^(i-1) to 2^i '''do'''
ins ← binarysearch(A[j], S, 2^i)
insert A[j] at S[ins]
Here, <code>binarysearch(el, A, k)</code> performs [[binary search]] in the first {{mvar|k}} elements of {{mvar|A}}, skipping over gaps, to find a place where to locate element {{mvar|el}}. Insertion should favor gaps over filled-in elements.
== References ==
Line 296 ⟶ 67:
{{sorting}}
[[Category:Comparison sorts]]
[[Category:Stable sorts]]
|