Why is sorting a huge deal in computer programming, one may ask. Let’s make an analogy that would make much sense:
You enter your room, and it’s a mess. Like a complete mess: clothes on the desk, laptop in the closet, mouse in your backpack which is under the bed, your bedsheets are in a box and you don’t even know why and etc. Oh and wow, in 40 minutes your date will come over, so you have to clean up your room completely and at the same time, you don’t want to stink. So you have to organize your cleanup in a such way so you would be able to clean your room completely and be able to take a shower. Here comes different approaches which would result in different amount of time that would need to clean up your room. So basically, you need to finish within 40 minutes and still save enough time to be able to take a shower. You think of different ways of cleaning up your room: first collect all clothes from everywhere and fold them vs break your room into squares and clean every single square one by one… (I do not even want to ask you to assume that your room is 100 times bigger than usual and the mess is also multiplied. I just do not want you to feel bad)
The same thing is with the sorting algorithms. They all work in different ways and achieve different results in time consumption.
There are different sorting algorithms (which include insertion, merge, bubble, quick and etc.) each of them take different amount of time because of the algorithm’s way of working. If there are a lot of loops,m that would keep checking every single item in the list, the time basically would get bigger (really simplified version of explanation of complexity).
So, what is big-Oh. Big-O basically is a way of comparing algorithms in their way of working in time consumption. Saying that an algorithm is in O(n^2), means that depended on the input n, the time that it will take to make the algorithm work will grow exponentially, which is undesirable. On the other hand, saying that an algorithm is in O(logn) means the time will grow only logarithmically, which is pretty acceptable.
The main reason of simplifying algorithms in terms of time, is to find another algorithm with would give the same result but in a reduced time.
I think it is almost the best way to explain complexities and sorts and unfortunately it is my last sLOG post. To be honest, i was a bit skeptic about this blog, but it was pretty fun writing stuff and explaining all what i learned to myself again and again and i’m hoping i will be able to make use of this knowledge sometime in the near future.