Introduction to Data Structures
Introduction to Data Structures
Tree: In this case, data often contain a hierarchical relationship among various elements. The
data structure that reflects this relationship is termed as rooted treegraph or a tree.
Graph: In this case, data sometimes hold a relationship between the pairs of elements which is
not necessarily following the hierarchical structure. Such data structure is termed as a Graph.
Array is a container which can hold a fix number of items and these items should be of the same
type. Most of the data structures make use of arrays to implement their algorithms. Following are
the important terms to understand the concept of Array.
Unit 1 Introduction to Data structures
Element − Each item stored in an array is called an element.
Index − Each location of an element in an array has a numerical index, which is used to
identify the element.
Array Representation:(Storage structure)
Arrays can be declared in various ways in different languages. For illustration, let's take C array
declaration.
Arrays can be declared in various ways in different languages. For illustration, let's take C array
declaration.
As per the above illustration, following are the important points to be considered.
Index starts with 0.
Array length is 10 which means it can store 10 elements.
Each element can be accessed via its index. For example, we can fetch anelement at index 6 as 9
Basic Operations
Following are the basic operations supported by an array.
Insertion − Adds an element at the given index.
Deletion − Deletes an element at the given index.
Search − Searches an element using the given index or by the value.
Update − Updates an element at the given index.
Traverse − print all the array elements one by one.
Insert operation is to insert one or more data elements into an array. Based on the requirement, a
Unit 1 Introduction to Data structures
new element can be added at the beginning, end, or any given index of array.
Here, we see a practical implementation of insertion operation, where we add data at the end of
the array −
Algorithm
Let LA be a Linear Array (unordered) with N elements and K is a positive integer suchthat
K<=N. Following is the algorithm where ITEM is inserted into the Kth position of LA
−
1. Start
2. Set J = N
3. Repeat steps 4 and 5 while J >= K
4. Set LA[J+1] = LA[J]
5. Set J = J-1[End of step 3 loop]
6. Set LA[K] = ITEM
7. Set N = N+1
8. Stop
Example
Following is the implementation of the above algorithm −
#include <stdio.h>
main () {
int LA[] = {1,3,5,7,8};
int item = 10, k = 3, n = 5;
int i = 0, j = n;
printf ("The original array elements are :\n");
for(i = 0; i<n; i++) {
printf("LA[%d] = %d \n", i, LA[i]);
}
n = n + 1; while(
j >= k) {
LA[j+1] = LA[j];
j = j - 1;
}
LA[k] = item;
Printf ("The array elements after insertion :\n");
for(i = 0; i<n; i++) {
printf("LA[%d] = %d \n", i, LA[i]);
}
}
Unit 1 Introduction to Data structures
When we compile and execute the above program, it produces the following result −
Output
The original array elements are :
LA[0] = 1
LA[1] = 3
LA[2] = 5
LA[3] = 7
LA[4] = 8
The array elements after insertion:
LA[0] = 1
LA[1] = 3
LA[2] = 5
LA[3] = 10
LA[4] = 7
LA[5] = 8
Deletion Operation
Deletion refers to removing an existing element from the array and re-organizing all elements of
an array.
Algorithm
Consider LA is a linear array with N elements and K is a positive integer such that K<=N.
Following is the algorithm to delete an element available at the Kth position of LA.
1. Start
2. Set J = K
3. Repeat steps 4 and 5 while J < N
4. Set LA[J] = LA[J + 1]
5. Set J = J+1
6. Set N = N-1
7. Stop
Example
#include <stdio.h>
void main() {
int LA[] = {1,3,5,7,8};
int k = 3, n = 5;
int i, j;
printf("The original array elements are :\n");
for(i = 0; i<n; i++) {
printf("LA[%d] = %d \n", i, LA[i]);
}
j = k;
while( j < n) {
LA[j-1] = LA[j];
j = j + 1;
}
n = n -1;
printf("The array elements after deletion :\n");
for(i = 0; i<n; i++) {
printf("LA[%d] = %d \n", i, LA[i]);
}
}
When we compile and execute the above program, it produces the following result −
Output
The original array elements are :
LA[0] = 1
LA[1] = 3
LA[2] = 5
LA[3] = 7
LA[4] = 8
The array elements after deletion :
LA[0] = 1
LA[1] = 3
LA[2] = 7
LA[3] = 8
Unit 1 Introduction to Data structures
Search Operation
You can perform a search for an array element based on its value or its index.
Algorithm
Consider LA is a linear array with N elements and K is a positive integer such that K<=N.
Following is the algorithm to find an element with a value of ITEM using sequential search.
1. Start
2. Set J = 0
3. Repeat steps 4 and 5 while J < N
4. IF LA[J] is equal ITEM THEN GOTO STEP 6
5. Set J = J +1
6. PRINT J, ITEM
7. Stop
Example
Following is the implementation of the above algorithm −
#include <stdio.h>
void main() {
int LA[] = {1,3,5,7,8};
int item = 5, n = 5;
int i = 0, j = 0;
printf("The original array elements are :\n");
for(i = 0; i<n; i++) {
printf("LA[%d] = %d \n", i, LA[i]);
}
while( j < n){
if( LA[j] == item ) {
break;
}
j = j + 1;
}
printf("Found element %d at position %d\n", item, j+1);
}
When we compile and execute the above program, it produces the following result −
Output
The original array elements are :
LA[0] = 1
LA[1] = 3
LA[2] = 5
Unit 1 Introduction to Data structures
LA[3] = 7
LA[4] = 8
Found element 5 at position 3
Update Operation
Update operation refers to updating an existing element from the array at a given index.
Algorithm
Consider LA is a linear array with N elements and K is a positive integer such that K<=N.
Following is the algorithm to update an element available at the Kth position of LA.
1. Start
2. Set LA[K-1] = ITEM
3. Stop
Example
Following is the implementation of the above algorithm −
#include <stdio.h>
void main() {
int LA[] = {1,3,5,7,8};
int k = 3, n = 5, item = 10;
int i, j;
printf("The original array elements are :\n");
for(i = 0; i<n; i++) {
printf("LA[%d] = %d \n", i, LA[i]);
}
LA[k-1] = item;
printf("The array elements after updation :\n");
for(i = 0; i<n; i++) {
printf("LA[%d] = %d \n", i, LA[i]);
}
}
When we compile and execute the above program, it produces the following result −
Output
The original array elements are :
LA[0] = 1
LA[1] = 3
LA[2] = 5
LA[3] = 7
LA[4] = 8
UNIT 1: Introduction to Data Structures
Algorithm: -
Definition: - An algorithm is a Step By Step process to solve a problem, where each step
indicates an intermediate task. Algorithm contains finite number of steps that leads to the
solution of the problem.
Properties/Characteristics of
Algorithm:-
Algorithm has the following basic
properties
Input-Output: - Algorithm takes ‘0’ or more input and produces the required
output. This is the basic characteristic of an algorithm.
Finiteness: - An algorithm must terminate in countable number of steps.
Definiteness: Each step of an algorithm must be stated clearly and unambiguously.
Effectiveness: Each and every step in an algorithm can be converted in to
programming language statement.
Generality: Algorithm is generalized one. It works on all set of inputs and
provides the required output. In other words, it is not restricted to a single input
value.
Categories of Algorithm:
Based on the different types of steps in an Algorithm, it can be divided into three categories,
namely
Sequence
Selection and
Iteration
Sequence: The steps described in an algorithm are performed successively one by one
without skipping any step. The sequence of steps defined in an algorithm should be simple
and easy to understand. Each instruction of such an algorithm is executed, because no
selection procedure or conditional branching exists in a sequence algorithm.
Example:
// adding two
numbers
Step 1: start
Step 2: read a,b
Step 3:
Sum=a+b
Step 4: write
Sum
Step 5: stop
Selection: The sequence type of algorithms are not sufficient to solve the problems, which
UNIT 1: Introduction to Data Structures
involves decision and conditions. In order to solve the problem which involve decision
making or option selection, we go for Selection type of algorithm. The general format of
Selection type of statement is as shown below:
if(condit
ion)
Statem
ent-1;
else
Statement-2;
The above syntax specifies that if the condition is true, statement-1 will be executed otherwise
statement-2 will be executed. In case the operation is unsuccessful. Then sequence of
algorithm should be changed/ corrected in such a way that the system will re- execute until
the operation is successful.
Iteration: Iteration type algorithms are used in solving the problems which involves
repetition of statement. In this type of algorithms, a particular number of statements are
repeated ‘n’ no. of times.
Example1:
Step 1 : start
Step 2 : read n
Step 3 : repeat step 4 until n>0
Step 4 :
r=n mod 10
s=s+r
n=n/1
Step 5 : write s
Step 6 : stop
Performance Analysis an Algorithm:
The Efficiency of an Algorithm can be measured by the following metrics.
i. Time Complexity and
ii. Space
Complexity
i.Time Complexity:
The amount of time required for an algorithm to complete its execution is its time complexity.
An algorithm is said to be efficient if it takes the minimum (reasonable) amount of time to
complete its execution.
ii. Space Complexity:
The amount of space occupied by an algorithm is known as Space Complexity. An algorithm
UNIT 1: Introduction to Data Structures
is said to be efficient if it occupies less space and required the minimum amount of time to
complete its execution.
Write an algorithm to find the largest among three different numbers entered by user
Step 1: Start
Step 2: Declare variables a,b and c
Step 3: Read variables a, b and c.
Step 4: If a>b
If a>c
Display a is the largest
number. Else
Display c is the largest number.
Else
If b>c
Display b is the largest
number. Else
Display c is the greatest number.
Step 5: Stop
ASYMPTOTIC NOTATIONS
Asymptotic analysis of an algorithm refers to defining the mathematical
foundation/framing of its run-time performance. Using asymptotic analysis, we can very
well conclude the best case, average case, and worst-case scenario of an algorithm.
Asymptotic analysis is input bound i.e., if there's no input to the algorithm, it is
concluded to work in a constant time. Other than the "input" all other factors are
considered constant.
Asymptotic analysis refers to computing the running time of any operation in
mathematicalunits of computation. For example, the running time of one operation is
computed as f(n) and may be for another operation it is computed as g(n2). This means
the first operation running time will increase linearly with the increase in n and the
running time of the second operation will increase exponentially when n increases.
Similarly, the running time of both operations will be nearly the same if n is significantly
small.
θ(f(n)) = { g(n) if and only if g(n) = Ο(f(n)) and g(n) = Ω(f(n)) for all n > n0. }