Wednesday, 18 November 2015

Motivation to learn about algorithms



If you have taken a look at my previous article, you must have observed that, I have taken some examples of the IT giants like Google, Microsoft, Facebook. So, you might be wondering, all these are related to the giants and they have found it very much useful to devise a new algorithm to solve the problem in hand, I don’t deal with such a huge data in my programs, so probably I don’t need it. Better have a coffee myself instead of reading this article.

Exactly the same I thought when I was pursuing my graduation and ignored this vital part of science. Honestly confessing, until today, I never bothered about this topic and whenever possible I had avoided the discuusion. But today, I found out how useful and interesting this field can be, I also could recall a scenario from my past IT experience. Somewhere there was a code, which was trying to sort an array of records and I remember, it was using a poor algorithm.

At the time of writing the code, the developer might not have thought of a scenario when the size of the incoming array would have crossed a limit this algorithm stands good for. So, when I was working on that piece of code, I faced the issue and the system was taking too long to respond.

No credit for me, I have not fixed the code.
:)

Credit goes to the senior team member who just took a look on the piece of code, removed the code and simply added Arrays.sort() method. I was astonished to see the performance improvement after that but could not figure out what he did on that piece of code.
Well almost after two years of that incident, I have found out the reason today. Basically, Java internally also uses algorithm to solve the case of sorting.

So, what’s the big deal, your code also had an algorithm?
Well, simply Java uses a better algorithm to solve the case of sorting.

So, how do you know about that now after such a long time?
Well, I was writing code for some algorithms and was astonished see the performance boost a good algorithm can provide you. Then I suddenly recalled the event and went to Java Docs to match my answer and my initial guess was correct that Java uses a better algorithm to solve problems.


So, I will be using the standard API, no problem. No need to study Algorithms…
Yes, I encourage you to use the standard API whenever possible. But, in some cases, you may also be stuck with a situation that I faced a long ago and you may not have a senior like you who can fix it in a minute.
To implement things in a better way, you need to figure out the existing worse way, right?

Hmm, makes sense.

Good that you are also in the same page I am into. Well, if you still have a doubt, just check out the following table which is a practical response I have got running two different algorithms on the same system, on the same data set.
Data Size
Run time for Algorithm 1 (in Milliseconds)
Run time for Algorithm 2 (in Milliseconds)
250
0 *
1
500
1
2
1000
0 *
4
1500
1
5
5000
2
26
10000
2
85
50000
11
2381
100000
12
7894
1000000
59
828873
50000000
8075
Running from November 18, 2015. I stopped the execution on November 27, 2015.

* This is inaccurate. It might have completed within a fraction of milliseconds, but definitely it has taken some time to complete but that is really negligible and tends to 0.

So, we can see that as the data size increases, the difference also increases significantly.

Actually, I earlier was stuck in the data range of 50,000. When the developer has wrote this code, he might not have thought of this scenario to be present and a difference between 2 milliseconds and 85 milliseconds is really not a very much notable difference but the difference between 11 milliseconds and 2381 milliseconds is quite observable.
So, you can get some idea why it is better to have knowledge about algorithms.


For your use, I have added all these code in my Git account, https://github.com/Palash90/algorithms.
You can check out the code to run it in your local system and  can play with it using different number sets.


Hope you enjoyed this article…

 Prev   Next
Palash Kanti Kundu

No comments:

Post a Comment