Why are data structures and algorithms important?

Tram Ho

What is the solution?

The best piece of code is not the shortest, fewest lines of code, but the highest performing code. In a university class there was a small problem presented, Mr. A’s code was very fast, very short and finished first, after a while Mr. B finished and the code was longer – neat, of course. But then look at the results of the two men: on average, all test cases Mr. A takes 1.25s, Mr. B takes 0.8s, the memory Mr. B needs is also less than Mr. A’s, so we should comment Which of your codes is better? Mr. B, of course.

What is the problem? code is not finished running, we have to know how to optimize them. Maybe here Mr. A knows how to solve problems, but Mr. B knows how to optimize performance for them. And Mr. B is definitely better at one thing – algorithms.

Algorithm – Algorithm – roughly translated as a set of clearly defined step-by-step instructions for processing data. It’s a bit vague, but simply understand that the calculations are sequenced, logical, step-by-step very clear to process data and do not depend on any language.

What is data structure?

Data structure is a way of storing and organizing data so that they are used efficiently.

Choosing the type of data structure will depend on each problem and each situation. For example, in a problem that needs to store a finite and available amount of data, we should use Array instead of other types of dynamic memory structures like Linked-list, because Array will need less memory and In this problem we only need to read the data, while the complexity of reading the data of Array is only O(1).

In short: Our code is always related to data structures and algorithms. The data structure is to store the data and the algorithm is the way to process the problem from that data pile. And every project we work on must choose the most suitable types for the program to run efficiently, optimizing their performance will make it easy to extend and maintain our code later.


Thank you for taking a few minutes to read, I will do each type of algorithm and data structure in this series.

Share the news now

Source : Viblo