UNIT 1- Data structure

Download as pdf or txt
Download as pdf or txt
You are on page 1of 58

DATA STRUCTURE

UNIT 1 – Introduction to Data Structure

Compiled by Ms. Archana Patil, Asst. Professor, ALSJ


Data
➢ The term data has been derived from the word datum. The
term data refers to the value or simply set of values that are
raw and unorganized.
➢ Data may be simple, random, and useless until it is organized.
➢ Data is valuable raw material which can be in different forms
such as numbers, words, alphabets, etc.

Compiled by Ms. Archana Patil, Asst. Professor, ALSJ


Information
When data is processed i.e. organized, structured, and
presented in a meaningful context so that it becomes
useful for decision making and understanding, it
becomes Information.

Compiled by Ms. Archana Patil, Asst. Professor, ALSJ


Compiled by Ms. Archana Patil, Asst. Professor, ALSJ
DATA STRUCTURE
 Data Structure is a way of storing the data in computer’s
memory so that it can be used efficiently.
 A Data Structure is a logical/ mathematical model of
organization of data.
 If a Data structure is well designed it allows a variety of
operations to be performed very efficiently.

Compiled by Ms. Archana Patil, Asst. Professor, ALSJ


Classification of Data Structures

Compiled by Ms. Archana Patil, Asst. Professor, ALSJ


Classification of Data Structures

 Linear and Non Linear

 Static and Dynamic

 Homogenous and Non-Homogenous

Compiled by Ms. Archana Patil, Asst. Professor, ALSJ


Classification of Data Structures
Linear and Non Linear
Linear Data Structure – The elements in a linear data
Structure is stored in linear sequence.

Non Linear Data Structure – The elements in a Non linear


data Structure does not form any linear sequence.

Compiled by Ms. Archana Patil, Asst. Professor, ALSJ


Classification of Data Structures
Static and Dynamic
Static Data Structure – Static Data Structure are those whose memory
occupation is fixed. The memory taken by this data structure cannot be
increased or decreased.

Dynamic Data Structure : Dynamic Data Structure are those whose


memory occupation is not fixed. The memory taken by this data
structure cannot be increased or decreased at run time.
Data
Structure
• Stack ,Queue, Tree,
Graph can be static Static Dynamic
NOTE
as well as non static.

Array Linked
List

Compiled by Ms. Archana Patil, Asst. Professor, ALSJ


Classification of Data Structures
Homogenous and Non-Homogenous
 Homogenous Data Structure - Homogenous Data Structure are
those in which data of same type is stored.
 Non Homogenous Data Structure – Non Homogenous Data
Structure are those in which data of different type can be
stored.
Data Structure

• Stack ,Queue, Tree, Graph


can be homogeneous as Non
well as non homogeneous Homogenous
Homogenous
NOTE

Array Linked List

Compiled by Ms. Archana Patil, Asst. Professor, ALSJ


Types of Data Structure

a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8] a[9] a[10]
• 57 • 47 • 28 • 12 • 17 •8 • 99 • 67 • 55 •6

b[1] b[2] b[3] b[4] b[5] b[6] b[7] b[8] b[9] b[10]

• S •T •R •U •C •T •U •R •E •S

Compiled by Ms. Archana Patil, Asst. Professor, ALSJ


Types of Data Structure

Compiled by Ms. Archana Patil, Asst. Professor, ALSJ


Types of Data Structure

Compiled by Ms. Archana Patil, Asst. Professor, ALSJ


Types of Data Structure

Compiled by Ms. Archana Patil, Asst. Professor, ALSJ


Types of Data Structure

Compiled by Ms. Archana Patil, Asst. Professor, ALSJ


Types of Data Structure

Compiled by Ms. Archana Patil, Asst. Professor, ALSJ


Operations on Data Structure
The data stored in data Structure can be processed
using various operations.
1. Insertion : Adding a new element into the data
structure
2. Deletion – Removing an element from the data
structure
3. Traversing : Accessing each element of Data
Structure exactly once.
4. Searching : Finding the position of desired element
of Data Structure.

Compiled by Ms. Archana Patil, Asst. Professor, ALSJ


There are some operations which are less frequently
performed on Data Structure.

Merging : Combining two or more list into a single list.

Splitting : Dividing a single list into two or more list.

Coping : Creating a duplicate copy of a list.

Sorting : Arranging the elements into ascending or


descending orders.

Compiled by Ms. Archana Patil, Asst. Professor, ALSJ


An Algorithm is defined as finite collection of well defined steps
designed to solve a particular problem.
An algorithm has following characteristics
1. Input - An algorithm must take some input that are required for a
solution.
2. Process - An algorithm must perform certain operations on the input data
which are necessary for the solution of the problem.
3. Output - An algorithm should produce certain output after processing the
data.
4. Finiteness - An Algorithm must terminated after executing certain finite
number of steps..
5. Effectiveness - Every step of an algorithm should play a role in the
solution to the problem

