Could someone please shed some light on the workings of A-sort (not asort()), which uses (a,b)-trees for sorting partially sorted sequences?
The algorithm itself is pretty simple - basically you insert all the elements in the tree, then traverse the tree to read the elements in order.
To get better performance for almost already sorted sequences, it uses the following modifications:
b>2a-1, that has O(1) amortized insert rebalancing time.To leverage the O(1) insert time, you just have to quickly find the place the next element belongs, assuming the sequence is nearly sorted (so it won't be far from where the previous element inserted). This is done roughly as follows:
This way, you can go traverse a^k elements of the tree in k steps, therefore, it will take you O(log(distance(current, previous)) time to find the proper place for the new element. Given amortized O(1) rebalancing, you get better than O(n log n) time for almost sorted sequences, while maintaining O(n log n) worst-case.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With