Sorting Algorithms

Sorting algorithms

Sorting Algorithms (Brief intro)

In this article, I will describe the most popular sorting algorithms and implement them from their definitions. The little background story of my burgeoning interest in data structures and algorithms generally lies in the fact that I like hacking. However my decision to formally acquaint myself with the subject was precipitated by a potential job interview and I have not looked back since. Now lets get to it.

Types of sort algorithms

  • In place algorithms: these algorithms don't need additional memory.
  • Stable algorithms: these algorithms don't change the original order of things after sorting.

Bogo sort

This algorithm is basically not your go to algorithm except you have a super computer. It basically gets all the possible permutations of the list to be sorted and then checks if they are sorted. The implementation can be seen below.

Time complexity of this algorithm is basically:

This algorithm is not a stable algorithm but it is an inplace algorithm.

Bubble sort

For some weird reason I like this algorithm. It basically bubbles the largest value to the end of the list. It does this by binary comparison starting with the first and second element. If the first is greater than the second you move the first to the second position and then proceed to compare the second and the third till you get to the end of the list and start all over. This is a stable and an inplace algorithm which means that if there is sorted subsection, the algorithm will leave them that way. The implementation can be seen below.

From the implementation, this algorithm has an complexity because you have to iterate twice over a list of size n.

Selection sort

This algorithm finds the largest(smallest) item and moves it over to the first index(last unsorted index) till the whole list is sorted. It is a stable and an inplace algorithm. The implementation can be seen below.

The time complexity is .

Insertion sort

This is very similar to selection sort with the exception that it makes many insertions and inserts can be costly depending on the type of memory we are looking at. It is an inplace and stable algorithm. Basically you compare an element in the list with the preceding ones and move it as required. In selection sort you had to check the elements in front for the smallest one but here you look at the preceding ones. The implementation can be seen below.

The time complexity is .

Quick sort

This is one of the most popular sort algorithms. And arguably one of the fastest. It is a divide and conquer algorithm. In the first iteration you divide the list into two and move elements bigger than the middle element to right and does smaller to the left. You repeat the process recursively till the list is sorted. This algorithm is an in place algorithm but not stable.

This algorithm has an time complexity.

Merge sort

This algorithm is another divide and conquer algorithm. It breaks the list down to a single unit and begins joining them in sorted order by making comparisms till the whole list is sorted. It is a stable algorithm but not in place. The implementation can be seen below.

This algorithm has an time complexity.

Competition time

I executed the code below

Result:

Bogo sort - 1.2 secs. Don't use this for a large list. I tried it and it took forever.

Bubble sort - 2.0 secs.

Selection sort - 1.8 secs. Has a reputation of being efficient for small lists like the one used above.

Insertion sort - 1.9 secs. Not far away from selection sort.

Quick sort - 3.5 secs.

Merge sort - 3.19 secs.

Bonus

Sorting an object. I implemented an obect to test the sorting algorithms.

Viola!!! Your object is sorted.

Thanks for reading.

Comments

Popular posts from this blog

How we processed data of over 100gb with 16gb of ram

AWS networking basics with terraform and a tiny bit of microservices

Python meets linear algebra