Sorting Algorithm
“A sorting algorithm is an algorithm that puts elements of a
list in a certain order” (Wikipedia). There are different sorting algorithms
such as insertion, selection and bubble sort. Some of them are more efficient
and some are less. The efficiency of these sorting algorithms is usually based
on its runtime, but what are the elements that affect runtime? One element is
the number of steps, and the other one is the length of the list that we want
to sort. To calculate the number of steps we can look at the code and analyze
it.in each sorting we need to go through the list in order to compare values to
each other and then do the exchange if needed. If we calculate the number of
steps we can have an estimation of the complexity of the algorithm and somehow
the runtime. Usually we demonstrate the complexity of the algorithm by big O.
using this notation we show the greatest power of the result as it has the
greatest impact in the complexity.
For example for bubble sort, each pass starts with comparing
the first two elements and swap them if the first one is greater than the other
and then do this for each two pair until the end of the list. Then the next
pass starts until the list becomes sorted. For analyzing the complexity of this
algorithm we should pay attention to the number of comparisons that happen each
time. As in each pass the n-1 comparisons happen, and each time we have 1 item
less than the last pass, the overall steps would be the summation of (n-1) +
(n-2) + (n-3) +… +1 which is ½ n2 – ½ n. As the largest power is n2 we can
show the complexity of this algorithm as O(n2).