Ungraded Lab 1- 3.3 Sorting Buckets

Hello,

I am really unable to understand the intuition behind the working of sorting algorithm, I do understand the code and its output at every step, but how is this sorting working in the first place?
The use of ticker and sticker specially?

Regards,

Hi @Aaditya1

The main idea is that you want to sort buckets, for example the 2 buckets hashes in the lab:
image

First bucket hash contains values 0123 and the second 4567 (offset by 4).


For sorting it and ā€œunsortingā€ we use ticker (indexes of the values):
image


This ticker will help us track where the values ā€œmoveā€. When we sort the buckets, we get this sticker:
image

Meaning the lowest value in the first bucket hash is index 0, the second lowest value is index 4 and so on.


To undo the sort we also keep track of the undo_sort variable:
image

Meaning that after sorting, to get back we want to take the first value at index 0, the second value at index 2, the third value at index 4 and so on.


When I wanted to understand the whole lab I implemented it in Excel (you can do it on paper if you prefer).

Hereā€™s the whole sequence of sorting and shuffling:

Later in the Assignment it gets more complicated so itā€™s better to understand it now.

Cheers

Hey @arvyzukai,
An intriguing explanation indeed, thanks for sharing it :nerd_face:

Cheers,
Elemento

1 Like

You explained it brilliantly @arvyzukai , but apart from this my one more pain point is why does this idea of ā€˜seqlen*buckets + ticker%seqlenā€™ work at the first place for sorting?

Is this a standard way to sort or something else, what is the reason for choosing that expression?

Thatā€™s a good question :+1: I corrected my response that might have caused the confusion because of my lapse of using ā€œbucketsā€ instead of ā€œhashesā€ (and not mentioning the offset).

So to answer why it works - is because the buckets in the second hash are offset by the number of buckets (4) so when we multiply by sequence length (seqlen*buckets), the minimum value in the second hash will never be lower than the max value in the first (31 vs. 32) and with the +ticker%seqlen part in the equation is becomes the ā€œinner battleā€ for position in the hash. (note the grey background row in the full picture)

I cannot answer this question competently because sorting algorithms is a research field on itā€™s own and I donā€™t have the expertise but my intuition lies that itā€™s just a simple arithmetic that works - computers do math pretty quickly (working with indexes) while moving stuff in the memory (moving values in list etc.) is slow. But I would ask someone else more competent on this.

Cheers

Thanks a lot!! Now I get it fully.

Regards,

Thanks @arvyzukai . My interpretation was that the +ticker%seqlen operation was required to make the sort stable (meaning that repeated invocations of the sort method would not result in a different ordering due to breaking ties randomly).

Hi @Rajiv_Bharadwaja

Your interpretation is correct :+1: and is the more elegant way than saying ā€œinner battleā€ :slight_smile:

Thanks for pointing that out!