Monday, 31 March 2014

SLOG #9: Sorting and Efficiency

Sorting and Efficiency are very important concepts in computer science. It's quite normal in real life to sort a series of data for example, ranking students' grades. It's significant to use a quick and space-efficient way to sort data, especially in cases where the data size is huge.

The speed of sorting is based on the sequence of the data. We describe the speed in three cases: the best case, the average case, the worst case.

We use big-Oh to describe the speed of sorting. Running time is a description based on the input size n. When describing this algorithm, we ignore the constant c in running time so the algorithm is shown in O(g(n)) instead of c g(n). It's actually very smart to scale speed in this way. Since it is obvious n^2 is much larger than c n when n is huge (some constant c). We could easily compare distinct methods' speed in this way. For example we describe the speed of merge sort as O(n log n) and the speed of selection sort as O(n^2). Thus selection sort is slower than merge sort. Scaling of insertion algorithm, selection algorithm and bubble algorithm is all O(n^2) in average cases so we could tweak implementation of the same algorithm, combine these methods, and find the best way.

We are going to discuss four sorting algorithms in detail: selection sort, insertion sort, quick sort and merge sort.

Selection sort
Selecting sort selects the smallest element from the remaining n-i right-post position and swaps it into position. Its efficiency is O(n^2) in the best and the worst case. Actually the same number of steps is taken with the same size n. This way of sorting is the slowest one compared to other three ways of sorting. However it's quite natural in playing cards in real life.

Insertion sort
Insertion sort is similar to selection sort. It takes the next element (at position i) and insert it into its proper place in the left-most i +1 positions. Its efficiency is O(n^2) in the average and the worst case but O(n) in the best case where the list is generally sorted. Thus insertion sort is a little bit quicker than selection sort. However since their algorithms are the same in the average case and it's rare to sort a sorted list, its speed is still slow.

Quick sort
Quick sort is much better than selection sort and insertion sort in average cases. Quick sort works by somehow choosing a pivot and breaking a list into two sublists (everything smaller than the pivot, everything larger than the pivot). It continues to break down the list in this way until the list can't be broken down and brings all the sublists together. Its efficiency is O(n log n) in the average and best case however O(n^2) in the worst case. The worst case is we keep choosing the greatest or smallest item as the pivot and it has the same efficiency as selection algorithm in this case. However we hardly have the chance to keep choosing the greatest or smallest item as pivot. We could choose to find the pivot by finding the first, last and middle elements of the list and choose the median as the pivot to avoid this. In general, quick sort is still very efficient compared to selection sort and insertion sort.

Merge sort
Merge sort is similar but better than quick sort. Merge sort works by dividing the list in half until it can't be divided anymore and then mergesorting each of the two halves. The efficiency of merge sort is O(n log n). The same steps in any cases with the size n are taken. It takes log(n) steps to break the list into pieces and then mergesort all elements with log(n) steps and n comparisons in each step so its efficiency is O(n log n).

In the class, we also discuss a little bit about count sort and python built-in sort Timsort. Counting sort is very convenient to small integers with efficiency O(n) basically. Timsort is a combination of merge sort and insertion sort. In the last lab, we used test_sort to test various algorithms’ performance and found Timsort is the quickest.

Efficiency is very important in computer science especially when we need to process huge amounts of data. I really enjoy learning about these topics. Feel free to leave comments here.

No comments:

Post a Comment