Why Learn Data Structures and Algorithms at All?

One question often asked by programming noobs is "why do I have to learn data structures and algorithms? I already have a ton of better stuff in the language itself. Array can be used to implement any sort of DS. Sort() build into the language is better than any sort I can implement. So why do we need to learn those at all?" 

If you are ONLY interested in doing web development forever, then no, you don't need data structure and algorithms. Webdev, however, is a bit of special case, as you are mainly assembling bits and pieces of other libraries and modules and such into a coherent whole, serve it up in HTML, and style it with CSS. Get some data from user, send it to server, get some data back, present it to the user. You rarely have to crunch large datasets by yourself as a webdev. 

However, if you deal with more complex software engineering, then you need some fundamentals, such as data structures and algorithms, to understand and solve even MORE complex problems. It's about having more tools in your toolbox for problem solving. 

Think of it this way, using the toolbox as a metaphor. If you need to take a bit off the wood to make it just a bit narrower, but all you have is a flat screwdriver, you can get the job done EVENTUALLY. But the job would be much easier with a sander, a chisel with the hammer, or any one of the more specialized tools to remove a very precise bit off a piece of wood... instead of a screwdriver. 

But you cannot wish for a sander or a chisel if you don't know what they are.  

It is the same with data structures and algorithms. You have to know that they exist (and when to use them) in order to use them. And you need to know the basic ones to understand the advanced ones. 

Let's try an example. 

Let's say, I need you to sort this 100000 item list. Each item in the list can go from 1 to 250. And I need it this program to run "as fast as possible".  Read in the items, sort it, print it out in order. 

You could simply call your language's "sort"... if you can declare an array that cotnains 100000 items. And sort will be quite slow. And even the best implementation can only give you O(N * log(N))  AND you are using a lot of memory. 

What if I tell you that it's not good enough? What would you do? 

(Spoiler: what if I tell you that you can do it in O(N)? )  

The answer is "counting sort", a subtype of radix sort. 

You declare an array from 1 to 250 and set everything to zero.. You load the array by incrementing the count for each number encountered. Then you simply iterate through the array, output each number however many times as needed. Done. You traverse only ONCE through the list. So it is only O(N). 

But you can't use this algorithm if you don't know about it. And to understand it, you need to understand all the other types of sort first. And understand that this type of sort is very special purpose, and only works in limited situations (such as low number of unique values). 

So yes, you do need to know data structures and algorithms, and understand their strengths and weaknesses and applications. You don't need to know how to implement them (you can look that up when you need to use them). You just need to know they are there for when you do need them. 


Comments

Popular posts from this blog

Yet another take on recursion

Problem Solving for Programmers: a Neglected Topic?

How to Solve a Problem, with Examples