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

DSA Module 1

Uploaded by

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

DSA Module 1

Uploaded by

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

COURSE

DATA STRUCTURES & ALGORITHMS 3


TITLE CREDITS
COURSE COURSE
3CS 1006 Core L-T-P 2-0-2
CODE CATEGORY
Approval LEARNING
Version 1.0 ACM BTL-3
Details LEVEL
ASSESSMENT SCHEME
Semester
During Semester Assessment (DSA) End
Examination
MSE DSA Components Attendance SEE

10% 35% 5% 50%


This course introduces fundamental concepts of data structure and algorithms. It
includes the concepts of stacks, queues, linked lists, trees, graphs, and hash tables.
Course It also includes the different searching and sorting techniques. It also helps learn
Description writing algorithms in a step-by-step approach for solving problems with the help
of fundamental data structures.
Assessment Methods

Sl.No Assessmen Description Weightage Course Outcomes To be Duration/Time


t Type in marks Assessed line
CO CO CO CO CO
1 2 3 4 5
1 QZ Quiz 5 1 Hr
Mid Semester
2 MSE 10 1 Hr
Exam
3 AS Assignment 10 2 Hrs
Coursera
4 CC 5 10 Hrs
Course
5 MSE LAB MSE Lab 15 2 Hrs
6 AT Attendance 5 45 Hrs
Semester End-
7 SEE 50 3 Hrs
Examination
Coursera Course:
https://www.coursera.org/programs/faculty-development-
programs4ph7/learn/cs-fundamentals-2?source=search#modules
(DSA Component) Ordered Data Structures | Coursera
1.To impart the basic concepts of data structures and algorithms.
2.To understand concepts about searching and sorting techniques
Course
3.To understand basic concepts about stacks, queues, lists, trees and graphs.
Objective
4.To learn to write algorithms for solving problems with the help of
fundamental data structure
1. Demonstrate an understanding of Arrays and Linked Lists. Analyze the
time and space complexity of algorithms
2. For a given problem of Stacks and Queues student will be able to
implement it and analyse the same to determine the time and computation
complexity.
Course
3. Student will be able to implement searching and sorting algorithms and
Outcome
analyse their performance in term of Space and Time complexity.
4. Upon completion student will be able to implement different types of tree
algorithms.
5. Student will be able to implement Graph search and traversal algorithms
and determine the time and computation complexity
MODULE 1: Introduction, Arrays and Linked Lists

Introduction to data structures, Need for Data Structures.


Types of data structures: Linear and Nonlinear. Operations on
data structures: Insertion, Deletion, Traversal etc.
Arrays: One-dimensional arrays, Multidimensional arrays.
Operations on arrays. CO-1
Linked lists: Creation of single linked list, double linked list,
circular linked list, and operations on them.
Introduction to algorithms-Definition and Characteristics,
Analysis of algorithms- Asymptotic notations for time and space
complexities with examples.
• A data structure defines a way of organizing all data items that considers not only the elements
stored but also their relationship to each other. The term data structure is used to describe the
way data is stored.
• It is also used for processing, retrieving, and storing data.
What is the need of Data Structure?
Data structures are essential for two main reasons: they make the code more efficient, and they
make the code easier to understand. Some specific reasons why data structures are important
include:
1.Improved Time Complexity: Using appropriate data structures can lead to better time
complexity, making it possible to solve problems more quickly. For example, searching for an
element in a sorted array is faster than searching for it in an unsorted array.
2.Better Space Complexity: Data structures can help to reduce the amount of memory needed to
store data. For example, using a linked list instead of an array can reduce the amount of memory
needed to store the same data.
3.Efficient Data Retrieval: Data structures make it easier to retrieve specific data efficiently. For
example, a hash table can retrieve data in constant time, while searching through an unsorted array
takes linear time.
4.Better Data Management: Data structures make it easier to manage and manipulate data. For
example, a stack can be used to implement an undo functionality in an application.
5.Solving Complex Problems: Data structures can provide the foundation for efficient algorithms,
making it possible to solve complex problems in a reasonable amount of time. For example, graph
algorithms can be used to find the shortest path between two points or to find the minimum
spanning tree of a graph.
Data Structures Taxonomies
Primitive Data Structures are the basic data structures that directly
operate upon the machine instructions.

