Monday, March 31, 2014

Sorting and Efficiency

Hello pythoneers! Have you ever wondered what you would do if you dropped a deck of cards? Well, if you're like me you would probably pick them up and remake the stack. BUT, what if you had obsessive-compulsive disorder and needed to sort every card in the deck to keep them in order? Then you would probably want to know the fastest way to sort your cards! If you think you fit the criteria for this compulsion, then this sounds like a slog entry for you!
You see, in 148 recently we've been learning about sorting algorithms and their efficiencies. The ones we focused on were bubble sort, insertion sort, selection sort, quicksort and mergesort. You might recall seeing a few of these from 108 and not knowing why they would be useful, but now I understand! Here's where sorting could improve your well-being: Let's say we have a huge list of data, like the index at the back of a textbook. Then, wouldn't it be insanely hard and time-consuming to search for something if the list wasn't sorted? You could save yourself loads of time by sorting this list first and then searching for what you want. Now, depending on how your list is sorted - ex: reversed, sorted - some sorting algorithms may be a better choice than others. For instance, in a best-case and a worst-case scenario selection sort would run at an efficiency of n^2, making it not that great of an algorithm (sorry selection sort). Mergesort however has an efficiency of nlogn for both cases!! wow! Lab #10 proved to be super helpful for getting to know my sorting algorithms. In that lab we compared these algorithms' runtimes by making graphs and by using different types of lists of various sizes. What my partner and I noticed is that bubble sort was pretty bad in every situation, while others proved to be quicker. The coolest thing about it was learning about the built-in sort that python uses. When you ask python to sort a list for you, it will choose the most efficient one according to the list you give it!!
Also, I was on Piazza the night before the second test and noticed that someone posted a link to a list of best/average/worst-case complexities for sorting algorithms. I'm guessing you want to see it so here you go!!!! http://bigocheatsheet.com/ You're welcome :)