Word wrap: Difference between revisions

From formulasearchengine
Jump to navigation Jump to search
en>FrescoBot
m Bot: link syntax and minor changes
 
Line 1: Line 1:
{{Infobox Algorithm
Hello and welcome. My name is Irwin and I totally dig that name. Managing people is what I do and the salary has been really satisfying. His family lives in South Dakota but his wife wants them to transfer. Body developing is 1 of the issues I love most.<br><br>Feel free to visit my web page - [http://xrambo.com/blog/191590 http://xrambo.com]
|class=[[Sorting algorithm]]
|image=
|data=[[Array data structure|Array]]
|time=<math>O(n^2)</math>
|best-time=<math>O(n)</math>
|average-time=<math>O(n\log n)</math>
|space=<math>O(n)</math>
|optimal=?
}}
'''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:
<blockquote>Suppose a librarian were to store his books alphabetically on a long shelf, starting with the A's at the left end, and continuing to the right along the shelf with no spaces between the books until the end of the Z's. 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 B's all the way down to the Z's 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.</blockquote>
 
The algorithm was proposed by [[Michael A. Bender]], [[Martín Farach-Colton]], and [[Miguel Mosteiro]] in 2004<ref>http://arxiv.org/abs/cs/0407003</ref> and published 2006.<ref name="definition">
{{cite journal | journal=Theory of Computing Systems | volume=39 | issue=3 | pages=391 | year=2006  | author=Bender, M. A.,  Farach-Colton, M., and Mosteiro M. | title=Insertion Sort is O(n log n) | doi = 10.1007/s00224-005-1237-z }}</ref>
 
Like the insertion sort it is based on, library sort is a [[stable sort|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(n<sup>2</sup>). The mechanism used for this improvement is very similar to that of a [[skip list]]. 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 library sort efficiency compares to 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 be implementation dependent. In the paper the size of the needed array is ''(1 + ε)n'',<ref name="definition" /> but with no further recommendations on how to choose ε. 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 is also adding an extra cost in the rebalancing step.
 
==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:
 
1. 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.
 
2. Insertion:
Inserting the element in the position found and swapping the following
elements by 1 position till an empty space is hit.
 
3. Re-Balancing:
Inserting spaces between each pair of elements in the array. This takes linear time, and
because there are log n rounds in the algorithm, total re-balancing takes
O(n log n) time only.
 
===Pseudocode===
 
'''proc''' rebalance(A, begin, end)
    r ← end
    w ← end * 2
    '''while''' r >= begin
        A[w+1] ← gap
        A[w] ← A[r]
        r ← r - 1
        w ← w - 2
 
'''proc''' sort(A)
    n ← length(A)
    S ← new array of n gaps
    '''for''' i ← 1 to floor(log2(n) + 1)
        '''for''' j ← 2^i to 2^(i+1)
            ins ← binarysearch(S, 2^(i-1))
            insert A[j] at S[ins]
 
Here, <code>binarysearch(A, k)</code> performs [[binary search]] in the first {{mvar|k}} elements of {{mvar|A}}, skipping over gaps. Insertion should favor gaps over filled-in elements.
 
== References ==
{{reflist}}
 
==External links==
*[http://www.cs.sunysb.edu/~bender/newpub/BenderFaMo06-librarysort.pdf Gapped Insertion Sort]
 
{{sorting}}
 
[[Category:Sorting algorithms]]
[[Category:Comparison sorts]]
[[Category:Stable sorts]]
[[Category:Online sorts]]

Latest revision as of 08:26, 11 February 2014

Hello and welcome. My name is Irwin and I totally dig that name. Managing people is what I do and the salary has been really satisfying. His family lives in South Dakota but his wife wants them to transfer. Body developing is 1 of the issues I love most.

Feel free to visit my web page - http://xrambo.com