0% found this document useful (0 votes)
123 views27 pages

Smooth Sort & Leonardo Heap

Smooth sort is an improvement on heapsort that aims to achieve an average-case runtime of O(n log n) while maintaining a best-case performance of O(n). It uses a data structure called a Leonardo heap, which represents a forest of balanced max-heap trees where the number of nodes in each tree is a Leonardo number. Smooth sort constructs a Leonardo heap from the input array, repeatedly removes the maximum element and places it at the end of the array while rebalancing the heap, achieving an overall runtime of O(n log n).

Uploaded by

Varun
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
123 views27 pages

Smooth Sort & Leonardo Heap

Smooth sort is an improvement on heapsort that aims to achieve an average-case runtime of O(n log n) while maintaining a best-case performance of O(n). It uses a data structure called a Leonardo heap, which represents a forest of balanced max-heap trees where the number of nodes in each tree is a Leonardo number. Smooth sort constructs a Leonardo heap from the input array, repeatedly removes the maximum element and places it at the end of the array while rebalancing the heap, achieving an overall runtime of O(n log n).

Uploaded by

Varun
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 27

SMOOCH SORT

& Leonardo Heaps


Why Smooth Sort?

Heapsort being the direct ancestor of smooth sort was slightly inefficient in managing large
data sets as it had the next, worst and the average case complexities of nlogn.

The respective steps for heapsort being as follows:

1) Creation of a max heap from the given array. {O(n)}


2) Subsequent deletion of the root node from the max heap and placing it at the end of the
array. {O(n*logn)}
Why Smooth Sort?

Smooth sort aims at attaining the worst case complexity of O(nlogn) but a best case complexity
of simply n which is a great deal!
Overview of Smooth Sort

Smooth sort has a very similar implementation as that of heap sort but instead of a binary heap
tree, we will use a forest of several binary max-heap trees.

This absurd data structure representing the forest is called a ‘Leonardo Heap’.
Leonardo Heaps

A leonardo heap represents a forest of balanced max/min heap trees where the number of
nodes in each heap is a ‘Leonardo Number’

Apart from this, the array representation of each heap is a bit different. Instead of the root
element being the leftmost element and it’s children being on the right side of it, leonardo
heaps contain the root element in the rightmost index and it’s children lie to the left side.
Leonardo Numbers

Leonardo Numbers are the numbers that belong to the leonardo series, a series very similar to
the fibonacci sequence!

A leonardo series is represented by:


L(0) = 1;

L(1)=1;

L(n)=L(n-1)+L(n-2)+1;

The first few terms of the leonardo sequence are:

1 1 3 5 9 15 25 . . .
Leonardo Numbers

2 really important lemmas which form the foundation of leonardo heaps are:

1) Any positive integer can be written as the sum of O(logn) distinct leonardo numbers thus
signifying that any binary tree of n numbers can be translated to a leonardo heap of k
different binary max heaps.
2) For any n, the number of binary trees needed to form the heap k is at most log n.

These lemmas are proven but are far beyond the reach of discussion for this particular topic.
Leonardo Heaps (contd.)

As discussed earlier, the number of heaps in the leonardo heap could be: 1 or 3 or 5 or 9 or 15
and so on.

Some really important features of the leonardo heaps is that:

1) The number of nodes in the subsequent max heaps keep on decreasing.


2) The root node of subsequent max heaps keep on increasing.
Leonardo Heaps: Operations

Any data structure is insignificant without significant fundamental operations on it.

The operations that are gonna be useful to us are:

1) Insertion
2) Deletion
Leonardo Heaps: Operations->Insertion

There are 3 steps for insertion:

1) Ensuring that the resulting heap has the correct shape


2) Ensuring that the tops of each and every heap is arranged in ascending order from left to
right.
3) Ensure the max heap property and heapify.
Insertion: Step 1

While inserting an element, 1 of the 2 cases might arise:

