Hola readers! Many beginners think that Data Structure and Algorithms are the same but not. Data Structures is a method to store the data and the Algorithm is a step by step instruction to solve a given problem. They are not programming languages and even it had no common structures like loops and conditional statements such as if/else. It doesn’t make for a particular programming language, we should use it with any programming languages by using conditional statements. In this article, we going through the basics of Data Structures and Algorithm.
Bad Programmers worry about the code, Good Programmers worry about data structures and their relationships.Linus Torvalds
Data Structures are the methods or instructions to store the data in memory. I day to day life, we used many data but when had thought that how the data are stored and how.
If you answered No! Then this is the right place. First of all the Data Structures are of two types
An array is a common data structure used to store data elements inhomogeneous order. They are found in most Programming Languages prebuilt. They are usually created using square brackets ‘[ ]’ like this. The size of an Array is predefined and can’t be dynamic. Array elements can be accessed using an index number. Index numbers begin at 0.
The syntax for Array :
Int x = [ 5 ] ;
- X is the name of the variable.
- [ ] represents the array.
- Finally 5. The maximum size of the Array. This means that the Array can contain only 5 elements of type int (integer).
A Stack is similar to an Array, but it follows the principle called LIFO (Last In First Out). This means that data can be manipulated only on one end. Insertion and Deletion of elements in Stacks take place at one end. For example, if we insert two elements A and B, the element inserted last (B) will be above the one inserted first (A). We can’t access element A first without accessing element B. There are two functions PUSH and POP. Push is used to insert an element and Pop is used to remove an element from the Stack.
A Queue is similar to Stack but follows FIFO(First In First Out) principle which means data can be inserted at one end (REAR) and deleted at another end (FRONT). For example, suppose that in a movie theatre people are standing in a queue to get tickets. If one person gets the ticket, the queue moves forward. And if a new person comes and stands in the Queue, the size of the Queue increases (considering you don’t break the Queue :P). The same process is seen in a Queue data structure.
Linked lists are the collection of connected nodes that contain a data attribute and a next attribute. The data attribute is used to store the actual data whereas the link attribute stores the memory address of the next node. The first node is considered as the Head which contains the only address of the first node and last node considered as Tail which contains one part as data and another as Null. While accessing a Linked list if it meets Null it is considered as the end of the list.
An Algorithm is a step-by-step procedure, which defines a set of instructions to be executed in a certain order to get the desired output. Algorithms are generally created independent of underlying languages, i.e. an algorithm can be implemented in more than one programming language.
From the data structure point of view, the following are some important categories of algorithms
Search − Algorithm to search a particular item in a data structure.
Sort − Algorithm to sort data in a certain order.
Insert − Algorithm to insert an item in a data structure.
Update − Algorithm to update an existing item in a data structure.
Delete − Algorithm to delete an existing item from a data structure.
How to Write an Algorithm?
There are no well-defined standards for writing algorithms. Rather, it is a problem and a resource-dependent issue. Algorithms are never written to support a particular programming code.
As we know that all programming languages share basic code constructs like loops (do, for, while), flow-control (if-else), etc. These common constructs can be used to write an algorithm.
We write algorithms in a step-by-step manner, but it is not always the case. Algorithm writing is a process and is executed after the problem domain is well-defined. That is, we should know the problem domain, for which we are designing a solution.
Let’s try to learn an algorithm-writing by using an example.
Problem − Design an algorithm to add two numbers and display the result.
Step 1 − START
Step 2 − declare three integers a, b & c
Step 3 − define values of a & b
Step 4 − add values of a & b
Step 5 − store output of step 4 to c
Step 6 − print c
Step 7 − STOP
Algorithms tell the programmers how to code the program. Alternatively, the algorithm can be written as −
Step 1 − START ADD
Step 2 − get values of a & b
Step 3 − c ← a + b
Step 4 − display c
Step 5 − STOP
In the design and analysis of algorithms, usually, the second method is used to describe an algorithm. It makes it easy for the analyst to analyze the algorithm ignoring all unwanted definitions. He can observe what operations are being used and how the process is flowing.
Writing step numbers is optional.
We design an algorithm to get a solution for a given problem. A problem can be solved in more than one way.
Hence, many algorithms can be derived for a given problem. The next step is to analyze those proposed solution algorithms and implement the best suitable solution.
These might sound hard to you, but believe me. Once you get the hand of it and understand all of this fits into your code – You will be what we call an Awesome Programmer! And it’s fun too (believe it or not :D)