Data Structures Presentation
Data Structures Presentation
A
INSTITUTE OF SCIENCE AND TECHNOLOGY
Deemed to be University
Declared as category ‘A’ University by MHRD, Govt. of India
Jeppiaar Nagar, Rajiv Gandhi Salai, Chennai – 600 119, Tamil Nadu. India.
UNIT : I
UNIT-I
Introduction Data Structures - Need - classification - operations –Abstract data types (ADT) -
Array - characteristics - types - storage representations. Array Order Reversal-Array Counting
or Histogram-Finding the maximum Number in a Set, Recursion- Towers of Hanoi-Fibonacci
series-Factorial.
ALGORITHM
Algorithm is finite set of logic or instructions, written in order for accomplishing the certain
predefined task.
Each algorithm must have:
Specification: Description of the computational procedure.
Pre-conditions: The condition(s) on input.
Body of the Algorithm: A sequence of clear and unambiguous instructions.
Post-conditions: The condition(s) on output.
CHARACTERISTICS OF AN
ALGORITHM
Input: An algorithm must have 0 or well defined inputs.
Output: An algorithm must have 1 or well defined outputs, and should match with the desired
output.
Finiteness: An algorithm must be terminated after the finite number of steps.
Effectiveness: An algorithm must have step-by-step directions which is independent of any
programming code.
Definiteness: An algorithm must be unambiguous and clear.
MAJOR CATEGORIES OF
ALGORITHMS
Sort: Algorithm developed for sorting the items in certain order.
Search: Algorithm developed for searching the items inside a data structure.
Delete: Algorithm developed for deleting the existing element from the data structure.
Insert: Algorithm developed for inserting an item inside a data structure.
Update: Algorithm developed for updating the existing element inside a data structure.
PERFORMANCE OF
ALGORITHM
Time complexity: It is a way of representing the amount of time needed by a program to run to
the completion.
Space complexity: It is the amount of memory space required by an algorithm, during a course
of its execution.
DATA
Data means a value or set of values.
for example, student’s name and its id are the data about the student.
STRUCTURE
A structure is an arrangement of and relations between parts or elements.
It has a form or shape.
DATA STRUCTURE
A data structure is an arrangement of data elements.
Data Structure can be defined as the group of data elements which provides an efficient way of storing and organizing
data in the computer so that it can be used efficiently.
Examples of Data Structures are arrays, Linked List, Stack, Queue, etc
Data Structures are widely used in almost every aspect of Computer Science i.e. Operating System, Compiler Design,
Artificial Intelligence, Graphics and many more.
DATA STRUCTURE
Data Structure mainly specifies the following three things
o Organization of Data
o Accessing methods
o Processing alternatives for information
Example
int [] i;
i = new int[10];
int [] i = new int[10];
INITIALIZATION – SINGLE
DIMENSION ARRAY
int [] x = new int[1];
int [] x = {10}; x 10
int [] x = { 10, 20 };
x 10 20
CHARACTERISTICS
Array stores elements of same data type.
The elements of an array are stored in contiguous memory locations.
When data objects are stored in array, individual objects are selected by an index. By default
an array index starts from 0 and ends with (n-1). Index is also referred as subscripts.
The size of the array is mentioned at the time of declaring array.
While declaring 2D array, number of columns should be specified whereas for number of rows
there is no such rule.
Size of array can’t be changed at run time (depends on the programming language)
STORAGE REPRESENTATION
An array is a set of homogeneous elements.
Every element is referred by an index.
Memory storage locations of the elements are not arranged as a rectangular array with rows
and columns. Instead they are arranged in a linear sequence beginning with location 1,2,3 and
so on.
The nature of storage depends on how the programming language stores the 2D array
elements.
The elements are stored either column-by-column (column-major order) or row-by-row (row-
major order).
EXAMPLE – ROW MAJOR
Rows
ORDER
:3
Data (A):
1 2 3 4
Columns :4 5 6 7 8
9 10 11 12
Linear Arrangement of Array A
Row 1 2 3
Index (1,1) (1,2) (1,3) (1,4) (2,1) (2,2) (2,3) (2,4) (3,1) (3,2) (3,3) (3,4)
Memory 100 102 104 106 108 110 112 114 116 118 120 122
Data 1 2 3 4 5 6 7 8 9 10 11 12
ACCESS
Columns: N
Element at ith row and jth column is at the position
N*(i–1)+(j–1)
EXAMPLE – COLUMN MAJOR
ORDER
Rows :3
Data (A):
1 2 3 4
Columns :4 5 6 7 8
9 10 11 12
Linear Arrangement of Array A
Column 1 2 3 4
Index (1,1) (2,1) (3,1) (1,2) (2,2) (3,2) (1,3) (2,3) (3,3) (1,4) (2,4) (3,4)
Memory 100 102 104 106 108 110 112 114 116 118 120 122
Data 1 5 9 2 6 10 3 7 11 4 8 12
ROW MAJOR ORDER
Location (A [j,k] ) = Base Address of (A) + w [ (N * (j-1)) + (k-1) ]
Memory 100 102 104 106 108 110 112 114 116 118 120 122
Data 1 2 3 4 5 6 7 8 9 10 11 12
Memory 100 102 104 106 108 110 112 114 116 118 120 122
Data 1 5 9 2 6 10 3 7 11 4 8 12
x 10 20
30 40
THREE DIMENSIONAL ARRAY
int [][][] A = new int[3][4][3];
1 2 3
REVERSING AN ARRAY
Input: A = [1, 2, 3, 4, 5]
Output: A = [5, 4, 3, 2, 1]
Algorithm:
n = length of the Array (A)
for i = n – 1, j = 0, i >= 0, increment j by 1 and decrement i by
B[j] = A[i]
for i = 0 to n
A[i] = B[i]
1)
ARRAY COUNT
Initialize i as 0
2) Do following while i < n
// If this element is already processed then nothing to do
a) If arr[i] 0)
(i) arr[i] = arr[elementIndex];
// After storing arr[elementIndex], change it to store initial count of 'arr[i]'
(ii) arr[elementIndex] = -1;
d) else
// If this is NOT first occurrence of arr[i], then decrement its count.
(i) arr[elementIndex]--;
// And initialize arr[i] as 0 means the element 'i+1' is not seen so far
(ii) arr[i] = 0;
(iii) i++;
3)Now -arr[i] stores count of i+1.
HISTOGRAM
PROCEDURE
#include <bits/stdc++.h>
// print last line denoted by ----
for (int i = 0; i < n + 3; i++)
void printHistogram(int arr[], int n)
cout << "---";
{
cout << "\n";
int maxEle = *max_element(arr, arr + n);
for (int i = maxEle; i >= 0; i--) { cout << " ";
cout.width(2); for (int i = 0; i < n; i++) {
cout << right << i << " | "; cout.width(2); // width for a number
for (int j = 0; j < n; j++) { cout << right << arr[i] << " ";
// if array of element is greater }
// then array it print x
}
if (arr[j] >= i)
int main()
cout << " x ";
{
// else print blank spaces
else int arr[10] = { 10, 9, 12, 4, 5, 2, 8, 5, 3, 1 };
cout << " "; int n = sizeof(arr) / sizeof(arr[0]);
} printHistogram(arr, n);
cout << "\n"; return 0;
} }
Int fibo(n) {
If (n==0) return 0;
If (n==1) return 1;
n=3
return fibo(n-1)+fibo(n-
2);
}
n=1 n=0
Int fibo(n) { Int fibo(n) {
If (n==0) return 0; If (n==0) return 0;
If (n==1) return 1; If (n==1) return 1;
return fibo(n-1)+fibo(n- return fibo(n-1)+fibo(n-
2); 2);
} }
1 +1=2
n=1
Int fibo(n) {
If (n==0) return 0;
If (n==1) return 1;
return fibo(n-1)+fibo(n-
2);
}
TOWERS OF HANOI
There are three towers
64 gold disks, with decreasing sizes, placed on the first tower
Need to move all of the disks from the first tower to the last tower
Larger disks can not be placed on top of smaller disks
The third tower can be used to temporarily hold disks
TOWER OF HANOI
2, A, C, B
1, A, B, C
0, A, C, B
0, C, B, A
1, B, C, A
0, B, A, C
0, A, C, B
2, C, B, A
1, C, A, B
0, C, B, A
0, B, A, C
1, A, B, C
0, A, C, B
0, C, B, A
TAIL RECURSION
void print(int n)
{
if (n < 0)
return;
printf(“%d”, n);
print(n-1);
}
OPERATIONS ON DATA
STRUCTURES
Traversing
Searching
Inserting
Deleting
Sorting
Merging