1) The number of elements in the 2 smallest leonardo trees after inserting the new element
will be a leonardo number
2) The number of elements in the 2 smallest leonardo trees after insertion of the new
element won't be a leonardo number.
Insertion: Step 1 -> Case 1

If it follows the first case, we merge the 2 smallest heaps with the new element as the root of
the new merged heap.
Insertion: Step 1 -> Case 2

If the circumstance follows the second case instead, we simply create a new singleton heap
with just the new element as the only 1 element in the heap.
Insertion: Step 2

For sorting the different leonardo trees/heaps, we practically use insertion sort in a much more
complex way (as we know that heaps can be represented as arrays).

The new element isn’t necessarily the largest element as we haven’t yet restored the property
of the max heap.

We also can’t just naively swap the root to its supposedly right position.
Insertion: Step 2 (contd.)

Thus, we apply insertion sort and swap only if the root is greater than the root of its preceding
tree and its sub tree's roots to ensure a ‘mostly heapified’ heap.

In the end we will end up with several heaps in the leonardo tree/forest where the heaps might
not be perfectly heapified but the maximum element of each heap will be on the top and will
be in ascending order as we move from left to right.

This ensures simpler and faster heapification


Insertion: Step 3

Now, we simply heapify the heaps to get the desired leonardo heap
Deletion

This process is similar to the first step of insertion of the element.

We consider 2 cases:

1) The number of elements after deletion is a leonardo number


2) The number of elements after deletion is not a leonardo number
Deletion: Case 1

If this is the scenario:

We simply remove the root of the rightmost heap as the resultant heap will still be a leonardo
heap.
Deletion: Case 2

In this case, we will delete the root element from the rightmost heap, arrange the heap as a
normal heap but exclude the element not following the min-heap criteria to a singleton heap.
Smooth Sort

The smooth sort was implemented in 3 different phases, each phase making the algorithm
more efficient.

The algorithm of smooth sort is very similar to that of the heapsort:

1) Construct a leonardo max heap in O(n) time.


2) Set x to the index of the last spot in the array
3) While the heap is not yet empty, remove the maximum element of the heap and place it
at position x then move x to the previous position.
4) Rebalance the max heap
Smooth Sort

If we perform the previous algorithm in an explicit manner (with converting the array to a heap
and performing operations over there instead of the array itself), we get the worst case
complexity of O(nlogn) and an extra O(n) memory to maintain the binary heap.

Thus, we prefer to convert the explicit heap to a mostly implicit leonardo heap as it only takes
O(logn) extra space instead of O(n).
Smooth Sort: Conversion to a Mostly Implicit Heap

We know how to represent a single heap in an implicit manner but leonardo heap is practically
a junction of many heaps altogether.

For this conversion we need to know about order:

Order: Every leonardo number has an order that is the index of the number where it lies
in the leonardo sequence.

Lt0 = 1; Lt1 = 1; Lt2 = 3; Lt3 = 5; Lt4 = 9 and so on.


Smooth Sort: Conversion to a Mostly Implicit Heap

A singleton leonardo heap can be simply represented by an array in the same manner as we
used to do with simple heaps
Smooth Sort: Conversion to a Mostly Implicit Heap

The root of the tree is at position L(k) - 1.

The root of the Ltk-1 subtree is at position L(k-1)-1.

The root of the Ltk-2 subtree is at position L(k)-2.


Smooth Sort: Conversion to a Mostly Implicit Heap

We have learnt how to represent a single leonardo tree.

For a complete forest, we simply represent individual trees implicitly and concatenate the
arrays.

Alongside, we keep on maintaining another auxiliary array which stores the sizes of the
concatenated arrays (or the individual leonardo tres).
Smooth Sort

Thus, we could rewrite the implementation of smooth sort as:

1) Construct a mostly implicit leonardo max heap from the input sequence
2) While the heap is not empty, remove the maximum element from the heap and place it
at the back of the sequence
3) Rebalance the max-heap.

This only takes the worst case complexity from O(n) to O(logn)
THANKS

You might also like