Non-primitive data structures are more complicated data structures


and are derived from primitive data structures.
They emphasize on grouping same or different data items with
relationship between each data item.
Types of
Data
Structures
Linear data structures organize their data elements in a linear
fashion, where data elements are attached one after the other.
Linear data structures are very easy to implement, since the memory of
the computer is also organized in a linear fashion.
Some commonly used linear data structures are arrays, linked lists,
stacks and queues.

In nonlinear data structures, data elements are not organized in a


sequential fashion.
Data structures like multidimensional arrays, trees, graphs, tables and
sets are some examples of widely used nonlinear data structures.
Linear Data Structures
Array:
An array is a collection of variables of the same type that are referred to through a common
name.
Operations on arrays are searching, insertion, deletion of an element.

Linked list:
linked list is a linear data structure which consist a group of nodes together represent a
sequence.
Each node is composed of data and an address (in other words, a link) to the next node in
the sequence.
The basic operations in a single linked list are:
Creation.
Insertion.
Deletion.
Traversing.
• A linked list is a fundamental data structure in computer science. It consists of nodes
where each node contains data and a reference (link) to the next node in the sequence.
• This allows for dynamic memory allocation and
efficient insertion and deletion operations compared to arrays.
Stack:
A stack is a list of elements in which an element may be inserted or deleted only at
one end, called the top of the stack.
Stacks are sometimes known as LIFO (last in, first out) lists. i.e. the last item to be
added to a stack is the first item to be removed.
The two basic operations associated with stacks are:
1. Push: is the term used to insert an element into a stack.
2. Pop: is the term used to delete an element from a stack.
Queue:
Queue is a linear data structure, in which the first element is inserted from one end called
REAR(also called tail), and the deletion of existing element takes place from the other end
called as FRONT(also called head).
This makes queue as FIFO data structure, which means that element inserted first will also be
removed first.
The following operations on queues are:
enqueue: The process of adding an element at the end of the queue is called Enqueue.
dequeue: The process of removing an element at the front of the queue is called Dequeue.
Non Linear Data Structures:
Tree:
A tree is hierarchical collection of nodes. One of the nodes, known as the root, is at the
top of the hierarchy.
Each node can have at most one link coming into it. The node where the link originates
is called the parent node.
The root node has no parent. The links leaving a node (any number of links are allowed)
point to child nodes.
Trees are recursive structures. Each child node is itself the root of a subtree. At the
bottom of the tree are leaf nodes, which have no children.

Graph:
Graph G is a pair (V, E), where V is a finite set of vertices and E is a finite set of edges.
A graph is generally displayed by following figure, in which the vertices are represented
by circles and the edges by lines.
Operations:

Traversal: accessing each node/element exactly once in the list. (This


accessing and processing is sometimes called “visiting” the node or element.).
Inserting: Adding a new node/element to the data structure.
Deleting: Removing a node/element from the data structure.
Searching: Finding the location of the desired node/element in the data
structure.
Arrays:
An array is a collection of variables of the same type that are referred to through a common name.
A specific element in an array is accessed by an index.
In C, all arrays consist of contiguous Memory locations. The lowest address corresponds to the first
element and the highest address to the last element.
The most common array is the string, which is simply an array of characters terminated by a null.
Arrays can have from one to several dimensions.

Types of Array:-
i. One-dimensional arrays
ii. Multi-dimensional arrays :
a) Two-dimensional arrays
b)Three-dimensional array and so on
One-Dimension Arrays:
A list of items can be given with one variable name using only one subscript and such a
variable is called a single-subscripted variable or a One-dimensional array.

The general form for declaring a single-dimension array is


type var_name[size];

Here, type declares the base type of the array, which is the type of each element in the array,
and size defines how many elements the array will hold.

Example: To represent a set of 5 numbers, declare the array of variable name ‘num’.
int num[5];

The computer reserves five storage locations as following.


Initializing Array:-
Syntax:-
<datatype> < arrayname>[size]={list of values};

Example:- int num[5]= {10,20,30,40,50};


Memory Map With One-Dimensional Array:

