If you have made your way to this page, by searching for
something else, I am sorry. The big ‘O’
relates to the efficiency of a program, maybe not what you were looking for.
The efficiency of computer programs has been a long-standing
concern with programmers. The first
computers to say the least were not efficient at all. Taking hours or even days to sort through
files of data. There have been several
different types of sorting algorithms presented to us. In CSC108 we learned about selection sort,
insertion sort, and bubble sort.
Programs like selection sort seem to have no applicable applications
besides as a teaching tool. With both
the best case and worse case scenarios being O(n2). The bigger the dataset the more extreme the
time becomes in sorting.
Bubble sort is a little bit better. Although the worst case scenario bubble sort
is no better than the selection sort, but in the best case with an already sorted
list runtime is O(n). Insertion sort
has similar efficiency as bubble sort, in both the best case and worse case
scenarios. That was CSC108.
Now, CSC148 has taken this even further. With a more in-depth look at efficiency, we
studied more sorting algorithms, such as merge sort, quick sort, and the
built-in sort function of python, timsort.
The performance of each of the sorting algorithms in the worst case is
better than everything learned in CSC108.
The runtime is O(nlgn) in worse case for timsort and merge sort. In quick sort the runtime is O(n2)
in the worse case. In the best case
scenario merge sort and quick sort are both O(nlgn) and timsort is O(n). Merge sort and quick sort work similar to
each other. They take a python list and
divide the list in half several times.
The difference arises in the selection of the pivot point in quick
sort. In the worse case the pivot point
is always the lowest value in the list.
Doing it this way means that the program has to keeps separating the
list unevenly. In the best case
scenario the list is roughly divided in half by the values greater than the
pivot and values less than the pivot.
These new lists are then sorted the same way recursively calling the
quicksort function.
In merge sort, the list is divided in half, and then half
again, and so on until, there is one value in each list. Then these smaller lists are compared to
each other, and the value of list is merged with the other in order. The smaller of the 2 values is merged to the
new list first, and then the next comparison is made.
Pyhton’s built in function timsort would be the best sorting
algorithm to use, based on the ones that have been presented thus far. In the worse case it is O(nlgn) and only
gets better in the best case.
The implementation of sorting algorithms can help us study the overall performance of most any program that we write. Understanding the underlying methods that python uses and how many times we make 'unnecessary calls' can really increase the overall efficiency of a program and less time waiting.