Sorting and Efficiency are two topics in Computer Science that is essential in being a good Computer Scientist. Sorting is simply organizing objects in a particular order, this order is generally done ascending or descending. Efficiency is the measure of performance over time of a program and maximizing efficiency is the key to writing good program as low run times is what computer scientists strive for in their code. These two concepts together sees the subject of comparison between sorting efficiency of algorithms. Different sorting algorithms will accomplish the same task with different methods that vary in efficiency.
Many different types of sorting algorithms exist and is nearly impossible to talk about them all in this Blog. A few popular ones that everyone knows include Bubble sort, insertion sort, selection sort, quick, merge, and tim sort. As it turns out, the examples provided are also listed in order of efficiency with bubble sort being most inefficient while tim sort having best efficiency. To be efficient, the sorting algorithm must be able to able to perform the program with relative short time. This efficiency can be measured in terms of constant time and and the number of iterations we use within our algorithm. This "time complexity" lives within a notation denoted by "Big-O" and can represent an algorithms worst, average, and best expected run time with respect to constant time. The most common run times we use to analyze the run times of programs are denoted by the following; O(n), O(lg n), O(n lg n), O(n^2), O(2^n), O(n!), and O(1). It should be mentioned that the lower number within the brackets represent the most efficient run time.
Lets analyze selection sort, the algorithm can be found below:
def select(L):
"""Produce copy of L in sorted order
>>> select([3, 1, 2])
[1, 2, 3]
"""
L1 = L[:]
for i in range(len(L1)):
m = min(L1[i:])
j = L1.index(m,i)
L1[i], L1[j] = L1[j], L1[i]
return L1
We have a list <L1> which is being iterated through by the for loop, assuming our list length is n, iterating through n items will take O(n) time. Everything else within the loop is O(1) time approximately because we generalize variable assignments and such to this order, rendering the program O(n). This makes sense, but the line where we copy <L> to <L1> could also take O(n) steps if we were to imagine it with a for loop. This is to say the program has O(n) + O(n) time, but this is would simply be 2*O(n) which is a constant multiplied by our scaling time complexity. The key to time complexity is that we only care about scaling variables, therefore we drop the 2 and end up with O(n) time.
Sorting is a very interesting in computer science as unlimited opportunities exist for the algorithm to be even more efficient. Some algorithms are more efficient in certain cases while perform worse in others. Efficiency is key in writing code as runtime really starts to matter when the program is executing on objects that are very large.
No comments:
Post a Comment