Lab Manual - DSA
Lab Manual - DSA
LAB MANUAL
Name Of Course Data Structures and Algorithms Lab
Semester III
Objectives
Prerequisites, if Any
NA
Course Outcomes 3
Syllabus/List of Programs 5
Course Outcomes:
CO 3 Implement the sorting and searching techniques for appropriate real-world problems
Apply the appropriate data structures, algorithms, and realizations to solve simple to
CO 5
complex real-world issues.
Mapping Between Course Outcome and Program Outcomes:
Course
Learning PO PSO PSO PSO
PO1 PO3 PO4 PO5 PO6 PO7 PO8 PO9 PO10 PO11 PO12
Outcomes 2 1 2 3
(CLOs)
CO1 2 2 2 2 2 2 3 3 3 3 3 3
CO2 2 2 1 2 2 2 3 3 3 3 3 2 3 3
CO3 2 2 2 2 2 3 3 3 3 3 3
CO4 2 3 2 2 3 3 3 3 3 3 2 3
CO5 2 2 2 3 3 3 3 3 3 3 3 2 3
Syllabus/List of Programs:
1. Basic Program
1.1 Write a Java program that prints the following pattern: Hello, World!
2. Array
2.3 Write a program to merge two arrays of the same size sorted in descending
order.
3. String Manipulation
3.1 Perform basic string operations like concatenation and substring extraction in
Java.
3.2 Implement dynamic strings in Java and analyze their memory representation.
4.1 Perform basic stack operations (push, pop, display) using arrays in Java.
6.1 Write a Java program that converts an arithmetic expression from infix
notation to postfix notation.
6.3 Create and perform basic operations on Binary Search Trees (BSTs) in Java.
7.1 Write a program to create a binary tree where the user inputs the values for
each node. After building the tree, print the values in an in-order traversal.
7.2 Write a program to find the height of a binary tree. The user will input the
values for each node, and the program will calculate and display the height of the
tree.
7.3 Write a program to count the number of leaf nodes in a binary tree. The user
will provide the values for each node, and the program will count and display the
number of leaf nodes.
8. Searching Algorithms
8.1 Write a Java program that performs an unordered linear search. The program
should take an array of integers and a target integer as input, then determine
whether the target exists in the array and print the index if found.
8.2 Write a Java program that performs a sorted linear search. The program
should take a sorted array of integers and a target integer as input, then
determine whether the target exists in the array and print the index if found.
8.3 Write a Java program that performs a binary search on a sorted array using
recursion. The program should take a sorted array of integers and a target
integer as input, then determine whether the target exists in the array and print
the index if found.
9. Sorting Algorithms
9.3 Implement selection sort and compare with other basic sorting algorithms in Java.
Practical 1.1 Write a Java program that prints the following pattern: Hello, World!
System.out.println("Hello, World!");
Practical 2.1 Write a program to store elements in an array and print them.
Practical 2.2 Write a program to find the sum of all array elements.
int sum = 0;
sum += num;
}
System.out.println("Sum of array elements: " + sum);
Practical 2.3 Write a program to merge two arrays of the same size sorted in descending
order.
import java.util.Arrays;
import java.util.Collections;
Arrays.sort(mergedArray, Collections.reverseOrder());
Practical 3.1 Perform basic string operations like concatenation and substring extraction
in Java.
// Concatenation
// Substring extraction
Practical 3.2 Implement dynamic strings in Java and analyze their memory
representation.
dynamicString.append(" World!");
permute(str, "");
}
public static void permute(String str, String prefix) {
if (str.length() == 0) {
System.out.println(prefix);
} else {
Practical 4.1 Perform basic stack operations (push, pop, display) using arrays in Java.
class Stack {
maxSize = size;
top = -1;
stackArray[++top] = value;
} else {
System.out.println("Stack Overflow");
System.out.println(stackArray[i]);
stack.push(1);
stack.push(2);
stack.push(3);
stack.display();
class Node {
int data;
Node next;
Node(int data) {
this.data = data;
this.next = null;
class LinkedList {
Node head;
if (head == null) {
head = newNode;
} else {
temp = temp.next;
temp.next = newNode;
temp = temp.next;
list.add(1);
list.add(2);
list.add(3);
list.display();
class DoublyNode {
int data;
DoublyNode next;
DoublyNode prev;
DoublyNode(int data) {
this.data = data;
this.next = null;
this.prev = null;
}
class DoublyLinkedList {
DoublyNode head;
if (head == null) {
head = newNode;
} else {
temp = temp.next;
temp.next = newNode;
newNode.prev = temp;
System.out.print("Forward: ");
temp = temp.next;
System.out.println();
temp = temp.next;
System.out.print("Backward: ");
temp = temp.prev;
System.out.println();
dll.insert(1);
dll.insert(2);
dll.insert(3);
dll.displayForward();
dll.displayBackward();
import java.util.Stack;
switch (ch) {
case '+':
case '-':
return 1;
case '*':
case '/':
return 2;
case '^':
return 3;
return -1;
}
// Convert infix to postfix
char c = expression.charAt(i);
if (Character.isLetterOrDigit(c)) {
result.append(c);
else if (c == '(') {
stack.push(c);
else if (c == ')') {
result.append(stack.pop());
stack.pop();
result.append(stack.pop());
}
stack.push(c);
while (!stack.isEmpty()) {
result.append(stack.pop());
return result.toString();
class TreeNode {
int data;
TreeNode(int data) {
this.data = data;
left = right = null;
class BinaryTree {
TreeNode root;
// Preorder Traversal
preorder(node.left);
preorder(node.right);
// Inorder Traversal
inorder(node.left);
inorder(node.right);
// Postorder Traversal
postorder(node.right);
System.out.print("Preorder: ");
tree.preorder(tree.root);
System.out.print("\nInorder: ");
tree.inorder(tree.root);
System.out.print("\nPostorder: ");
tree.postorder(tree.root);
Practical 6.3 Create and Perform Basic Operations on Binary Search Trees (BSTs) in Java
class BSTNode {
int key;
BSTNode left, right;
key = item;
class BinarySearchTree {
BSTNode root;
BinarySearchTree() {
root = null;
if (root == null) {
return root;
}
if (key < root.key) {
return root;
return root;
// Delete a key
} else {
root.key = minValue(root.right);
return root;
root = root.left;
minValue = root.key;
return minValue;
}
// Inorder traversal
void inorder() {
inorderRec(root);
if (root != null) {
inorderRec(root.left);
inorderRec(root.right);
bst.insert(50);
bst.insert(30);
bst.insert(20);
bst.insert(40);
bst.insert(70);
bst.insert(60);
bst.insert(80);
System.out.println("Deleting 20");
bst.delete(20);
bst.inorder();
import java.util.Scanner;
class Node {
int data;
Node(int data) {
this.data = data;
class BinaryTree {
Node root;
if (root == null) {
} else {
} else {
return root;
// In-order traversal
if (root != null) {
inOrderTraversal(root.left);
inOrderTraversal(root.right);
int n = scanner.nextInt();
System.out.println("Enter values:");
tree.inOrderTraversal(tree.root);
import java.util.Scanner;
class BinaryTreeHeight {
Node root;
if (node == null) {
return -1;
} else {
int leftHeight = findHeight(node.left);
int n = scanner.nextInt();
System.out.println("Enter values:");
class BinaryTreeLeafCount {
Node root;
int n = scanner.nextInt();
System.out.println("Enter values:");
}
}
import java.util.Scanner;
int u = scanner.nextInt();
int v = scanner.nextInt();
adjMatrix[u][v] = 1;
System.out.println("Adjacency Matrix:");
System.out.println();
import java.util.Scanner;
int u = scanner.nextInt();
int v = scanner.nextInt();
adjMatrix[u][v] = 1;
int degree = 0;
degree += adjMatrix[vertex][i];
import java.util.*;
visited[node] = true;
dfs(neighbor);
int u = scanner.nextInt();
int v = scanner.nextInt();
graph.get(u).add(v);
dfs(0);
if (!nodeVisited) {
isConnected = false;
break;
if (isConnected) {
} else {
import java.util.Scanner;
public class UnorderedLinearSearch {
int n = scanner.nextInt();
System.out.println("Enter elements:");
arr[i] = scanner.nextInt();
if (arr[i] == target) {
index = i;
break;
}
if (index != -1) {
} else {
import java.util.Scanner;
int n = scanner.nextInt();
arr[i] = scanner.nextInt();
if (arr[i] == target) {
index = i;
break;
break;
if (index != -1) {
} else {
import java.util.Scanner;
return -1;
if (arr[mid] == target) {
return mid;
return binarySearch(arr, target, left, mid - 1); // Search in the left half
} else {
int n = scanner.nextInt();
arr[i] = scanner.nextInt();
if (result != -1) {
} else {
import java.util.Scanner;
int n = scanner.nextInt();
int[] arr = new int[n];
System.out.println("Enter elements:");
arr[i] = scanner.nextInt();
arr[j + 1] = temp;
System.out.println("Sorted array:");
}
Practical 10.2 Insertion Sort
import java.util.Scanner;
int n = scanner.nextInt();
System.out.println("Enter elements:");
arr[i] = scanner.nextInt();
int j = i - 1;
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
System.out.println("Sorted array:");
import java.util.Scanner;
int n = scanner.nextInt();
System.out.println("Enter elements:");
arr[i] = scanner.nextInt();
int minIdx = i;
minIdx = j;
arr[minIdx] = arr[i];
arr[i] = temp;
System.out.println("Sorted array:");