0% found this document useful (0 votes)
3 views

Introduction to Data Structures

This document provides an introduction to data structures, explaining their importance in organizing and storing data efficiently. It categorizes data structures into linear (like arrays, stacks, queues, and linked lists) and non-linear (like trees and graphs), detailing their characteristics and basic operations such as insertion, deletion, searching, and updating. Additionally, it discusses algorithms, their properties, types, and performance analysis through time and space complexity.

Uploaded by

onkargutti94
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Introduction to Data Structures

This document provides an introduction to data structures, explaining their importance in organizing and storing data efficiently. It categorizes data structures into linear (like arrays, stacks, queues, and linked lists) and non-linear (like trees and graphs), detailing their characteristics and basic operations such as insertion, deletion, searching, and updating. Additionally, it discusses algorithms, their properties, types, and performance analysis through time and space complexity.

Uploaded by

onkargutti94
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Unit 1 Introduction to Data structures

Introduction to Data structures: -


In computer terms, a data structure is a Specific way to store and organize data in a computer's memory
so that these data can be used efficiently later.
Data may be arranged in many different ways such as the logical or mathematical model for aparticular
organization of data is termed as a data structure.
The variety of a particular data model depends on the two factors -
Firstly, it must be loaded enough in structure to reflect the actual relationships of the data
with the real world object.
Secondly, the formation should be simple enough so that anyone can efficiently process
the data each time it is necessary.

Categories of Data Structure:


The data structure can be sub divided into major types:
Linear Data Structure
Non-linear Data Structure
Linear Data Structure:
A data structure is said to be linear if its elements combine to form any specific order. There are
basically two techniques of representing such linear structure within memory.
First way is to provide the linear relationships among all the elements represented by
means of linear memory location. These linear structures are termed as arrays.
The second technique is to provide the linear relationship among all the elements
represented by using the concept of pointers or links. These linear structures are termed as linked
lists.
The common examples of linear data structure are:
Arrays-Array is a collection of Similar type of element.
Queues-we can perform two operations i.e.- Rear and Front
Queue is a FIFO operation.
Stacks-we can perform two operations i.e.-Push and Pop
Stack is a LIFO operation.
Linked lists-Linked list is connected to nodes. Were each node
stores the data the address of the next node
Non linear Data Structure:
This structure is mostly used for representing data that contains a hierarchicalrelationship
among various elements.
Examples of Non-Linear Data Structures are listed below:
Graphs
family of trees

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

Differences between linear and nonlinear data structure

Linear Data structure Non-Linear Data Structure


1.Every data element is connected to its 1.Every data element is connected with many
previous & next one. other data elements.
2.Data is arranged in a sequence manner. 2.Data is not arranged in a sequence manner.
3.Data can be traversed in a single run 3.Data cannot be traversed in a single run.
4.Implementation is easy. 4.Implementation is difficult.

5.Ex:-Array, Stack, Queue, Linked List. 5.Ex:-Tree, Graph

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

Following is the implementation of the above algorithm −


Unit 1 Introduction to Data structures

#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

The array elements after updation :


LA[0] = 1
LA[1] = 3
LA[2] = 10
LA[3] = 7
LA[4] = 8

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.

The time required by an algorithm falls under three types −


 Best Case − Minimum time required for program execution.
 Average Case − Average time required for program execution.
 Worst Case − Maximum time required for program execution.
Asymptotic Notations
Following are the commonly used asymptotic notations to calculate the running
time complexity of an algorithm.
 Ο Notation
 Ω Notation
 θ Notation
Big Oh Notation, Ο
The notation Ο(n) is the formal way to express the upper bound of an algorithm's
running time. It measures the worst-case time complexity or the longest amount of time
an algorithm can possibly take to complete.
UNIT 1: Introduction to Data Structures

For example, for a function f(n)


Ο(f(n)) = { g(n) : there exists c > 0 and n0 such that f(n) ≤ c.g(n) for all n > n0. }
Omega Notation, Ω
The notation Ω(n) is the formal way to express the lower bound of an algorithm's
running time. It measures the best case time complexity or the best amount of time an
algorithm can possibly take to complete.

For example, for a function f(n)


Ω(f(n)) ≥ { g(n) : there exists c > 0 and n0 such that g(n) ≤ c.f(n) for all n > n0. }
Theta Notation, θ
The notation θ(n) is the formal way to express both the lower bound and the upper
bound of an algorithm's running time. It is represented as follows -

θ(f(n)) = { g(n) if and only if g(n) = Ο(f(n)) and g(n) = Ω(f(n)) for all n > n0. }

You might also like