There are lots of times when one needs to sort the data in a list in order for the data to be useful. Pythons builtin sort, Timsort, is the most efficient sorting algorithm. It is a hybrid of merge sort and quick sort, and is O(n logn). This seems to be the optimal efficiency of a sorting algorithm and is often achieved by repeatedly splitting the list into half, causing the logn, and each individual step being linear(causing n). This type of behaviour is displayed in both quick and merge sort, except merge sort is less efficient than quick sort because it takes extra storage to split the lists and then merge them together.
There are many different sorting algorithms used in python, some better than others. The worst efficiency to have is O(n!), followed by O(n^n). The hierarchy as listed in lecture is as follows:
O(1) <= O(lgn) <= O(n) <= O(n^2) <= O(n^3) <= O(2^n) <= O(n^n)
The worst sorting algorithm (in terms of efficiency) we've studied is bubble sort. It is O(n^2). Bubble sort starts at the beginning of the list and bubbles the item up the list until it reaches an item bigger than it. Eventually it bubbles the largest item to the end of the list and the list is sorted. Although selection and insertion sort are a little bit faster, they are also O(n^2) and have algorithms that involve iterating through n-1 items and making n comparisons. However, this varies for some lists. For example, since there are less exchanges in selection sort, in the list below, bubble sort makes 20 exchanges and selection only makes 8 (http://interactivepython.org/courselib/static/pythonds/SortSearch/sorting.html)
[54,26,93,17,77,31,44,55,20]
The other approach to sorting is often called "divide and conquer". This involves repeatedly dividing the list into half (log n splits) and sorting it through either recursively merging the sorted lists back together (merge sort) or using a pivot and partition process (quick sort). These sorting algorithms perform at O(n logn) even for large values of n, but are limited to n <1000 because of pythons maximum recursion depth (which is 1000). The most efficient sort, as stated at the beginning, is Timsort.
If theres one thing I've learned from this course, its to always stick to the builtins. Even if you think you can write something that works really fast and it seems to work, chances are there is something you didn't consider and your code is too specialized. This is definitely applicable to sorting algorithms, as pythons sort is both efficient and general enough because it works well in all cases. Although understanding the behaviour of sorting algorithms and how to achieve n logn was difficult at first, once I learned that the divide and conquer method uses ln n approach, it became simpler to understand. Sorting is a very important thing to understand when using python, and can be much more complex than someone who doesn't study computer science would expect.