Why We Need to Learn Data Structure and Algorithm
If you are new to programming and have just completed a beginner course in any programming language, the next thing anyone would suggest you learn is Data Structure and Algorithm (DSA). So, what is DSA, and why do we learn it?
Data Structure
A data structure is a structured set of data that allows you to store, access, change, or remove data. The way you perform these tasks depends on how you have structured the data in your data structure. Some popular data structures are arrays, linked lists, queues, stacks, and trees.
Let’s understand what data structures are by comparing two different data structures: arrays and linked lists.
Array
An array is a continuous sequential structure of data. An array has a specific name and a specific number of cells where the data can be stored. Each cell of the array has an index number. So, if you want to set, change, or access the value of that cell in the array, you can use the name of the array along with the index of that specific cell. Thus, an array makes it easier to store related data like the marks obtained by students of a section or the list of friends that you have on your Facebook profile.
But there is a drawback of arrays. When you declare an array, your computer allocates some sequential memory cells for that array. So, after declaration, you cannot change the size of the array. Also, you cannot insert a new cell between two other cells of the array.
Linked List
The drawbacks that arise from an array can be solved by using a linked list. A linked list is also like an array that has a name and cells which can be used to store data. But the cells of a linked list are not sequential memory cells; rather, in a linked list, each cell of the list works like a node which contains two pieces of information: the value of the cell and the ‘address of the next cell’ of the list. So, if you want to insert a new cell in a specific place of the list, you can just set the ‘address of the next cell’ of the preceding cell to your new cell, and you should also store the address of the next cell in your new cell. Likewise, you can also delete cells from a list by manipulating cell addresses. But a linked list also has its own drawbacks. A linked list consumes more memory as each cell stores both a value and an address. A linked list is also difficult to traverse.
So, you see, learning data structures can help you choose the correct data structure for the real-life problem that you want to solve.
Algorithm
Now, let’s talk about algorithms. Algorithms are everywhere. Every program that you write is a set of instructions that you give to your computer, and the step-by-step instructions that your computer follows are called an algorithm. Then what does it actually mean to ‘learn algorithms’? In programming, there are some similar types of problems that we often need to solve. For example, searching for something from a data structure (binary search, linear search, DFS, BFS), sorting data in a data structure (quick sort, merge sort, bubble sort, counting sort), or finding the shortest path to reach a node (Kruskal’s algorithm, Dijkstra’s algorithm, Bellman-Ford algorithm). To solve these problems, there are already some popular algorithms in use. Each algorithm has its own time and space complexity, which indicates how efficient the algorithm is. When we “learn algorithms,” we actually learn how to implement the popular algorithms and how efficient they are.
Another reason why you should learn data structures and algorithms is that they help you get placed. Meaning, in most programming job interviews, you’ll be asked about these popular data structures and algorithms. So, having a good foundation in them can definitely prepare you well for the competitive job industry.