Sortings: Algorithms & Implementation & Analyze

Sorting is always the top topic in computer science. While today we usually use Array.sortall the time for the sake of simplicity, it’s important to learn algorithms and mechanisms behind. In this post, I’ll list 3 top sorting algorithms, with its implementation in Ruby, and time complexity.

Selection sort

The selection sort is the most basic and intuitive sorting algorithm. The idea is that starting from the first element from the array, we find the minimum element in the following array, then swap the current element with the minimum element. Thus, the minimum element is always replaced to the front. After we walk through the whole array, the smaller elements are all placed in the front. See the following graph for more details:

Implementation:

Time complexity:

The running time is O(n²) for the selection sort. Because for every element in length N array, it needs to look up the minimum element in the following array. It runs O(n²) in total to sort the array.

Merge sort

Merge sort has better performance than the selection sort. It’s done recursively. It has 3 steps to process an unsorted array:

  • Sort the first half of the array
  • Sort the second half of the array
  • Combine two sorted halves into one sorted sequence

Don’t worry if it sounds too abstract, see the following graph for explanation:

Implementation:

Time complexity:

Since the merge sort contains recursive calls, it’s hard to determine the running time. Let’s break it down to levels, each level contains chunks of an array, and in the end, all these chunks have the minimum size: 1. I draw a graph to illustrate the process. As we can see from the graph, each level is dividing the array into same size chunks. And at each level, the total cost is a constant number: n. Now the problems become into how many levels it has in order to get size 1 chunks? By calculation, there are total log2(n) numbers of levels. And the time complexity is O(nlog(n)).

Quicksort

Quicksort is my personal favorite sorting algorithm. It’s easy to understand and to implement. Its average time complexity is O(nlog(n)) , same with merge sort. Similar to merge sort, it also uses the divide-and-conquer recursive algorithm. It has three steps to sort an array:

  • Select an element in the array as a ‘pivot’, then put this pivot to its correct position, so that all the elements in the left side of the pivot are smaller than it, and all the elements in the right side of the pivot are greater than it
  • Quicksort all the elements in the left side of the pivot
  • Quicksort all the elements in the right side of the pivot

Implementation:

Time complexity:

Running time for quicksort is depends on the input array. For the average and best case when the input array is nearly sorted, the complexity is nearly O(nlog(n)) . However, when dealing with the worst-case(the input array is in descending order), the running time is O(n²) .

Conclusion

There’s never a best or worst algorithm in sorting. When choosing a sorting algorithm, we always need to consider which is the best in a certain scenario. For example, if an array is nearly sorted, quicksort is the best solution. For more information, here’s the link with detailed time and space complexity for all sorting algorithms. Hope this post can help!

on the way to become a programmer