Understanding DATA STRUCTURE & ALGORITHM.
What are data structures & algorithms?
The term data structure is used to describe how the data is stored and the algorithm is used to describe how data is pressed, both data and algorithm are irrelated to each other, whereas data structure is nothing but a representation of the logical relationship between each element of the data. In other words, a data structure helps you organize all the data items that consider the way data elements are stored and their relationship to one another.
The algorithm is a finite set of instructions, which is used to accomplish a particular task. Each of these has a clear meaning and can be performed with a finite number of efforts over a finite length of time. The best thing about the algorithm is that it doesn’t matter what input values may be, the algorithm terminates after executing a finite number of instructions.
The data structure can be represented as:
Algorithm + Data structure= program
What is ALGORITHM?
An algorithm is a step-by-step process, that is to be followed when writing an algorithm to define a set of instructions in a certain order to get the output, The algorithm can be written in more than one programming language. According to the data structure, some of the important categories of the algorithm are,
- Insert- an element is inserted in the data structure
- Sort- element is sorted (arranged) in a certain order in the data structure
- Search- an element is searched in the data structure
- Delete- an element is deleted in the data structure.
- Update- The element is updated in the data structure.
How to write an algorithm?
Well, there is no well-defined structure for writing an algorithm. Algorithms are never written to be code for any particular programming language. We may know that all programming language(s) share some of the basic code constructures, like flow-control (if, else); loops (for, do, while), etc. These common constructures can help you in writing an algorithm.
We know that we write an algorithm in step-by-step order but, it is not in all cases. Writing an algorithm is the process of execution after the problem domain is well defined. Before you begin writing an algorithm you must know the main problem domain, for which you are about to design the solution.
Some of the examples are given to understand more about how to write an algorithm:
EXAMPLE 1
Write an algorithm to add 2 numbers,
STEP 1 - start
STEP 2 - Read a, b
STEP 3 - Sum a+b
STEP 4 - print
STEP 5 - stop
EXAMPLE 2
Check the eligibility of getting a driving license!
STEP 1 - start
STEP 2 - read age
STEP 3 - if age >= 18 then go to step_4 else step_5
STEP 4 - writ "The person is eligible to get a driving license"
STEP 5 - writ "The person is not eligible to get a driving licence"
STEP 6 - Stop
It is not necessarily that one must write an algorithm in the same way as mentioned above, the algorithm tells the programmer how to code to the given problem domain effectively. Moreover, we design algorithms to get the solution to a given problem. The problem can be solved in more than one way.
PERFORMANCE OF ALGORITHM
The efficiency of the algorithm is mostly dependent on
- Time complexity
- Space complexity
TIME COMPLEXITY:
The amount of time required for the algorithm to complete its execution is the time complexity. An algorithm is said to be efficient if it takes a minimum(reasonable) amount of time to complete the execution.
SPACE COMPLEXITY:
It’s the space occupied by the algorithm is known as space complexity. An algorithm is said to be efficient if it takes less space and requires less amount of time to complete its execution.
ASYMPTOTIC NOTATIONS
Asymptotic analysis is the algorithm that refers to defining the mathematical bounding/framing of its run time. Asymptotic helps you to find the best, average, worst, case scenario of an algorithm. Asymptotic analysis refers to the computing time of any operation in a mathematical unit of computation.
For example, the running time of one operation is computed as f(n), and another operation is taken to compute (n²). This means that the first operation running time will increase linearly with the increase in n and the running time of the second operation will increase exponentially when n is increased.
The time required to compute by the algorithm falls under three (3) categories:
- O NOTATION (Best case) — minimum time required for the execution of the program
- Ω NOTATION (Average case) — average time required for the execution of the program
- Θ NOTATION (Worst case) — maximum time required for the execution of the program.
Why do we need to study DSA?
In this, we will learn why one should learn Data structure and algorithm (DSA), by learning Data structure and algorithm (DSA) you can understand the concept of the data and how to store the data in a way that can be found easily so that the user doesn’t face any problem finding out the data. As the applications are getting bigger and more complex and increasing in data, three (3) common problems may occur in the application.
- Data Search
- Processor speed
- Multiple requests
To overcome this problem, we use data structures in this Data structure and algorithm (DSA) we will see the time and space complexity of the code and the best possible way to store the data. There are a few ways by which you can store the data,
- Array
- Stacks and queues
- Linked list
- Trees
- Graphs
- Selection and sorting.
ARRAY:
An array is a type of data structure that stores homogeneous elements in a memory location which are contiguous. The main concept of an array is to store multiple data in one location where each data is stored together to save memory and time to search the data. The data (or) objects which are of the same type are stored in the same order in the array. The size of the array needs to be defined before the data is stored in the array. Any data stored in the array can be accessed and modified easily. The data which are stored in the array are given a subject (or) index to identify the location of the data stored.
EXAMPLE:
let’s, store the marks of 20 students, then the size of an array has to be mentioned as 20. The student’s marks can then be stored in the created array without creating separate variables for marks for every student. A Simple traversal of an array could be done to access the elements.
STACK
A stack is a linear data structure, where elements are inserted and deleted only from one end, that is the top of the stack. Stack follows the principle of LIFO (last in first out). The elements which are last in the stack will be deleted first as the elements can only be added from the top. So, the element which is added last will be the one that needs to be removed first.
The best example to understand the stack properties would be arranging the books according to their Roll number where the first roll number’s book goes at the bottom and the book of the last roll number is placed at the top. And if the books need to be distributed to students again the first book goes to the last roll number person. the items can only be added or removed from the top that is the last item to be added to a stack will be removed first from the stack.