The amount of storage required to hold an array is directly related to its type and size. Size of
a single dimension array:
total bytes = sizeof(base type) × length of array;
Example: int num[5];
total bytes = 2 x 5 =10 bytes of memory is allocated to the variable ‘num’ at compile time.
OPERATIONS ON ARRAYS:
1. Retrieval of an element
2. Searching an element
3. Insertion of an element
4. Deletion of an element

1. Retrieval(Accessing) Elements of an Array:


To get or access an element from an array by using index or position is called retrieval of
an element. An element of an array is accessed by indexing the array name.
This is done by placing the index of the element within square brackets after the name of
the array.
This index number specifies the element’s position in the array.
Index number starts with 0. Thus, num[2] is not the second element of the array, but it is the
third.
Example:
void main()
{
int a[5]={10,20,3,15,25};
int i;
//retrieving first and fifth elements from array elements
printf(“first element =%d \n”,a[0]);
printf(“fifth element =%d \n”,a[4]);
//retrieving all the elements from array elements
for ( i = 0 ; i <5 ; i++ )
{
printf ( "%d \n", a[i] ) ;
}
}
2. Searching an element from an array:

The process of finding the location of a particular element in an array is called searching,
it is the method for finding a particular value in the list.

Type of searching:
1. Linear (sequential) search.
2. Binary search.

