Data Structures and Algorithm-SEDA-225-Lab Manual
Data Structures and Algorithm-SEDA-225-Lab Manual
Preface
This lab manual has been prepared to facilitate the students of software engineering in studying
and implementing linear and nonlinear data structures and algorithms. The students will also
analyse the best and most efficient data structures and algorithms to solve problems. The
students can learn and implement several searching and sorting algorithms. After the
completion of this course students will be able to practically implement the concepts of data
structure and algorithms on real life applications.
Tools/ Technologies
• C++ Language
• Dev C++
2
BS (Software Engineering) 2023
TABLE OF CONTENTS
Preface ....................................................................................................................................... 2
Tools/ Technologies .................................................................................................................. 2
LAB 1: Arrays and its Operations ......................................................................................... 4
LAB 2: Searching and Sorting ................................................................................................ 7
LAB 3: Recursion ................................................................................................................... 12
LAB 4: Array Based Stack Implementation ....................................................................... 14
LAB 5: Array Based Linear Queue Implementation ......................................................... 16
LAB 6: Array based Circular Queue Implementation ....................................................... 18
Lab 7: Insertion in Linked List............................................................................................. 21
Lab 8: Deletion from Linked List ......................................................................................... 23
Lab 9: Linked List based Stack and Queue ........................................................................ 26
Lab 10: Doubly Linked List Insertion and Deletion ........................................................... 29
LAB 11: Reverse using Linked List ..................................................................................... 31
LAB 12: Sorting using Divide and Conquer Approach ..................................................... 33
LAB 13: Binary Search Tree and its operations ................................................................. 36
LAB 14: Graph....................................................................................................................... 39
3
BS (Software Engineering) 2023
Objectives
Array
• Group of consecutive memory locations with same name and type.
• The memory locations in the array are known as the elements of array.
Lab Tasks
Program:
Declare an array, takes values from the user and display it.
4
BS (Software Engineering) 2023
Program:
Declare an array, initialize the values and display it.
Linear Search:
Linear search is a very simple search algorithm. In this type of search, a sequential search is
made over all items one by one. Every item is checked and if a match is found then that
particular item is returned, otherwise the search continues till the end of the data collection.
Algorithm:
1. Start
2. Set i=0
3. while i<size
4. if arr[i] == item
5. return i and goto step 10
6. end if
7. Set i=i+1
8. end while
9. return -1
10. End
5
BS (Software Engineering) 2023
6
BS (Software Engineering) 2023
Objectives
• To learn about Binary Search, Bubble Sort, Selection Sort and Insertion Sort
Binary Search
• Binary search can be implemented only on a sorted list of items.
• It works on the principle of divide and conquer.
Algorithm:
A ← sorted array
n ← size of array
x ← value to be searched
Set beg = 0
Set end = n-1
while beg <= end
set mid = (beg + end) / 2
if A[mid] = x
EXIT: x found at location mid
if A[mid] < x
set beg = mid + 1
if A[mid] > x
set end = mid - 1
end while
Bubble Sort
In an exchange sort, adjacent elements of the list are exchanged with one another so that the
list becomes sorted.
Example:
Pass 1
7
BS (Software Engineering) 2023
Pass 2
Pass 3
Pass 4
Algorithm:
Start
Input arr
n -> length of arr
for (i = 0; i < n-1; i++)
for (j = 0; j < n-i-1; j++)
if (arr[j] > arr[j+1])
swap(arr[j], arr[j+1])
end if
end for
end for
Print arr
End
8
BS (Software Engineering) 2023
Selection Sort
• Divides the array into two parts:
a) Already sorted
b) Unsorted
• On each pass, finds the smallest of the unsorted elements, and swaps it into its correct
place, thereby increasing the number of sorted elements by one.
Example
9
BS (Software Engineering) 2023
Algorithm
Start
Input arr
n -> length of arr
for(i=0; i<n-1; i++){
for(j=i+1; j<n; j++){
if(arr[i] > arr[j])
swap(arr[i], arr[j])
end if
end for
end for
Print arr
End
Insertion Sort
Works like someone who “inserts” one more card at a time into a hand of cards that are already
sorted.
Example
10
BS (Software Engineering) 2023
Algorithm
INSERTION-SORT(A)
for i = 1 to n
key ← A [i]
j←i–1
while j > = 0 and A[j] > key
A[j+1] ← A[j]
j←j–1
End while
A[j+1] ← key
End for
Lab Tasks
Implement Binary Search, Bubble Sort, Selection Sort and Insertion Sort in C++.
11
BS (Software Engineering) 2023
LAB 3: Recursion
Objectives
To learn about Binary Search and Factorial using recursion.
Recursion
The process in which a function calls itself directly or indirectly is called recursion and the
corresponding function is called as recursive function.
Factorial:
int fact(int n)
{
if (n==0 || n==1)
return 1;
else
return n * fact(n-1);
}
12
BS (Software Engineering) 2023
Binary Search:
int BinarySearch(int arr[], int num, int beg, int end)
{
int mid;
if (beg > end)
return 0;
else {
mid = (beg + end) / 2;
if(arr[mid] == num)
return mid;
else if (num > arr[mid])
BinarySearch (arr, num, mid+1, end);
else if (num < arr[mid])
BinarySearch (arr, num, beg, mid-1);
}
}
Lab Tasks
Implement Binary Search and Factorial using Recursion in C++.
13
BS (Software Engineering) 2023
Objectives
To learn about array based stack implementation
Stack
• It is an ordered group of homogeneous items.
• Items are added to and removed from the top of the stack
LIFO property: Last In, First Out
• The last item added would be the first to be removed
Lab Task
Header file:
14
BS (Software Engineering) 2023
Implementation File:
Main File:
15
BS (Software Engineering) 2023
Objectives
To learn about array based linear queue implementation
Queue
• It is an ordered group of homogeneous items.
• Queues have two ends:
Items are added at one end.
Items are removed from the other end.
• FIFO property: First In, First Out
• The item added first is also removed first
Lab Task
16
BS (Software Engineering) 2023
17
BS (Software Engineering) 2023
Objectives
To learn about array based circular queue implementation
Circular Queue
Circular Queue is a linear data structure in which the operations are performed based on FIFO
(First In First Out) principle and the last position is connected back to the first position to make
a circle.
Lab Task
18
BS (Software Engineering) 2023
19
BS (Software Engineering) 2023
20
BS (Software Engineering) 2023
Objectives
To learn about linked list operations including insertion from beginning, middle, end and
display.
Linked List
• Linked List is a linear data structure which is made up of nodes connected by pointers.
• Dynamic Data Structure
• Each node has two parts:
Data: To store information (data)
Link: To store address of next node
Lab Task
21
BS (Software Engineering) 2023
22
BS (Software Engineering) 2023
Objectives
To learn about linked list operations including deletion from beginning, middle, end and
display.
Linked List
• Linked List is a linear data structure which is made up of nodes connected by pointers.
• Dynamic Data Structure
• Each node has two parts:
Data: To store information (data)
Link: To store address of next node
Lab Task
23
BS (Software Engineering) 2023
24
BS (Software Engineering) 2023
25
BS (Software Engineering) 2023
Objectives
To learn about linked list based stack and queue implementation.
Lab Task
Linked List Based Stack Implementation
26
BS (Software Engineering) 2023
27
BS (Software Engineering) 2023
28
BS (Software Engineering) 2023
Objectives
To learn about Doubly Linked List Insertion and Deletion.
Lab Task
29
BS (Software Engineering) 2023
30
BS (Software Engineering) 2023
Objectives
To learn about reverse display using linked list.
Lab Task
31
BS (Software Engineering) 2023
32
BS (Software Engineering) 2023
Objectives
To learn about divide and conquer algorithms implementation (Merge Sort, Quick Sort)
Merge Sort
The Merge Sort algorithm is a sorting algorithm that is considered as an example of the divide
and conquer strategy. So, in this algorithm, the array is initially divided into two equal halves
and then they are combined in a sorted manner. We can think of it as a recursive algorithm that
continuously splits the array in half until it cannot be further divided. This means that if the
array becomes empty or has only one element left, the dividing will stop, i.e. it is the base case
to stop the recursion. If the array has multiple elements, we split the array into halves and
recursively invoke the merge sort on each of the halves. Finally, when both the halves are
sorted, the merge operation is applied. Merge operation is the process of taking two smaller
sorted arrays and combining them to eventually make a larger one.
33
BS (Software Engineering) 2023
General Algorithm
if the list is of a size greater than 1
{
a. Divide the list into two sublists.
b. Merge sort the first sublist.
c. Merge sort the second sublist.
d. Merge the first sublist and the second sublist.
}
Quick Sort
QuickSort is a Divide and Conquer algorithm. It picks an element as pivot and partitions the
given array around the picked pivot. There are many different versions of quickSort that pick
pivot in different ways.
• Always pick first element as pivot.
• Always pick last element as pivot (implemented below)
• Pick a random element as pivot.
• Pick median as pivot.
The key process in quickSort is partition(). Target of partitions is, given an array and an element
x of array as pivot, put x at its correct position in sorted array and put all smaller elements
(smaller than x) before x, and put all greater elements (greater than x) after x. All this should
be done in linear time.
34
BS (Software Engineering) 2023
General Algorithm
if (the list size is greater than 1)
{
a. Partition the list into two sublists, say lowerSublist and upperSublist.
b. Quick sort lowerSublist.
c. Quick sort upperSublist.
d. Combine the sorted lowerSublist and sorted upperSublist.
}
35
BS (Software Engineering) 2023
Objectives
To learn about Binary Search Tree and its operations and traversal.
Lab Task
36
BS (Software Engineering) 2023
37
BS (Software Engineering) 2023
38
BS (Software Engineering) 2023
Objectives
To learn about graph and basic algorithms implementation
Graph
• A data structure that consists of a set of nodes (vertices) and a set of edges between the
vertices.
• The set of edges describes relationships among the vertices.
Lab Task
Adjacency Matrix
Adjacency Matrix is a 2D array of size V x V where V is the number of vertices in a graph. Let
the 2D array be adj[][], a slot adj[i][j] = 1 indicates that there is an edge from vertex i to vertex
j. Adjacency matrix for undirected graph is always symmetric. Adjacency Matrix is also used
to represent weighted graphs. If adj[i][j] = w, then there is an edge from vertex i to vertex j
with weight w.
39
BS (Software Engineering) 2023
Implementation:
Adjacency List
An Adjacency list is an array consisting of the address of all the linked lists. The first node of
the linked list represents the vertex and the remaining lists connected to this node represents
the vertices to which this node is connected. This representation can also be used to represent
a weighted graph. The linked list can slightly be changed to even store the weight of the edge.
Example:
0: 1, 4
1: 0, 4, 2, 3
2: 1, 3
3: 1,4, 2
4: 3, 0,1
Graph Searching
• Problem: Find if there is a path between two vertices of the graph (e.g., Austin and
Washington)
• Methods: Depth-First-Search (DFS) or Breadth-First-Search (BFS)
Depth First Search
Depth-first traversal of a graph is roughly analogous to preorder traversal of an ordered tree.
Suppose that the traversal has just visited a vertex v, and let w1;w2; : : : ;wk be the vertices
adjacent to v. Then we shall next visit w1 and keep w2; : : : ;wk waiting. After visiting w1, we
traverse all the vertices to which it is adjacent before returning to traverse w2; : : : ;wk .
40
BS (Software Engineering) 2023
Algorithm
for all v in V[G] do
visited[v] := false
S := EmptyStack
Push(S,s)
while not Empty(S) do
u := Pop(S)
if not visited[u] then
visted[u] := true
for all w in Adj[u] do
if not visited[w] then
Push(S,w)
Algorithm
for all v in V[G] do
visited[v] := false
Q := EmptyQueue
Enqueue(Q,s)
while not Empty(Q) do
u := Dequeue(Q)
if not visited[u] then
visted[u] := true
for all w in Adj[u] do
if not visited[w] then
Enqueue(Q,w)
41