Different sorting algorithms for programming, and why it matters which one you choose

There are a number of sorting algorithms you can use in your programs. How can you know which one is the best? There is really no clear answer to be honest. The bottom line is that you have to take into account what your program will be sorting. Will the data be completely mixed and random? Will the data be nearly sorted? Will you be sorting HUGE data sets, or small data sets? All of these factors plus more will play a role into choosing the most efficient algorithm for your program. Here is a website that compares the run time of 8 different algorithms on 4 different initial conditions. You can watch and see how much time it takes each data set to be sorted and compare them. You can even change the problem size to see the effects of larger and smaller data sets. It is such a good visual representation of how these algorithms work. Here is the link:

https://www.toptal.com/developers/sorting-algorithms

I did a tutorial on bubble sort a while ago. This is a very easy algorithm to learn. I wanted to explain it because my tutorial series has been very beginner-based and as simplified as I could make it. The bubble sort is perfectly fine with small data sets, but when you get to HUGE databases it is one of the absolute worst algorithms you can choose! It would take an eternity compared to some of the other sort algorithms!

I just found a gif someone made of this website!

Image source

Sort:  

If the array / list is close to being sorted already, it looks like the bubble sort is the fastest sort method of the ones listed, probably because (if I recall correctly, I haven't looked at the code for a bubble sort in a long time).

Of course, that's a BIG assumption, although I'm sure there are ways you could rely on that case being true (none that I am aware of though)...

It doesn't seem to do as well if the data is fairly randomized, needs to be reversed or if the data has few items that are the same.

Great introduction, can't wait to see what else you come up with!

Bubble sort is actually very fast compared to most of the algorithms if the data is nearly sorted! Good observation. It is not very likely for the data to be almost sorted, although I'm sure there are cases where this is implemented!

Coin Marketplace

STEEM 0.30
TRX 0.12
JST 0.032
BTC 58557.01
ETH 2992.97
USDT 1.00
SBD 3.74