1 DS Intro
1 DS Intro
1
Data Structure Classification
However, when the complexity of the program increases, the linear data
structures might not be the best choice because of operational
complexities.
2
To learn more, visit Java Array.
In stack data structure, elements are stored in the LIFO principle. That is,
the last element stored in a stack will be removed first.
It works just like a pile of plates where the last plate kept on the pile will be
removed first. To learn more, visit Stack Data Structure.
In a stack, operations can be perform only from one end (top here).
Unlike stack, the queue data structure works in the FIFO principle where
first element stored in the queue will be removed first.
It works just like a queue of people in the ticket counter where first person
on the queue will get the ticket first. To learn more, visit Queue Data
Structure.
3
In a queue, addition and removal are performed from separate ends.
In linked list data structure, data elements are connected through a series
of nodes. And, each node contains the data items and address to the next
node.
A linked list
Non-linear data structures are further divided into graph and tree based
data structures.
In graph data structure, each node is called vertex and each vertex is
connected to other vertices through edges.
4
Graph data structure example
5
Linear Vs Non-linear Data Structures
Now that we know about linear and non-linear data structures, let's see the
major differences between them.
The data items are arranged in sequential The data items are arranged in non-sequential
order, one after the other. order (hierarchical manner).
All the items are present on the single layer. The data items are present at different layers.
6
Need of Data Structures
As applications are getting complexed and amount of data is increasing
day by day, there may arrise the following problems:
in order to solve the above problems, data structures are used. Data is
organized to form a data structure in such a way that all items are not
required to be searched and required data can be searched instantly.
7
structure in order to perform some specific operation like searching or
sorting.
If the size of data structure is n then we can only insert n-1 data elements
into it.
6) Merging: When two lists List A and List B of size M and N respectively,
of similar type of elements, clubbed or joined to produce the third list, List
C of size (M+N), then this process is called merging
8
DS Algorithm
What is an Algorithm?
An algorithm is a process or a set of rules required to perform calculations
or some other problem-solving operations especially by a computer. The
formal definition of an algorithm is that it contains the finite set of
instructions which are being carried in a specific order to perform the
specific task. It is not the complete program or code; it is just a solution
(logic) of a problem, which can be represented either as an informal
description using a Flowchart or Pseudocode.
Characteristics of an Algorithm
The following are the characteristics of an algorithm:
o Input: An algorithm has some input values. We can pass 0 or some input
value to an algorithm.
o Output: We will get 1 or more output at the end of an algorithm.
o Unambiguity: An algorithm should be unambiguous which means that
the instructions in an algorithm should be clear and simple.
o Finiteness: An algorithm should have finiteness. Here, finiteness means
that the algorithm should contain a limited number of instructions, i.e., the
instructions should be countable.
o Effectiveness: An algorithm should be effective as each instruction in an
algorithm affects the overall process.
o Language independent: An algorithm must be language-independent so
that the instructions in an algorithm can be implemented in any of the
languages with the same output.
Dataflow of an Algorithm
o Problem: A problem can be a real-world problem or any instance from the
real-world problem for which we need to create a program or the set of
instructions. The set of instructions is known as an algorithm.
o Algorithm: An algorithm will be designed for a problem which is a step by
step procedure.
o Input: After designing an algorithm, the required and the desired inputs
are provided to the algorithm.
9
o Processing unit: The input will be given to the processing unit, and the
processing unit will produce the desired output.
o Output: The output is the outcome or the result of the program.
o We will write an algorithm to add two numbers entered by the user.
o The following are the steps required to add two numbers
entered by the user:
o Step 1: Start
o Step 2: Declare three variables a, b, and sum.
o Step 3: Enter the values of a and b.
o Step 4: Add the values of a and b and store the result in the sum
variable, i.e., sum=a+b.
o Step 5: Print sum
o Step 6: Stop
10
Complexity of Algorithm
The term algorithm complexity measures how many steps are required by
the algorithm to solve the given problem. It evaluates the order of count
of operations executed by an algorithm as a function of input data size.
11
N number of elements for solving a given problem.
If N = 100, it will endure 10,000 steps. In other words, whenever the
order of operation tends to have a quadratic relation with the input
data size, it results in quadratic complexity. For example, for N
number of elements, the steps are found to be in the order of
3*N2/2.
o It also imposes a run time of O(n*log(n)). It undergoes the
execution of the order N*log(N) on N number of elements to
solve the given problem.
For a given 1000 elements, the linear complexity will execute
10,000 steps for solving a given problem.
12