Algorithm

Compiled by Ms. Archana Patil, Asst. Professor, ALSJ


Programming Requirements of an Algorithm
An Algorithm must use the features supported by the
programming language in which it is to be implemented.
An algorithm is of no use if it cannot be programmed
and implemented. Therefore algorithm must satisfy
programming features. i.e the requirements .
1. Space Requirement of Algorithm.
2. Time Requirement of Algorithm.

Compiled by Ms. Archana Patil, Asst. Professor, ALSJ


To execute any program , space is needed for various reasons.

 Space required by Data: This include space required to


store variables and constants which is fixed or Sometimes
Space is allocated dynamically( at runtime) which is not fixed.

 Space required by instructions : This is space which is


used to store the instruction set. This is fixed because the
instructions does not change during run time.

Space Requirement of an Algorithm

Compiled by Ms. Archana Patil, Asst. Professor, ALSJ


Each algorithm takes some time to execute. To study the time
requirement of algorithm is very important for the following
reason

 Sometimes it is necessary to know in advance how much time


the program will take to execute, so that we can find out
whether it is acceptable or not.

 As one problem can be solved in many ways with different


time requirement , therefore we can select the optimal one.

Time Requirement of an Algorithm

Compiled by Ms. Archana Patil, Asst. Professor, ALSJ


Complexity of an algorithm
Complexity means the time and space requirement of an
algorithm.
If time and space requirement is more complexity is more , if
time and space requirement is less complexity is less.

Out of this two factors, the space requirement is not very


important but the time requirement is considered to be the most
important factors.

The time requirement of an


algorithm depends on the input
size. i.e if the input size is more
complexity will be more and if
the input size is less complexity
will be less.

Compiled by Ms. Archana Patil, Asst. Professor, ALSJ


 Complexity is divided into three types.
1. Worst Case complexity – if the running time of the
algorithm is longest for all the input, then the complexity is
called worst case complexity.

2. Best Case Complexity - if the running time of the


algorithm is shortest for all the input, then the complexity is
called Best case complexity.

3. Average Case Complexity – if the running time of the


algorithm fall between Best case and worst case, then the
complexity is called Average case complexity.

Types of Complexity
Compiled by Ms. Archana Patil, Asst. Professor, ALSJ
1 45
Enter Element to be searched
Worst
case 67
2 34
Time required for search – 8 m sec
3 76
Enter Element to be searched
4 85 Best case 45
Time required for search – 1m sec
5 23

6 49
Enter Element to be searched
Average
case 85
7 12
Time required for search – 4 m sec
8 67

Considering that time required to traverse one single index of array is 1 millisecond

Compiled by Ms. Archana Patil, Asst. Professor, ALSJ


DATA STRUCTURE
UNIT 1 – ARRAY

Compiled by Ms. Archana Patil, Asst. Professor, ALSJ


 An array is linear collection of finite number of homogeneous
data elements.
 When array is stored in computer’s memory, its elements gets
stored in consecutive memory locations.
 The elements of the array are referenced with an index set
consisting of consecutive number.
 Arrays are popularly known as Vectors and Tables in
mathematics.

Array (One dimensional Array)


Compiled by Ms. Archana Patil, Asst. Professor, ALSJ
Size of an Array
The number of elements in an array is called the size or the
length of the array.
Consider an Array S with the 8 number of elements,
200 300 250 600 540 190 330 760

2776 2777 2778 2779 2780 2781 2782 2783

Here in above example the size of array is calculated using


following formula : ub – upper bound (upper
Size of an array S = ub – lb +1 index of an array)
= 2783- 2776 +1
= 7 +1 lb- lower bound ( lower
index of an array)
= 8

Compiled by Ms. Archana Patil, Asst. Professor, ALSJ


Examples to solve

1. Consider the upper bound of an array as 3660


and lower bound as 3567. Calculate the size of an
array.

2. Consider an array A of size 22. The upper bound


of an array is 4879. Find the lower bound.

Compiled by Ms. Archana Patil, Asst. Professor, ALSJ


Various operations associated with Array
There are various operations that can be performed
on Array. Some of these operations are
 Traversing
 Insertion
 Deletion
 Searching
 Sorting
 Merging

Compiled by Ms. Archana Patil, Asst. Professor, ALSJ


Traversing an Array
 Traversing operations refers to processing or visiting the
elements of the array.
 Consider the array S, of size 8 with ub and lb
S[0] S[1] S[2] S[3] S[4] S[5] S[6] S[7]

200 300 250 600 540 190 330 760

2776 2777 2778 2779 2780 2781 2782 2783

 Traversing array S means , visiting each and every element


