To sort a list of data means to rearrange it to be in order. There are applications of sorting algorithms everywhere we look, from organizing a library in alphabetical order to arranging scores in athletic competitions. We have learnt that in computer science, it is often very useful to have the data sorted, as it enables more efficient search algorithms (eg Binary Search).
http://www.question-defense.com/wp-content/uploads/2010/03/itunes-music-library.gif |
We have learnt that there are many ways to approach the problem of sorting, and some are more efficient than others. The main algorithms that we talked about in class are listed below from the most inefficient to the most efficient:
- bubble sort
- selection sort
- insertion sort
- merge sort
- quick sort
The first three are less efficient ( O(n^2) ).
Bubble sort works by going through the list comparing each item to the next one, exchanging them if list[i+1] is greater than list[i], "bubbling" an item until a greater one is found or the end of the list is reached placing the largest item at the end. The process is repeated n-1 times, bubbling through a smaller list every time. This algorithm is considered to be the most inefficient as it can often exchange the same variable several times before it is at is final location.
Selection sort similarly requires n-1 passes, however this time each pass requires only one exchange. First t the whole list is considered, the min of the list is found, and replaced with the first item of the list, then the algorithm repeats the remaining n-2 times until the end is reached.
Insertion sort also requires n-1 passes, creating a sorted a sublist from the first item of the list and inserting each new value into the sorted sublist at it's correct position.
Merge sort is different from the previous three algorithms. It is a recursive algorithm that works by continuously splitting the list in half until each item is a single element list, then going backwards by merging the original lists in correct order. Merge sort (unlike quick sort) has a consistent efficiency of O(n log n) however it also requires more memory to be storing one half of the list while performing operations on the other.
Quick sort is similar to merge sort in that it is a divide and conquer (recursive) algorithm. It works by picking a pivot value and going through the rest of the list from both sides. Swapping values that are in the wrong place (>pivot value going from left, <pivot value going from right), until the search crosses and the location for pivot value is found (all items less than pivot value on one side, greater on the other). At this point we swap pivot value with the item at pivot value location, and recursively call quicksort on list[:pivot_value_location] and list[pivot_value_location]. Quick sort doesn't require as much memory as merge sort, however it's efficiency can be compromised by choosing pivot value that is too big or too small (optimized when the pivot value is close to the median)
http://sortvis.org/images/dense-quicksort.png |
PS :
I found some really cool visualizations of different sorting algorithms like the one above that you can check out here:
also this video is pretty neat: