DSA Basic Notes
DSA Basic Notes
AND
ALGORITHM
BY - HIMANSHU RAWAT
Topics of Lecture 1
● What is Data Structure
● Classification of data structure
● Basic Terminology: Elementary Data Organization
● Operations in Data Structure
● Need of Data Structure
● What is Algorithm
● Examples of Algorithm
● What is Flowchart
● Symbols in FLowchart
● Rules for creating Flowchart
● Examples of Flowchart
● Homework Question
● Motive of DSA (complexities)
What Is Data Structure?
Examples of data structures include arrays, linked lists, stacks, queues, trees, and
graphs.
Classification of Data Structure
There are two types of data structures:
● Primitive data structure
● Non-primitive data structure
Data structures where data elements are not arranged sequentially or linearly
are called non-linear data structures. In a non-linear data structure, single level
is not involved. Therefore, we can’t traverse all the elements in single run only.
Non-linear data structures are not easy to implement in comparison to linear
data structure. It utilizes computer memory efficiently in comparison to a
linear data structure. Its examples are trees and graphs.
Linear Data structures can also be classified as:
In the above figure ( ID, Age, Gender, First, Middle, Last, Street, Area ) are elementary data
items, whereas (Name, Address ) are group data items.
● Data Type:-
Data type is a classification identifying one of various types of data, such as floating-point,
integer, or Boolean, that determines the possible values for that type; the operations that
can be done on values of that type; and the way values of that type can be stored. It is of
two types: Primitive and non-primitive data type. Primitive data type is the basic data
type that is provided by the programming language with built -in support. This data type is
native to the language and is supported by machine directly while non-primitive data type
is derived from primitive data type. For example- array, structure etc.
● Variable:-
It is a symbolic name given to some known or unknown quantity or information, for the
purpose of allowing the name to be used independently of the information it represents.
A variable name in computer source code is usually associated with a data storage
location and thus also its contents and these may change during the course of program
execution.
● Record:-
Collection of related data items is known as record. The elements of records are usually called fields
or members. Records are distinguished from arrays by the fact that their number of fields is typically
fixed, each field has a name, and that each field may have a different type.
● Program:-
A sequence of instructions that a computer can interpret and execute is termed as program.
● Entity:-
An entity is something that has certain attributes or properties which may be assigned some values.
The values themselves may be either numeric or non-numeric. The example of entity with its attribute
and its values are shown in figure.
● Entity Set
● Field
● File
File is a collection of records of the entities in a given entity set. For example, file containing
records of students of a particular class.
● Key
A key is one or more field(s) in a record that take(s) unique values and can be used to
distinguish one record from the others.
Operations in Data Structure
Operations in data structures refer to the fundamental actions or functions that can be performed on
data structures, such as arrays, linked lists, trees, graphs, and more. These operations are essential for
manipulating, retrieving, and managing data efficiently within a data structure. The specific operations
available may vary depending on the type of data structure, but here are some common data structure
operations:
● Creation: The creation operation in data structure is the process of allocating memory for the
data structure and initializing its values. This operation is usually performed when the program
starts running.
● Insertion: Adding a new element or data item into the data structure. The position of insertion
may vary based on the data structure, such as adding an element at the beginning, end, or a
specific location.
● Deletion: Removing an element or data item from the data structure. Like insertion, deletion may
involve specifying the location of the item to be removed.
● Traversal: Visiting all elements of the data structure to perform a specific operation on each item.
Traversal is commonly used in tasks like searching, printing, or modifying elements.
● Search: Finding a specific element within the data structure. The goal is to determine whether a
particular item exists in the structure and, if so, its location or value.
Step 1 : Start.
Step 5 : Exit
2. Find maximum of three given numbers.
Step 1 : Start.
Step 2 : Read a , b & c.
Step 3 : If a>b do
If a>c do
Print a
Else
Print c
Else
If b>c do
Print b
Else
Print c
Step 4 : Exit
What is a Flowchart?
Rule 3: All symbols in the flowchart must be connected with an arrow line.
Rule 4: The decision symbol in the flowchart is associated with the arrow line.
1. Find the sum of any two given number Start
Step 1 : Start.
Input num1 and
Step 2 : Input two numbers num1 and num2
num2.
Step 5 : Exit
Print Sum
Exit
2. Find maximum of three given numbers. Start
Step 1 : Start.
Step 2 : Read a , b & c. Read a,b&c
Step 3 : If a>b do
If a>c do
Print a
Else YES NO
Is
Print c
a>b?
Else
If b>c do
Print b NO NO
Is Is
Else a>c? b>c?
Print c
YES YES
Step 4 : Exit
Exit
Question for homework :-
"Complexity" typically refers to two main aspects: time complexity and space
complexity. These complexities help us analyze and understand how
efficiently an algorithm or data structure performs in terms of execution time
and memory usage.
Brain Teaser!
How we get to know 9 min is past?
7 min 4 min
Topics of Lecture 2
❖ Types of Data Structures
❖ Advantages of data structure
❖ Selecting a Data Structure
❖ Data Types and its types
❖ Characteristics of algorithm
❖ Analysis of algorithm
❖ Types of analysis of algorithm
❖ Space Complexity
❖ Time Complexity
❖ Time-Space trade off
❖ What is Asymptotic?
❖ Types of Asymptotic Notation
❖ Big O notation & its example
❖ Big omega notation & its example
❖ Theta notation & its example
Types of Data Structures:-
The collections of data you work with in a program have some kind of structure or organization.
No matter how complex your data structures are, they can be broken down into two fundamental types:
1. Contiguous
2. Non-Contiguous.
In contiguous structures, terms of data are kept together in memory (either RAM or in a file). An
array is an example of a contiguous structure. Since each element in the array is located next to
one or two other elements. In contrast, items in a non-contiguous structure and scattered in memory, but
we linked to each other in some way. A linked list is an example of a non-contiguous data structure. Here,
the nodes of the list are linked together using pointers stored in each node.
Advantages of Data structures
● Reusability: The data structure provides reusability means that multiple client
programs can use the data structure.
● Abstraction: The data structure specified by an ADT also provides the level of
abstraction. The client cannot see the internal working of the data structure, so it
does not have to worry about the implementation part. The client can only see the
interface.
Selecting a Data Structure
➢ Analyze the problem to determine the resource constraints a solution must meet
➢ Determine basic operation that must be supported. Quantify resource constraint
for each operation
➢ Select the data structure that best meets these requirements.
Each data structure has cost and benefits. Rarely is one data structure better than other
in all situations. A data structure require :
● Input specified - The input is the data to be transformed during the computation to produce the
output.An algorithm should have 0 or more well-defined inputs.Input precision requires that you
know what kind of data, how much and what form the data should be.
● Output specified - The output is the data resulting from the computation (your intended result).
An algorithm should have 1 or more well-defined outputs, and should match the desired
output.Output precision also requires that you know what kind of data, how much and what form
the output should be (or even if there will be any output at all!).
● Definiteness - Algorithms must specify every step and the order the steps must be taken in the
process.Definiteness means specifying the sequence of operations for turning input into output.
Algorithm should be clear and unambiguous.Details of each step must be also be spelled out
(including how to handle errors).It should contain everything quantitative and not qualitative.
● Effectiveness - For an algorithm to be effective, it means that all those steps that are required
to get to output must be feasible with the available resources.It should not contain any
unnecessary and redundant steps which could make an algorithm ineffective.
● Finiteness - The algorithm must stop, eventually.Stopping may mean that you get the
expected output OR you get a response that no solution is possible. Algorithms must terminate
after a finite number of steps.An algorithm should not be infinite and always terminate after
definite number of steps.
There is no point in developing an algorithm which is infinite as it will be useless for us.
Thus,these are the characteristics that an algorithm should have for its fruitfulness.
Analysis of algorithm
After designing an algorithm, it has to be checked and its correctness needs to be predicted. This
is done by analysing the Algorithm.
An Algorithm analysis measures the efficiency of the algorithm. The efficiency of an algorithm
can be checked by
➔ Correctness of an Algorithm
➔ Implementation of an Algorithm
➔ Simplicity of an Algorithm
➔ Execution time and Memory requirement of an Algorithm.
Types of Analysis:-
● Worst Case Analysis (Running Time)
● Average Case Analysis (Running Time)
● Best case Analysis (Running time)
● Space Complexity
● Time Complexity
1. Space Complexity :-
Analysis of space complexity of an Algorithm or program is the amount of memory. It needs to run to completion.
● Instruction Space - Space needed to store the executable version of the program and it is fixed.
● Data Space - Space needed to store all constants, variables, Values and has further two components.
B. Space needed by fixed sized structural variables, such as array and structures.
● Environment Stack Space - This space is needed to store the information to resume the suspended junctions.
Each time a junction is invoked the following data is saved on the environment stack.
a. Return Address - From where it has to be resume after completion of the called junction.
b. values of all lead variables and the values of formal parameters in the junction being invoked.
2. Time Complexity-
The time complexity of an Algorithm or a program is the amount of time it needs to run to completion. The
exact time will depend on the implementation of the algorithm, Programming language, optimising the
capabilities of the compiler used, CPU speed, other hardware characteristics / specifications and so on.
The time complexity also depends on the amount of data inputted to an Algorithm. But we can calculate the
order of Magnitude for the time required.
It means a line that continually approaches a given curve but does not meet it at any finite distance.
Example:-
We say that f(n) is Big-O of g(n), written as f(n) = O(g(n)), iff there are
positive constants c and n0 such that
1. Problem Definition
2. Program Designing
3. Algorithm Development and flow charting
4. Program Coding
5. Debugging and compilation
6. Program Testing
7. Implementation and Documentation
● Problem Definition
This is the very first step to be followed during development of a program. Before writing the program
one must have a proper understanding of the problem.
He must know what are the output requirement of the problems and what type of input must be
followed (supplied).
● Program Designing
In this phase its solution procedure is designed and all alternatives are consulted. All types of input,
output format.
● Program Coding
Normally the language for coding the program for any project is decided in advance.
When the algorithm is developed and tested it needs implementation using that language. This is
known as program coding.
● Debugging and Compilation
Once the program are ready they must be checked for any types of errors.
➔ Syntax error
➔ Logical error
➔ Execution error
Isolation of errors & their removal is known as debugging. Compilation is helpful in removing syntax
error.
● Program Testing
After compilation there may be logical errors, which may lead to undesired output.
For testing a program some sample data are taken and their output is calculated manually.
Then the some data are given as input to program. The output is compared to the manual output. If result
is ok, there are no logical error in the program. After program testing is completed successfully you move
to next step.
● Implementation and Documentation
This phase consist 3 major jobs.
➢ Installation
➢ Maintenance
➢ Documentation
● Installation - Means to load the program at user site & train him to use the software.
● Maintenance - After installation the software is evaluated regularly to confirm its utility
with time. Also if need maintenance i.e., there may be some errors discovered by user
during its day to day use which need removal and also sometimes there may be slight
change in user requirement which can be made upto date.
A structured program can be completely developed using four basic control structures.
1. Sequential
2. Conditional
3. Repetition
4. Procedures
● Sequential : This structure composed of statements executed one after another. There is only one
entry point and one exit point.
Bottom-up programming is just the opposite of top-down programming. Here, the program modules
are more general and reusable than top-down programming. Bottom-up model is based on
composition approach. Bottom-Up model have high interactivity between various modules.
Bottom-Up Model is mainly used by object oriented programming languages like Java, C++ etc.
Advantages of Structured Programming :
The pointer in C language is a variable which stores the address of another variable.
This variable can be of type int, char, array, function, or any other pointer.
General syntax of pointer * is dereferencing operator
& is referencing operator
type *var-name;
Here, type is the pointer's base type; it must be a valid C data type and var-name is
the name of the pointer variable. The asterisk * used to declare a pointer is the
same asterisk used for multiplication. However, in this statement the asterisk is
being used to designate a variable as a pointer.
By the help of * (indirection operator), we can print the value of pointer variable .
Advantages of pointer
Mingw - https://sourceforge.net/projects/mingw-w64/files/latest/download
Vs code - https://code.visualstudio.com/docs/?dv=win