from S[0] to S[7].

Compiled by Ms. Archana Patil, Asst. Professor, ALSJ


 Algorithm

S[0] S[1] S[2] S[3] S[4] S[5] S[6] S[7]


200 300 250 600 540 190 330 760

2776 2777 2778 2779 2780 2781 2782 2783


Compiled by Ms. Archana Patil, Asst. Professor, ALSJ
Insertion in an Array
• Insertion refers to adding new element in the array.
• The new element can be inserted at any position in the array
space that the memory allocated to the array is sufficient to
accommodate the new element.
• Suppose if the size of element
is 5 and the array already have
stores 5 elements then it is not
possible to insert new element.

If the insertion is at end then its simple operation as no data


moment take place, we just need to insert a new element at end.
But if the insertion is at any other position of an array, then
elements starting from insertion position are required to move
one position right to make space for new element.
Compiled by Ms. Archana Patil, Asst. Professor, ALSJ
Algorithm for inserting element into an Array

Consider array of size 8, it is


filled by 5 elements , suppose
we want to insert new element
at index position 4

Compiled by Ms. Archana Patil, Asst. Professor, ALSJ


Consider an array S of size 6 with 3 elements
Here n (last index) =2
11 43 47 i=n
Algorithm : (loop)
S[0] S[1] S[2] S[3] S[4] S[5]
set S[i+1]=S[i]
set i = i-1
Insert New element 53 , at position 2 i.e. k=S[1]
Here n (last index) = 2
i=2
11 43 47 47
Algorithm : (loop)
S[0] S[1] S[2] S[3] S[4] S[5] set S[2+1]=S [2]
set i = 2-1

11 43 43 47 Algorithm : (loop)
i=1
S[0] S[1] S[2] S[3] S[4] S[5] set S[1+1]=S [1]
set i = 1-1
New
Set S[k] =new
11 53 43 47
S[1] = 53
S[0] S[1] S[2] S[3] S[4] S[5] Set n = n+1
n=2+1
Compiled by Ms. Archana Patil, Asst. Professor, ALSJ
Deletion from Array
Deletion operation refers to removing an existing element from
an array.
The element from any position can be removed from an array.
Removal of element from the end of the array is very simple
operation as no data element is involved.
But removing an element at any position from an array, all
element which is next to the element to be deleted has to be
shifted one position to its left.

Compiled by Ms. Archana Patil, Asst. Professor, ALSJ


Example

Compiled by Ms. Archana Patil, Asst. Professor, ALSJ


Algorithm

Compiled by Ms. Archana Patil, Asst. Professor, ALSJ


Consider an array S of size 6 with 4 elements
Here n (last index) = 3
11 43 47 54 i=k
Algorithm : (loop)
S[0] S[1] S[2] S[3] S[4] S[5]
set S[i]=s[i+1]
set i = i+1
delete element 43 , which is at position 1, k = [1]
Here n (last index) = 3
47 i=1
11 43 47 54
Algorithm : (loop)
S[0] S[1] S[2] S[3] S[4] S[5] set S[1]=S [1+1]
set i = [1+1]
47

11 47 53
47 53 Algorithm : (loop)
i=2
S[0] S[1] S[2] S[3] S[4] S[5] set S[2]=S [2+1]
set i = [2+1]

11 47 53 Set n = n-1
n=3-1
S[0] S[1] S[2] S[3] S[4] S[5]

Compiled by Ms. Archana Patil, Asst. Professor, ALSJ


Searching in an Array
 Searching operation referred to finding a position
of desired elements in an array.
 There are two different searching techniques
1. Linear Search
2. Binary Search

Compiled by Ms. Archana Patil, Asst. Professor, ALSJ


Linear Search
➢A linear search or sequential search is a method for finding an
element within a list. It sequentially checks each element of the
list until a match is found or the whole list has been searched.
➢ Linear search is applied when we have no knowledge about the
arrangement of the elements.
➢ In this the searching the element is compared to the
1st position of an array, then the 2nd position, then 3rd S[0] 34
and so on. If the desired element is found then search S[1] 45
is successful and the algorithm terminates. S[2] 23
Or else if we reach to the end of the array and still S[3] 56
the match is not found then the S[4] 12
search is said to be unsuccessful. S[5] 7

Compiled by Ms. Archana Patil, Asst. Professor, ALSJ


 Algorithm

Here n = 6
Suppose we want to find 23
so Data =23

S[1] 34
S[2] 45
S[3] 23
S[4] 56
S[5] 12
S[6] 7

Element is found at position 3

Compiled by Ms. Archana Patil, Asst. Professor, ALSJ


 Binary Search
Binary search is applied on an array when it is known that the
elements in the array are sorted alphabetically or numerically in
increasing and decreasing.
When Binary search is applied on an array, the first step is to find
the index of middle element of an array.

