Why learn Data Structures and Algorithms ...

Why learn Data Structures and Algorithms?

May 13, 2024

Data Structures and Algorithms (DSA) are very important concepts in computer programming. They're everywhere from your smartwatch to your car.

There's a growing demand for programmers who are proficient at DSA, as they can write more efficient and scalable code. And so, learning DSA is crucial for any programmer who wants to get a good job out there with a good salary.

What are algorithms?

An algorithm is basically a solution to a problem. It describes the steps that you have to take to solve a problem or complete a task.

Let's take an example from real life and say that you want to make a PB&J sandwich for you. That's now a problem that you need to solve.

The steps to solve this problem are:

  1. Get two slices of bread, peanut butter, jelly and a knife;

  2. Scoop out some peanut butter and spread it over one slice of bread;

  3. Scoop out some jelly and spread it over the other slice of bread;

  4. Smash the two slices of bread together, with the sides with peanut butter and jelly touching each other.

Now you have a PB&J sandwich! Of course, programming algorithms are a little bit more complex.

For example, let's write an algorithm to multiply a number m by another number n:

  1. Initialize variables m, n and result;

  2. Assign two numbers to m and n and zero to result;

  3. For each value v in range 1 to n (inclusive), add m to result;

  4. result has the product of multiplying m by n.

Now, there's an important difference between algorithms and code. Code is text compliant with the syntax of a certain programming language, while algorithm is what we've been talking about so far.

For example, the following code is written in the JavaScript programming language and represents the above algorithm:

What are Data Structures?

Data structures are simply ways of organizing information in the computer's memory.

Once we have our data organized we can write algorithms to solve problems involving that data.

There are a bunch of ways in which data can be organized and choosing the right structure will make all the difference for the system you're working with.

DSA Importance

Data structures and algorithms work together to build any software system in the world. By knowing about the different data structures that can be used, you can choose optimal solutions to real-world problems that you're facing.

Efficiency and scalability are two things that should always be prioritized in any piece of software, because programs that can handle large amounts of data faster bring more profit.

In any job interview, there'll be questions about DSA and, if you want to get the job, you'd better know about it, because companies want programmers who know how to write fast and scalable code that will solve their problems efficiently and satisfy their customers.

Moreover, DSA is something that can be applied in any programming language and in various different fields like finances, physics, chemistry, and so on. You can use it to solve pretty much any problem that comes up in the real world.

What is efficiency?

Efficiency means the quality of being efficient. Being efficient means working productively without wasting resources. For a computer algorithm, it means that it accomplishes its task with minimum use of computational resources such as time and memory.

You should always try to limit the amount of memory that your algorithms use, because it isn't always abundant. Memory is crucial for any program to run and the less you use the more efficient your code is.

And, of course, the less amount of time your algorithm takes to solve a problem, the faster it will be. For computer algorithms, the fewer steps they need to complete their job, the less time they'll use, and the faster they'll be. Thus, reducing the amount of steps required to finish the job is essential for optimizing the performance of your programs.

What is scalability?

Scalability means that your program has the ability to handle larger amounts of data efficiently.

For example, suppose a website initially has to store and process data of 50 users. That's not much; a good computer can handle it in a matter of milliseconds.

Now, suppose the website becomes more famous and has 1000 users, then 100,000, then 10,000,000 and so on. Scalable algorithms and data structures will be able to handle that growth without any problems, while bad algorithms and data structures will make the website feel slow and clumsy.

Why are efficiency and scalability important?

If you're like me, you might have wondered, "But why do I have to care about efficiency and scalability? Don't I have to just solve the problem and that's it? Doesn't the computer hardware determine how quickly code will execute?" Well, for the importance of efficiency and scalability to really sink in, you need to experience the differences between poorly-written code and efficient code firsthand.

In my case, LeetCode helped me understand it very well. LeetCode is a website which proposes a bunch of problems from real interviews in lots of companies. I solved some problems there and realized how much my coding skills sucked at efficiency and scalability.

My solutions often took long to complete because they used too much memory or because they weren't good enough. And then I saw other people's code and how efficient they were using specific data structures and algorithms. They knew how to solve problems optimally using their DSA knowledge and I realized I had to learn and use it as well.

And so I started learning DSA and I am still learning.

I'd recommend you to go to LeetCode or any other programming problems websites and try to solve their problems. If you have no DSA knowledge, you'll realize why you need to learn it.

And don't feel bad for writing inefficient code. That's an opportunity for improvement. If you're not able to solve a problem efficiently by yourself, take a look at other people's code and understand their solutions. You'll be amazed by how much you're going to improve. Don't be afraid. Failure is the way forward.

Conclusion

If you don't know algorithms well, you won't be able to know where and if you can improve the code you are writing right now. In order to optimize your code, you should know algorithms and data structures in advance and apply your knowledge wherever possible.

Keep in mind that a piece of software consists of many algorithms and optimizing any of them can enhance the program performance. But also be aware that optimizing algorithms is not the only way of improving software. There are other techniques utilized to make better software such as distributed computing which allows a program to be run by different machines simultaneously.

Vous aimez cette publication ?

Achetez un café à Rafael Maia

Plus de Rafael Maia