Sorting, sorting, sorting... First of all, what is sorting? Well the idea is really simple, sorting is just taking a collection of elements and putting them in order, according to some trait be it length, size, value, etc. What makes it an interesting computer science is not what sorting is but rather HOW we sort, what algorithm or set of rules we follow to sort. There are numerous types of algorithms, and generally we can say the "best" algorithm is the most "efficient", that is, the one that takes the least amount of time to sort a given set. However, due to the varying types of set we are given, it is generally true that there is no "best" algorithm, although certainly there are _better_ algorithms. To see the varying efficiency of different algorithms on different types of shuffled sets, you can play with this animation:
http://www.sorting-algorithms.com/.
You will note that there are several different types of algorithms outlined in the linked animations. Let's try to understand some them and how efficient they are (to help you understand I will link to folk dances on youtube that model different sorting algorithms (seriously check these out they are great))!
Bubble Sort:
https://www.youtube.com/watch?v=lyZQPjUT5B4
(1) Starting at the beginning of the collection, go through the entire collection and swap consecutive pairs of elements if they are out of order.
(2) If no swaps occur, the collection is sorted, otherwise, go to (1)
Bubble sort is a simple algorithm in principle. However, it is not terribly efficient. In the best case scenario, bubble sort passes through all n elements of the list once, and has O(n) efficiency, otherwise it passes through all n elements up to n times and has O(n^2) efficiency on average, and in the worst case.
Insertion Sort:
(1) Consider the entire collection unsorted.
(2) Move the first element of the unsorted section into the sorted section, inserting it where it must fit by comparing it to the elements of the unsorted section until it is larger than an element.
(3) If there are no more elements in the unsorted section, the collection is sorted, otherwise, go to (2)
Like bubble sort, insertion sort has O(n^2) efficiency on average and in the worst case, and O(n) efficiency in the best case. However, since less comparisons need to be made for each pass than in bubble sort, it is generally more efficient.
Selection Sort:
https://www.youtube.com/watch?v=Ns4TPTC8whw
(1) Consider the entire collection unsorted.
(2) Move the smallest element of the unsorted section into the sorted section by comparing all the elements in the unsorted section to a temporary "smallest element" and replacing it if a smaller element is encountered.
(3) If the unsorted section is empty, the collection is sorted, otherwise, go to (2)
Selection sort passes through the entire list n times and makes n + (n - 1) + (n - 2) + ... + 1 comparisons no matter what. It has O(n^2) efficiency in all cases.
Merge Sort:
(1) Split the collection into two sections.
(2) Merge sort each sub-collection.
(3) Combine the two sorted sub-collections.
This sorting algorithm uses the magic of recursion. It essentially splits the collection into n pieces and the puts them back together. It has O(nlogn) efficiency in the worst and average case.
Quick Sort:
https://www.youtube.com/watch?v=ywWBy6J5gz8
(This dance is my favourite)
(1) Pick a pivot element from the collection (in the dance, this would be the black hat)
(2) Move all the elements smaller than the pivot into one sub-collection before the pivot and all the elements larger than the pivot into another after the pivot. The pivot is now sitting in its final position.
(3) Quick sort the sub-collections.
This sorting algorithm also uses recursion. Again there is splitting of the initial collection into n pieces, however, once split out of the sub-collections, each pivot is now in its final position. Comparisons are made for each element to the pivot for each pass of quick sort. It has O(nlogn) efficiency on average, and is (generally) faster than other O(nlogn) algorithms. This however depends: as in the animations we see that when there are lists containing several non-distinct elements ("few unique") quick sort performs much worse than merge sort.
To read more sloggy material on sorting algorithms, check out this slog:
http://slogmyblog.blogspot.ca/, which describes in more detail an efficiency measuring notation I've used, known as "big-O".
FINAL (slightly off topic) NOTE:
This week's slog is brought to you by, bogobogosort the best worst sorting algorithm ever.
A few years ago, I took a one day class on sorting algorithms, and at the end of the day, we were divided into teams for different algorithms and raced see which algorithm was "most efficient" by trying to sort a set of cards with numbers. I was part of team bogosort.
Bogosort is a sorting algorithm sometimes known as Monkey sort. The idea is pretty simple:
(1) Shuffle the set at random.
(2) If the set is now in order, you are done. Otherwise, go to (1).
Bogosort has O(n!) efficiency on average (since it is simply a probability calculation).
As you can imagine, although team bogosort failed miserably at actually sorting our set of cards, we arguably had the most fun, throwing them in the air and checking to see if they were in the correct order. Although I am impressively fond of bogosort, I discovered (during extra time during lab) the magic of bogobogosort, which takes all of the randomness of bogosort and makes far less efficient. If you want to read more about bogobogosort (which I'm sure you do!) you can check out:
http://www.dangermouse.net/esoteric/bogobogosort.html, which provided me personally with much joy.