Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Smoothsort
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==Overview== Like [[heapsort]], smoothsort organizes the input into a [[priority queue]] and then repeatedly extracts the maximum. Also like heapsort, the priority queue is an [[implicit data structure|implicit]] heap data structure (a [[Heap (data structure)|heap]]-ordered implicit [[binary tree]]), which occupies a prefix of the array. Each extraction shrinks the prefix and adds the extracted element to a growing sorted suffix. When the prefix has shrunk to nothing, the array is completely sorted. Heapsort maps the binary tree to the array using a top-down breadth-first traversal of the tree; the array begins with the root of the tree, then its two children, then four grandchildren, and so on. Every element has a well-defined depth below the root of the tree, and every element except the root has its parent earlier in the array. Its height above the leaves, however, depends on the size of the array. This has the disadvantage that every element must be moved as part of the sorting process: it must pass through the root before being moved to its final location. Smoothsort uses a different mapping, a bottom-up depth-first [[Tree traversal#Post-order, LRN|post-order traversal]]. A left child is followed by the subtree rooted at its sibling, and a right child is followed by its parent. Every element has a well-defined height above the leaves, and every non-leaf element has its ''children'' earlier in the array. Its depth below the root, however, depends on the size of the array. The algorithm is organized so the root is at the end of the heap, and at the moment that an element is extracted from the heap it is already in its final location and does not need to be moved. Also, a sorted array is already a valid heap, and many sorted intervals are valid heap-ordered subtrees. More formally, every position {{mvar|i}} is the root of a unique subtree, whose nodes occupy a contiguous interval that ends at {{mvar|i}}. An initial prefix of the array (including the whole array), might be such an interval corresponding to a subtree, but in general decomposes as a union of a number of successive such subtree intervals, which Dijkstra calls "stretches". Any subtree without a parent (i.e. rooted at a position whose parent lies beyond the prefix under consideration) gives a stretch in the decomposition of that interval, which decomposition is therefore unique. When a new node is appended to the prefix, one of two cases occurs: either the position is a leaf and adds a stretch of length 1 to the decomposition, or it combines with the last two stretches, becoming the parent of their respective roots, thus replacing the two stretches by a new stretch containing their union plus the new (root) position. Dijkstra noted{{r|EWD-796a}} that the obvious rule would be to combine stretches if and only if they have equal size, in which case all subtrees would be perfect binary trees of size {{math|2<sup>''k''</sup>β1}}. However, he chose a different rule, which gives more possible tree sizes. This has the same [[Asymptotic analysis|asymptotic efficiency]],{{r|hertel}} but gains a small constant factor in efficiency by requiring fewer stretches to cover each interval. The rule Dijkstra uses is that the last two stretches are combined if and only if their sizes are consecutive [[Leonardo number]]s {{math|''L''(''i''+1)}} and {{math|''L''(''i'')}} (in that order), which numbers are recursively defined, in a manner very similar to the [[Fibonacci number]]s, as: * {{math|1=''L''(0) = ''L''(1) = 1}} * {{math|1=''L''(''k''+2) = ''L''(''k''+1) + ''L''(''k'') + 1}} As a consequence, the size of any subtree is a Leonardo number. The sequence of stretch sizes decomposing the first {{mvar|n}} positions, for any {{mvar|n}}, can be found in a greedy manner: the first size is the largest Leonardo number not exceeding {{mvar|n}}, and the remainder (if any) is decomposed recursively. The sizes of stretches are decreasing, strictly so except possibly for two final sizes 1, and avoiding successive Leonardo numbers except possibly for the final two sizes. In addition to each stretch being a heap-ordered tree, the roots of the trees are maintained in sorted order. This effectively adds a third child (which Dijkstra calls a "stepson") to each root linking it to the preceding root. This combines all of the trees together into one global heap. with the global maximum at the end. Although the location of each node's stepson is fixed, the link only exists for tree roots, meaning that links are removed whenever trees are merged. This is different from ordinary children, which are linked as long as the parent exists. In the first (heap growing) phase of sorting, an increasingly large initial part of the array is reorganized so that the subtree for each of its stretches is a max-heap: the entry at any non-leaf position is at least as large as the entries at the positions that are its children. In addition, all roots are at least as large as their stepsons. In the second (heap shrinking) phase, the maximal node is detached from the end of the array (without needing to move it) and the heap invariants are re-established among its children. (Specifically, among the newly created stepsons.) Practical implementation frequently needs to compute Leonardo numbers {{math|''L''(''k'')}}. Dijkstra provides clever code which uses a fixed number of integer variables to efficiently compute the values needed at the time they are needed. Alternatively, if there is a finite bound {{mvar|N}} on the size of arrays to be sorted, a precomputed table of Leonardo numbers can be stored in {{math|''O''(log ''N'')}} space.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)