Saturday, June 9, 2012

how to learn complexities of sorting algorithms

The sorting algorithms we usually come across are:

Bubble , Insertion, Heap, Merge, Selection and Quick Sorting and it is very difficult to learn the complexities of all these algorithms. It becomes a more tedious task when it comes to three cases of complexities(Worst, Average, Best).

Below is a method to memorize all the complexities(including average, best and worst cases) of these six algorithms.

First letter of every sorting stands for that sorting. For ex.- B stands for Bubble sort, I stands for Insertion sort and so on....
there are only types cases of complexities:
  1. n
  2. n2(n square)
  3. nlogn
(dont worry about the complexity "n", it is not used frequently)

the trick to remember is as follows:

prepare a table in which first column contains all the algorithms names(in any order)
and first row contains all three cases(worst, average and best) as shown in figure in the specified order(i.e. increasing from worst to best).


Now, the words you have to remember are


  • BIn

B means bubble sort
I means insertion sort

both Bubble and insertion sort have same set of complexities but their best case is different than other. so remember "BIn": short for "Best Is n".


  • HtMl

H: Heap
M: Merge
t: just to remeber the whole word. it is of no use otherwise.
l means complexity "nlogn".

both Heap and Merge have same set of complexities and also all of them are nlong(l).


  • S stands for selection sort and "square". so all three of selection sort are n2.

  • Qsl(Queen of  scott-land): first column of quick sort is n2(as "s" in scott) and rest are nlong (as l in land).
Note: nlogn is used whenever it is specified otherwise use n2 as default
like: in BIn, use n2 in average and worst cases.
as I said earlier that complexity "n" is not much of use as it is used only twice that too can easily be memorize as "best is n".


Practice this two or three times and you will remember it as good as anything.







Thank you,

Please feel free to leave any comment or suggestion.

No comments:

Post a Comment

For any query, feedback, suggestion..
Feel free to comment here...