1. Linear search: linear search or sequential search is a method for finding a particular
value in a list that checks each element in sequence until the desired element is found or
the list is exhausted. The list need not be ordered.
Algorithm: Linear search()
Step-1: Read size of the array ‘N’.
Step-2: Read array elements A.
Step-3: Read the variable ‘element’ to search from the array.
Step-4: SET count=0
Step-5: SET i=0 (counter variable)
Step-6: REPEAT step-7 UNTIL i < num
Step-7: IF element = =A[i] THEN
Count =1
Step-8: SET i=i+1 (increment counter variable)
Step-9: IF count =1 THEN
PRINT “element found in the array”
ELSE PRINT “element not found in the array”
PROGRAM:
include <stdio.h> /* Linear search begins */
void main() for (i = 0; i < n ; i++)
{ {
int a[100]; if (element == a[i] )
int i, n, element, count = 0; {
printf("Enter the size of array \n"); count = 1;
scanf("%d", &n); break;
printf("Enter the array elements \n"); }
for (i = 0; i < n; i++) }
{ if (count == 1)
scanf("%d", &a[i]); printf("Element is present in the array\n");
} else
printf("Enter the element to be searched \n"); printf("Element is not present in the array\n")
scanf("%d", &element);
Output: Enter the value of num 5
Enter the elements one by one
456 78 90 40 100
Enter the element to be searched 70
Element is not present in the array

Binary Search:

• Binary Search is applied on the sorted array or list.


• In binary search, we first compare the value with the elements in the middle position of
the array. If the value is matched, then we return the value.
• If the value is less than the middle element, then it must lie in the lower half of the array
and if it's greater than the element then it must lie in the upper half of the array.
• We repeat this procedure on the lower (or upper) half of the array.
• Binary Search is useful when there are large numbers of elements in an array
#include <stdio.h>
int main()
{
int i, first, last, middle, n, search, a[100];
printf("Enter number of elements\n");
scanf("%d",&n);
printf("Enter %d integers\n", n);
for (i = 0; c < n; i++)
scanf("%d“,&a[c]);
printf("Enter value to find\n");
scanf("%d", &search);
first = 0;
last = n - 1;
middle = (first+last)/2;
while (first <= last)
{
if (a[middle] < search)
first = middle + 1;
else if (a[middle] == search)
{
printf("%d found at location %d.\n", search, middle+1);
break;
}
else
last = middle - 1;
middle = (first + last)/2;
}
if (first > last)
printf("Not found! %d is not present in the list.\n",search);
return 0;
}
The elements of the array are - 11 14 25 30 40 41 52 57 70
Element to be searched is - 40
Element is present at 5 position of array

The elements of the array are - 11 14 25 30 40 41 52 57 70


Element to be searched is - 70
Element is present at 9 position of array

The elements of the array are - 11 14 25 30 40 41 52 57 70


Element to be searched is - 46
Element is not present in the array
3. Insertion of an element:

Adding an element to an array is called insertion

Algorithm: INSERTION( )
step-1: Read size of the array ‘N’.
Step-2: Read array elements A.
FOR i=0 to N
Read A[i]
Step-3: Read the variable ‘element’ to insert in to the array.
Step-4: Read the variable ‘pos’ (‘pos’ is the position at which the element is to be inserted)
Step-5: SET i=n-1 (counter variable)
Step-6: REPEAT step-7 UNTIL i > pos-1
Step-7: A[i+1] = A[i];
Step-8: SET i=i-1 (decrement counter variable)
Step-9: PRINT “The resultant Array”
FOR i=0 to N
PRINT A[i]

Program:
#include <stdio.h>
void main()
{
int a[100], pos, i, n, value;
printf("Enter number of elements in array\n");
scanf("%d", &n);
printf("Enter the elements of the array\n", n);
for (i = 0; i< n; i++)
scanf("%d", &a[i]);
printf("Enter the location where you wish to insert an element\n");
scanf("%d", &pos);
printf("Enter the value to insert\n");
scanf("%d", &value);
for (i = n - 1; i >= pos - 1; i--)
a[i+1] = a[i];
a[pos-1] = value;
n=n+1;
printf("Resultant array after insertion is\n");
for (i = 0; i<= n; i++)
printf("%d\n", a[i]);
}
Output of program:
Enter number of elements in array Resultant array after insertion is
5 20
Enter the elements of the array 30
20 100
30 40
40 34
34 15
15
Enter the location where you wish to
insert an element
3
Enter the value to insert
100
4. Deletion of an element:

Deleting the value of an element from the array by using index position or key value is
called deletion of an element.

Algorithm: DELETION( )
Step-1: Read size of the array ‘N’.
Step-2: Read array elements A.
FOR i=0 to N
Read A[i]
Step-3: Read the variable ‘pos’ ( ‘pos’ is the position at which the element is to insert).
Step-4: IF pos > n+1 THEN PRINT “Deletion is not possible” exit(0).
Step-5: SET i=pos-1 (counter variable)
Step-6: REPEAT step-7 UNTIL i < n-1
Step-7: A[i] = A[i+1];
Step-8: SET i=i+1 (decrement counter variable)
Step-9: SET n=n-1
Step-10: PRINT “The resultant Array”
FOR i=0 to n
PRINT A[i]

Two-Dimensional Arrays:

A two -dimensional array is called an “array of one-dimensional arrays”.


Two-dimensional array is a type of array, which has finite number of rows and finite
number of columns.
The declaration form of 2-dimensional array is
Data_type Array_name [row size][column size];

The type may be any valid type supported by C.


The rule for giving the array_name is same as the ordinary variable.
The row size and column size should specify the number of rows and columns in an array.

Example: int arr[3][3];


The amount of memory allocated for 2D array in memory:
bytes = size of 1st index × size of 2nd index × sizeof(base type)

Example: Assuming 16-bit processor i.e. 2-byte for integers


int d[10][20];
memory = 10 x 20 x 2= 400 bytes allocated.
initializing two-dimensional array:
int anArray[3][3] = { { 10, 20, 4 5}, { 42, 79, 81}, { 89, 9, 36 } };
Reading & writing an 2D Array elements:
#include<stdio.h>
#include<conio.h>
void main()
{
int i, j;
int a[3][3];
printf(" reading 2D Array Elements from key board \n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d",&a[i][j]);
}
}
printf(" writing 2D Array Elements on to the screen \n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
}
Output: reading 2D Array Elements from key board
12 45 63 89 34 73 19 76 49
writing 2D Array Elements on to the screen

12 45 63
89 34 73
19 76 49
Linked List:
• A linked list is a linear collection of elements called nodes. Each node
consists two parts – the data part , pointer part (pointer part stores the
address of next node).

• Linked list is a dynamic data structure.

• A linked list is a sequence of data structures. It is also known as a linear


data structure that comprises a set of connected nodes. Each node is used
to store the data and also the address of the next node.
Types of Linked Lists

1. Single Linked List.


2. Double Linked List.
3. Circular Linked List.
1. Single Linked List
It is the most manageable type of linked list in which every node includes
some data and the address part, which means a pointer to the next node in the
series. In a singly linked list, we can perform operations like insertion,
deletion, and traversal.
Implementation of Single Linked List:

Node structure:
Each node contains two fields; a "data" field to store data, and a "next" field
which is a pointer used to store the address of next node.

struct node
{
int data;
struct node *next;
};
Insertion of a Node:
The new node can then be inserted at three different places namely:
a. Inserting a node at the beginning.
b. Inserting a node at the end.
c. Inserting a node at intermediate position.

Inserting a node at the beginning:


The following steps are to be followed to insert a new node at the beginning
of the list:
1. Get the new node using getnode().
newnode = getnode();
2. If the list is empty then start = newnode.
3. If the list is not empty, follow the steps given below:
newnode -> next = start;
start = newnode;
Insert node at position:
The following steps are followed, to insert a new node in an intermediate position in the
list:
1. Get the new node using getnode().
newnode = getnode();
2. Read the position ‘pos’
3. Store the starting address (which is in start pointer) in temp pointer. Then traverse the
temp pointer upto the specified position.
temp =start;
for(i=1; i<pos-1; i++)
{
temp = temp -> next;
}
4. After reaching the specified position, follow the steps given below:
newnode -> next = temp -> next;
temp -> next = newnode;
Deletion of a node:

Another primitive operation that can be done in a single linked list is the
deletion of a node.

Memory is to be released for the node to be deleted. A node can be deleted


from the list from three different places namely.
a. Deleting a node at the beginning.
b. Deleting a node at the end.
c. Deleting a node at intermediate position.
Deleting a node at the beginning:

The following steps are followed, to delete a node at the beginning of the list:

1. If list is empty then display ‘Empty List’ message.


IF start == NULL THEN
printf("\n Empty List’..");

2. If the list is not empty, follow the steps given below:


temp = start;
start = start -> next;
free(temp);
Deleting a node at the end:
The following steps are followed to delete a node at the end of the list:
1. If list is empty then display ‘Empty List’ message.
2. If the list is not empty, follow the steps given below:
temp = prev = start;
while(temp -> next != NULL)
{
prev=temp;
temp = temp -> next;
}
prev -> next = NULL;
free(temp);
Deleting a node at Intermediate position:
The following steps are followed, to delete a node from an intermediate position in the
list (List must contain more than two node).
1. If list is empty then display ‘Empty List’ message
2. If the list is not empty, follow the steps given below.
for(i=1;i<pos;i++)
{
prev = temp;
temp = temp -> next;
}
prev -> next = temp -> next;
free(temp);
printf("\n Node deleted..");
2. Doubly Linked List

A double linked list is a two-way list in which all nodes will have two links.
This helps in accessing both successor node and predecessor node from the given
node position.
It is Bi-directional traversing
When a node holds a data part and two addresses, it is known as a doubly-linked
list. Two addresses means a pointer to the previous node and the next node.
Each node contains three fields:
Left link.
Data.
Right link.
Creating a node for Double Linked List:
Creating a double linked list starts with creating a node. Sufficient memory
has to be allocated for creating a node.
The memory is allocated by using the malloc() function.
Inserting a node at the beginning:

The following steps are to be followed to insert a new node at the beginning
of the list:
1. Get the new node using getnode().
newnode=getnode();
2. If the list is empty then start = newnode.
3. If the list is not empty, follow the steps given below:
newnode -> right = start;
start -> left = newnode;
start = newnode;
Inserting a node at the end:
The following steps are followed to insert a new node at the end of the
list:
1. Get the new node using getnode()
newnode=getnode();
2. If the list is empty then start = newnode.
3. If the list is not empty follow the steps given below:
temp = start;
while(temp -> right != NULL)
temp = temp -> right;
temp -> right = newnode;
newnode -> left = temp;
Inserting a node at an intermediate position:

The following steps are followed, to insert a new node in an intermediate position in the list:
1. Get the new node using getnode().
newnode=getnode();
2. Read the position at which do you want to insert- ‘pos’
3. Store the starting address (which is in start pointer) in ‘temp’ and ‘prev’ pointers.
temp=start;
prev=start;
4. Traverse the temp pointer upto the specified position followed by prev pointer.
5. After reaching the specified position, follow the steps given below:
newnode -> left = temp;
newnode -> right = temp -> right;
temp -> right -> left = newnode;
temp -> right = newnode;
Deleting a node at the beginning:

The following steps are followed, to delete a node at the beginning of the list:
1. If list is empty then display ‘Empty List’ message.
2. If the list is not empty, follow the steps given below:
temp = start;
start = start -> right;
start -> left = NULL;
free(temp);
Deleting a node at the end:
The following steps are followed to delete a node at the end of the list:
1. If list is empty then display ‘Empty List’ message
2. If the list is not empty, follow the steps given below:
temp = start;
while(temp -> right != NULL)
{
temp = temp -> right;
}
temp -> left -> right = NULL;
free(temp);
Deleting a node at Intermediate position:
else
void dll_delete_mid() {
{ printf("\n Enter the position of the node
int i , pos; to delete: ");
node *temp; scanf("%d", &pos);
if(start == NULL) temp=start;
{ prev=start;
printf("\n Empty List"); for(i=1;i<pos;i++)
} {
prev = temp;
temp =temp -> right;
}
prev -> right = temp -> right;
temp -> right -> left = prev;
free(temp);
printf("\n node deleted..");
}}
3. Circular Linked List
• In a circular linked list, the last node of the series contains the address of the first node to
make a circular chain.
• A single linked list can be made a circular linked list by simply storing address of the very
first node in the link field of the last node.
• A circular linked list has no beginning and no end. It is necessary to establish a special pointer
called start pointer always pointing to the first node of the list.
• In circular linked list no null pointers are used, hence all pointers contain valid address.
Inserting a node at the beginning:

The following steps are to be followed to insert a new node at the beginning of the
circular list:
1. Get the new node using getnode().
newnode = getnode();
2. If the list is empty, assign new node as start.
start = newnode;
newnode -> next = start;
3. If the list is not empty, follow the steps given below:
temp = start;
while(temp -> next != start)
temp= temp -> next;
newnode -> next = start;
start = newnode;
temp -> next = start;
Figure shows inserting a node into the circular single linked list at the beginning
void cll_insert_beg()
{ {
node *newnode, *temp; temp = start;
newnode = getnode(); while(temp -> next != start)
if(start == NULL) temp= temp -> next;
{ newnode -> next = start;
start = newnode; start = newnode;
newnode -> next = start; temp -> next = start;
} }
else }
Inserting a node at the end:

The following steps are followed to insert a new node at the end of the list:
1. Get the new node using getnode().
newnode = getnode();
2. If the list is empty, assign new node as start.
start = newnode;
newnode -> next = start;
3. If the list is not empty follow the steps given below:
temp = start;
while(temp -> next != start)
temp = temp -> next;
temp -> next = newnode;
newnode -> next = start;
Figure shows inserting a node into the circular single linked list at the end.
Insert node at a position:
The following steps are followed, to insert a new node in an intermediate
position in the list:
1. Get the new node using getnode().
newnode = getnode();
2. Read the position ‘pos’
3. Store the starting address (which is in start pointer) in temp pointer. Then
traverse the temp pointer upto the specified position.
temp =start;
for(i=1; i<pos-1; i++)
{
temp = temp -> next;
}
4. After reaching the specified position, follow the steps given below:
newnode -> next = temp -> next;
temp -> next = newnode;
Deleting a node at the beginning:

The following steps are followed, to delete a node at the beginning of the list:
1. If the list is empty, display a message ‘Empty List’.
2. If the list is not empty, follow the steps given below:
last = temp = start;
while(last -> next != start)
{ last= last -> next; }
start = start -> next;
last -> next = start;
free(temp)
3. After deleting the node, if the list is empty then start = NULL.

[before deleting check if(start->next==NULL) that is list contains only one node. And
after deleting that node list becomes empty. That is start =NULL]
Figure shows deleting a node at the beginning of a circular single linked list.
Deleting a node at the end:

The following steps are followed to delete a node at the end of the list:
1. If the list is empty, display a message ‘Empty List’.
2. If the list is not empty, follow the steps given below:
temp = start;
prev = start;
while(temp -> next != start)
{
prev=temp;
temp = temp -> next;
}
prev -> next = start;
3. After deleting the node, if the list is empty then start = NULL.
Figure shows deleting a node at the end of a circular single linked list.
Deleting a node at Intermediate position:

The following steps are followed, to delete a node from an intermediate position in the
list (List must contain more than two node).
1. If list is empty then display ‘Empty List’ message
2. If the list is not empty, follow the steps given below.
for(i=1;i<pos;i++)
{
prev = temp;
temp = temp -> next;
}
prev -> next = temp -> next;
free(temp);
printf("\n Node deleted..");
void delete_mid()
{
{
printf("\n Enter position of node to
int i, pos;
delete: ");
node *temp, *prev;
scanf("%d", &pos);
if(start == NULL)
temp = prev = start;
{
for(i=1;i<pos;i++)
printf("\n Empty List..");
{
return ;
prev = temp;
}
temp = temp -> next;
else
}
prev -> next = temp -> next;
free(temp);
printf("\n Node deleted..");
}
}
Introduction to algorithms
Definition
An algorithm is a well-defined sequence of instructions designed to
perform a specific task or solve a particular problem. It is a step-by-
step procedure that takes an input, processes it through a series of
operations, and produces an output.
Characteristics of an Algorithm

Well-Defined Inputs: If an algorithm says to take inputs, it should be well-defined inputs. It


may or may not take input.
Well-Defined Outputs: The algorithm must clearly define what output will be yielded and it
should be well-defined as well. It should produce at least 1 output.
Clear and Unambiguous: The algorithm should be unambiguous. Each of its steps should be
clear in all aspects and must lead to only one meaning.
Finite-ness: The algorithm must be finite, i.e. it should terminate after a finite time.
Feasible: The algorithm must be simple, generic, and practical, such that it can be executed
with the available resources. It must not contain some future technology or anything.
Language Independent: The Algorithm designed must be language-independent, i.e. it must
be just plain instructions that can be implemented in any language, and yet the output will be
the same, as expected.
Analysis of algorithms
Algorithm analysis is an important part of computational complexity theory, which
provides theoretical estimation for the required resources of an algorithm to solve a
specific computational problem. Analysis of algorithms is the determination of the
amount of time and space resources required to execute it.

Types of Algorithm Analysis:


Best case
Worst case
Average case

Best case: Define the input for which algorithm takes less time or minimum time.
In the best case calculate the lower bound of an algorithm.
Example: In the linear search when search data is present at the first location of
large data then the best case occurs.
Worst Case: Define the input for which algorithm takes a long time or maximum
time. In the worst calculate the upper bound of an algorithm.
Example: In the linear search when search data is not present at all then the worst
case occurs.

Average case: In the average case take all random inputs and calculate the
computation time for all inputs. And then we divide it by the total number of
inputs.

Average case = all random case time / total no of case


Asymptotic Notation
➢ Asymptotic notation is a fundamental concept in computer science and
mathematics that allows us to describe the behavior of algorithms and
functions as their input size approaches infinity.
➢ It provides a simplified way to analyze and compare the efficiency of
algorithms, focusing on their growth rates without being concerned with
constant factors and lower-order terms.

Three Main Asymptotic Notation


➢ Ο Big Oh Notation
➢ Ω OmegaNotation
➢ θ Theta Notation
Big-O Notation (O-notation)
Big-O notation represents the upper bound of the running time of
an algorithm. Thus, it gives the worst-case complexity of an algorithm.
Omega Notation (Ω-notation)
Omega notation represents the lower bound of the running time of an
algorithm. Thus, it provides the best case complexity of an algorithm.
Theta Notation (Θ-notation)
Theta notation encloses the function from above and below. Since
it represents the upper and the lower bound of the running time of
an algorithm, it is used for analyzing the average-case complexity of
an algorithm.
Space Complexity:
Amount of memory an algorithm needs to run to completion.
Space needed by algorithms is a combination of two components:

Fixed Part includes the instruction space (i.e. Code, Simple Variables,
Fixed components, Constants etc.

Variable Part includes space needed by component variables whose size


is dependent upon particular problem instance.

Time Complexity: Amount of time an algorithm needs to run to completion.


We can have three cases to analyze an algorithm.
Doubts?????

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