0% found this document useful (0 votes)
31 views12 pages

1 DS Intro

Uploaded by

psparsewar18
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views12 pages

1 DS Intro

Uploaded by

psparsewar18
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

What are Data Structures?

Data structure is a storage that is used to store and organize data. It is a


way of arranging data on a computer so that it can be accessed and
updated efficiently.

Depending on your requirement and project, it is important to choose the


right data structure for your project. For example, if you want to store data
sequentially in the memory, then you can go for the Array data structure.

Types of Data Structure


Basically, data structures are divided into two categories:

 Linear data structure

 Non-linear data structure

Let's learn about each type in detail.

1
Data Structure Classification

Linear data structures


In linear data structures, the elements are arranged in sequence one after
the other. Since elements are arranged in particular order, they are easy to
implement.

However, when the complexity of the program increases, the linear data
structures might not be the best choice because of operational
complexities.

Popular linear data structures are:


1. Array Data Structure

In an array, elements in memory are arranged in continuous memory. All


the elements of an array are of the same type. And, the type of elements
that can be stored in the form of arrays is determined by the programming
language.

2
To learn more, visit Java Array.

An array with each element


represented by an index

2. Stack Data Structure

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).

3. Queue Data Structure

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.

4. Linked List Data Structure

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.

To learn more, visit Linked List Data Structure.

A linked list

Non linear data structures


Unlike linear data structures, elements in non-linear data structures are not
in any sequence. Instead they are arranged in a hierarchical manner where
one element will be connected to one or more elements.

Non-linear data structures are further divided into graph and tree based
data structures.

1. Graph Data Structure

In graph data structure, each node is called vertex and each vertex is
connected to other vertices through edges.

To learn more, visit Graph Data Structure.

4
Graph data structure example

Popular Graph Based Data Structures:


 Spanning Tree and Minimum Spanning Tree
 Strongly Connected Components
 Adjacency Matrix
 Adjacency List
2. Trees Data Structure

Similar to a graph, a tree is also a collection of vertices and edges.


However, in tree data structure, there can only be one edge between two
vertices.

To learn more, visit Tree Data Structure.

Tree data structure example

Popular Tree based Data Structure


 Binary Tree
 Binary Search Tree
 AVL Tree
 B-Tree
 B+ Tree
 Red-Black Tree

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.

Linear Data Structures Non Linear Data Structures

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.

It can be traversed on a single run. That is, if


It requires multiple runs. That is, if we start from the first
we start from the first element, we can
element it might not be possible to traverse all the
traverse all the elements sequentially in a
elements in a single pass.
single pass.

Different structures utilize memory in different efficient


The memory utilization is not efficient.
ways depending on the need.

The time complexity increase with the data


Time complexity remains the same.
size.

Example: Arrays, Stack, Queue Example: Tree, Graph, Map

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:

Processor speed: To handle very large amout of data, high speed


processing is required, but as the data is growing day by day to the
billions of files per entity, processor may fail to deal with that much
amount of data.

Data Search: Consider an inventory size of 106 items in a store, If our


application needs to search for a particular item, it needs to traverse 106
items every time, results in slowing down the search process.

Multiple requests: If thousands of users are searching the data


simultaneously on a web server, then there are the chances that a very
large server can be failed during that process

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.

Operations on data structure


1) Traversing: Every data structure contains the set of data elements.
Traversing the data structure means visiting each element of the data

7
structure in order to perform some specific operation like searching or
sorting.

Example: If we need to calculate the average of the marks obtained by a


student in 6 different subject, we need to traverse the complete array of
marks and calculate the total sum, then we will devide that sum by the
number of subjects i.e. 6, in order to find the average.

2) Insertion: Insertion can be defined as the process of adding the


elements to the data structure at any location.

If the size of data structure is n then we can only insert n-1 data elements
into it.

3) Deletion:The process of removing an element from the data structure


is called Deletion. We can delete an element from the data structure at
any random location.

If we try to delete an element from an empty data structure


then underflow occurs.

4) Searching: The process of finding the location of an element within


the data structure is called Searching. There are two algorithms to
perform searching, Linear Search and Binary Search. We will discuss each
one of them later in this tutorial.

5) Sorting: The process of arranging the data structure in a specific order


is known as Sorting. There are many algorithms that can be used to
perform sorting, for example, insertion sort, selection sort, bubble sort,
etc.

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.

To assess the complexity, the order (approximation) of the count of


operation is always considered instead of counting the exact steps.

Asymptotic notations are mathematical tools to represent the time complexity


of algorithms for asymptotic analysis.
There are mainly three asymptotic notations:
1. Big-O Notation (O-notation)
2. Omega Notation (Ω-notation)
3. Theta Notation (Θ-notation)

Typical Complexities of an Algorithm


o Constant Complexity:
It imposes a complexity of O(1). It undergoes an execution of a
constant number of steps like 1, 5, 10, etc. for solving a given
problem. The count of operations is independent of the input data
size.
o Logarithmic Complexity:
It imposes a complexity of O(log(N)). It undergoes the execution of
the order of log(N) steps. To perform operations on N elements, it
often takes the logarithmic base as 2.
For N = 1,000,000, an algorithm that has a complexity of O(log(N))
would undergo 20 steps (with a constant precision). Here, the
logarithmic base does not hold a necessary consequence for the
operation count order, so it is usually omitted.
o Linear Complexity:
o It imposes a complexity of O(N). It encompasses the same
number of steps as that of the total number of elements to
implement an operation on N elements.
o Quadratic Complexity: It imposes a complexity of O(n2). For N
input data size, it undergoes the order of N 2 count of operations on

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.

o Cubic Complexity: It imposes a complexity of O(n3). For N input


data size, it executes the order of N 3 steps on N elements to solve a
given problem.
For example, if there exist 100 elements, it is going to execute
1,000,000 steps.
o Exponential Complexity: It imposes a complexity of O(2n),
O(N!), O(nk), …. For N elements, it will execute the order of count
of operations that is exponentially dependable on the input data
size.
For example, if N = 10, then the exponential function 2 N will result in
1024. Similarly, if N = 20, it will result in 1048 576, and if N = 100, it
will result in a number having 30 digits. The exponential function N!
grows even faster; for example, if N = 5 will result in 120. Likewise,
if N = 10, it will result in 3,628,800 and so on.

12

You might also like