Programming the God’s Way — I
What if humans were just programs of gods? What if all that’s happening around is just a code running in her/his supercomputer? If you want to learn programming the god’s way and relate each concept with the real life occurrences that happen around us, hop in this new series with me folks!
‘SORTING’ YOUR WAY TO ORGANIZATION
Hola all my neatness freaks and decluttering experts! (Even if you just love coding and landed in here somehow, it’ll suffice)
Ever found the different sorts in data structures more complicated than the relationship with your ex-vacuum cleaner? Well, here’s a breakdown of various types of sorting algorithms which you can apply in your daily life and maybe make order out of chaos, a less monotonous and more innovative process:
P.S: Assume ascending order is the new ‘fad’ while going through the following examples:
1. Bubble & Selection Sort
Situation: Watching a movie in a hall with people of different heights. The movie is yet to begin and everyone is looking here and there in discomfort,some craning their necks up and others down.
Bubble Sort: Find the giraffe of the group and convince him/her sit at the back! i.e The first person at the front compares his/her height with the one behind.If the former is taller,he/she swaps.Else he/she need not move.The process(consisting of ‘passes’) continues till the largest is at the back.Thereby find the second largest,third largest and so forth making the least tall sit in the front.
Selection Sort: Find the mayfly of the pack and convince him/her sit at the front! i.e as opposed to Bubble Sort,we detect the smallest person,second smallest and so on making the least small sit at the back.
(Same work,just direction of approach differs.)
P.S: We love all the giraffes and mayflies, don’t bully me for bullying now.
2. Merge & Quick Sort
As the Romans loved, ’maxim divide et impera‘ aka Divide and Conquer algorithms
Situation: The line to attend the music concert is too huge to handle at once
Merge Sort: The guards divide the single line into 2 sub-lines and the people who have already been checked by their ID cards, form the third line i.e containing the ones which can simply pass through the gates. (3 lines i.e the waiting area better be big for this out-place sorting)
Quick Sort: At random, the guards select the pivot i.e any person standing in the queue. (You get to be a V.I.P somewhere lol) Then they rearrange the entire line by asking the people having lower ID card numbers than the pivot stand before him/her and the ones with a higher ID card number stand behind him/her.
3. Insertion Sort
Situation: Playing a game of cards (Poker or Blackjack fans anyone?)
Sort: The player segregates his pack of cards according to their desired places, by removing each card, creating a space for it and finally inserting it.
4. Heap Sort
Situation: Assume that in a nation with 2 child policy ,a kid gets a homework question: to draw his/her family tree, starting from the oldies to the youngsters and thy strict teacher hands him/her, a bunch of empty cutouts (data in a heap).
Note: This sort forms a max heap in the form of a complete binary tree or descending ordered items by default.
Sort: In order to wade through the heap systematically & speedily (same time taken in average, best and worst case scenarios!), the smart kid writes names of his/her family members on the cutouts and piles them up to do the following:
a) Find and remove the greatest ancestor from the pile (Deletion)
b) Place him/her on the top (root of the tree) of the chart paper (Insertion)
Then the above steps are repeated with subsequent smaller aged members (Yeah mums and aunts, you need not lie about your age any longer!) and lo & behold, a tree is obtained.
5. Bucket/ Bin Sort
Situation: A college library with a rack of empty shelves labelled from ‘A’ to ‘Z’ and a tower of boring new reference books to be arranged in them (Why do the pages smell so good though!)
Sort: Group each book according to their titles, starting with the same alphabet and then arrange them subsequently in the shelves.
That’s all for now folks! Stay tuned for the next part, coming soon to your Medium Story Shelf ;)