Then element is compared at middle index, if found then algorithm


is stopped.
Other wise if desired element is smaller than middle element then
first part of the list is searched or else if desired element is
greater than middle element then first part of the list is searched

Compiled by Ms. Archana Patil, Asst. Professor, ALSJ


Consider an sorted array S of size 7. 4 10 13 23 45 63 70
1 2 3 4 5 6 7

Search Element 10 , Data = 10


First middle index is found Middle = Integer (start + End) M = (1+ 7)
-------------- --------- = 4
2 2

Compare Data with middle , S[4] Not Successful

Then check if Data is less or grater than S[M], 10 is les than 23 so


i.e. Check weather 10 > 23 or 10 < 23 search it in first part

Now considering the first part of list again start the algorithm

Compiled by Ms. Archana Patil, Asst. Professor, ALSJ


First Part - Repeat the algorithm 4 10 13
1 2 3

Step 1 : find the middle element : (1+3) /2 = 4/2 = 2

4 10 13
1 2 3

Step 2 : Compare Data with Mid element.

IS Data = S[2] Successful

Result : Data is present at location 2

Compiled by Ms. Archana Patil, Asst. Professor, ALSJ


Compiled by Ms. Archana Patil, Asst. Professor, ALSJ
Sorting an Array
Sorting of an array refers to arranging the elements of
the array in some logical order. The logical order can
be increasing or decreasing order of element.
The following is the sorted array S of size 8
4 10 13 23 45 63 70 82
1 2 3 4 5 6 7 8

Where S[1] < S[2] < S[3] ……. <S[8]

The simplest sorting technique is Bubble sort of Exchange Sort

Compiled by Ms. Archana Patil, Asst. Professor, ALSJ


Merging of Array
Merging of Array refers to combining the elements of two linear
arrays into a single array. We can merge it in 2 ways.
1. When elements of the array are not sorted and merged
array also need not to be sorted.
4 20 13 23 7 30 14
1 2 3 4 1 2 3

Array 1 Array 2

4 20 13 23 7 30 14
1 2 3 4 5 6 7

New Merged Array

Compiled by Ms. Archana Patil, Asst. Professor, ALSJ


2. If the element in the given arrays are sorted and the
merged array is also required to be sorted.

4 7 30 52 10 35 50
1 2 3 4 1 2 3

Sorted Array 1 Sorted Array 2

4 7 10 30 35 50 52
1 2 3 4 5 6 7

New Merged Sorted Array

Compiled by Ms. Archana Patil, Asst. Professor, ALSJ


Spare Array
Compiled by Ms. Archana Patil, Asst. Professor, ALSJ
Spare Array

Compiled by Ms. Archana Patil, Asst. Professor, ALSJ


Spare Array

Compiled by Ms. Archana Patil, Asst. Professor, ALSJ


Spare Array

Compiled by Ms. Archana Patil, Asst. Professor, ALSJ


Vector representation of Matrix
In this technique the non zero element of the spare matrix is
stored in the array along with the row id and column id. The non
zero element of the matrix are stored in the row discarding all
the zero entries.
For eg : consider a 4X5 spare Array, which is represented in
vector V.

Compiled by Ms. Archana Patil, Asst. Professor, ALSJ


ADVANTAGES OF ARRAY
1. Array is simple kind of Data Structure which is very easy
to implement
2. Address of any element of Array can be calculated
easily as elements are stored in contiguous memory
locations.
3. Array can be used to implement other data structure
such as stack, queue, tree and graph.
4. If elements of an array are stored in one logical order
then binary search can be applied to search an element
of an array efficiently.

Advantages & Limitations of Array


Compiled by Ms. Archana Patil, Asst. Professor, ALSJ
LIMITATIONS OF ARRAY

1. Array is a static data structure. Memory used by an array


cannot be increased or decreased whether it is allocated
at run time or compile time.
2. Insertion and deletion of elements are very time
consuming in Array.
3. Only homogeneous elements can be stored in the array.

Advantages & Limitations of Array


Compiled by Ms. Archana Patil, Asst. Professor, ALSJ
Q1. Consider an array A of size 15
5 8 10 16 19 22 28 32 47 50 55 67 83 91 98

Search element 55 in the above array by linear as well


as binary search, find the time complexity and state
which is efficient algorithm.

Consider time required to perform each search is 2


millisecond.

Compiled by Ms. Archana Patil, Asst. Professor, ALSJ


Q2. consider the following 6 X 5 matrix, represent in it
vector.
4 0 9 0 0
0 5 0 0 0
0 0 1 0 8
0 0 0 0 0
1 0 0 3 0
2 4 7 0 0

Compiled by Ms. Archana Patil, Asst. Professor, ALSJ

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy