All Final
All Final
Lab-Report
TABLE OF CONTENTS
1 1 ARRAYS
d. Sorting of strings
j. Matrix Multiplication
3 3 Sorting Algorithms
a. Bubble Sort
b. Insertion Sort
c. Selection Sort
d. Merge Sort
e. Quick Sort
TOTAL MARKS
AVERAGE
Aim:
ALGORITHM:
Step 1: Start the Program.
Step 2: Get the size of the array from the user as input.
Step 3: Get the array elements based on the array size using for() loop.
Step 5: Iterate through each element of the array and add each element to the variable
sum.
Source Code:
import java.io.InputStreamReader;
class Main {
int sum=0;
arr[i]= Integer.parseInt(b.readLine());
sum+=arr[i];
}
OUTPUT:
RESULT:
Thus all the JAVA programs to practice the implementation and usage of arrays have been
successfully executed
Aim:
To write a JAVA program to find the Single element among the doubles from the array.
ALGORITHM:
Step 2: Get the size of the array from the user as input.
Step 4: Get the array elements from the user and store them in the array.
Step 5: Initialize a variable x to store the value that occurs only once in the array.
Step 6: Start iterating through the array. For each element at index i:
For each element at index j (starting from i+1), compare the elements.
Step 8: Display the value stored in x which is the element that occurs once in the array.
SOURCE CODE:
import java.io.InputStreamReader;
class Main {
int i, j, x=0;
arr[i]= Integer.parseInt(b.readLine());
int count=0;
if (arr[i] == arr[j]){
count++;
}
if(count == 0){
x = arr[i];
break;
OUTPUT:
RESULT:
Thus all the JAVA programs to practice the implementation and usage of arrays have been
successfully executed
Aim:
To write a JAVA program to Rearrange the array in that even positioned are greater than
odd.
ALGORITHM:
Step 2: Get the size of the array from the user as input.
Step 4: Get the array elements from the user and store them in the array.
Step 5: Iterate through the array with the outer loop i incrementing by 2 (i.e., at even
positions):
Inside this loop, iterate through the rest of the array starting from i+1 and
incrementing by 2 (i.e., considering only elements at odd positions).
If the element at index i is greater than the element at index j, swap the values of
arr[i] and arr[j].
Step 6: After sorting the elements at even positions to ensure they are greater than the
odd-positioned elements, print the modified array.
SOURCE CODE:
import java.io.InputStreamReader;
class Main {
arr[i]= Integer.parseInt(b.readLine());
arr[i] = arr[j];
arr[j] = temp;
System.out.println(arr[i]);
OUTPUT:
RESULT:
Thus all the JAVA programs to practice the implementation and usage of arrays have been
successfully executed
Aim:
To write a JAVA program to Sort a string array without using the sort () function.
ALGORITHM:
Step 2: Prompt the user to input the size of the string array.
Step 4: Prompt the user to enter the strings, and store them in the array.
Step 6: Perform Bubble Sort to arrange the strings in ascending lexicographical order:
If the string at the current position is lexicographically greater than the next string,
swap them.
SOURCE CODE:
import java.io.InputStreamReader;
class Main {
a[i]= b.readLine();
System.out.println("Before sorting:");
for(String i:a){
System.out.println(i);
if(a[j].compareTo(a[j+1])> 0){
a[j] = a[j+1];
a[j+1]= temp;
System.out.println("After Sorting:");
for (String i : a) {
System.out.println(i);
OUTPUT:
RESULT:
Thus all the JAVA programs to practice the implementation and usage of arrays have been
successfully executed
Aim:
To write a JAVA program to Rotate the array to the left by n positions.
ALGORITHM:
Step 2: Prompt the user to input the size of the string array.
Step 4: Prompt the user to enter the strings, and store them in the array.
Step 6: Prompt the user to input the number of positions to rotate the array.
Step 7: Adjust the number of positions to rotate (r) by performing modulo operation: r = r %
size.
Step 8: Reverse the first r elements of the array using the helper function reverse.
Step 9: Reverse the remaining elements (from index r to the last element) of the array using
the helper function reverse.
Step 10: Reverse the entire array (from the first element to the last) using the helper
function reverse.
SOURCE CODE:
import java.io.InputStreamReader;
class Main {
a[i]= b.readLine();
System.out.println("Before Rotation:");
for(String i:a){
System.out.println(i);
int r= Integer.parseInt(b.readLine());
r = r % size;
reverse(a, 0, r-1);
reverse(a, r, size-1);
reverse(a, 0, size-1);
System.out.println("After Rotation:");
for(String i:a){
System.out.println(i);
while(low< high){
a[low]= a[high];
a[high] = temp;
low++;
high--;
}
OUTPUT:
RESULT:
Thus all the JAVA programs to practice the implementation and usage of arrays have been
successfully executed
Aim:
To write a JAVA program to find out whether palindrome or not for the string array.
Algorithm:
i. Compare the character at position i (from the start) with the character at position z -
1 - i (from the end).
ii. If the characters do not match, set y = false and exit the loop.
SOURCE CODE:
import java.io.InputStreamReader;
String x= b.readLine();
Boolean y= true;
int z= x.length();
if(x.charAt(i) != x.charAt(z-1)){
y= false;
break;
else
OUTPUT:
RESULT:
Thus all the JAVA programs to practice the implementation and usage of arrays have been
successfully executed
Aim:
To write a JAVA program to find the frequency of each element in the array.
Algorithm:
3. Initialize Variables:
i. Compare the character at position i (from the start) with the character at position z -
1 - i (from the end).
ii. If the characters do not match, set y = false and exit the loop.
SOURCE CODE:
import java.io.InputStreamReader;
import java.io.IOException;
int n = Integer.parseInt(br.readLine());
visit[i] = false;
System.out.println("Element Frequencies:");
if (visit[i]) {
continue;
int count = 1;
if (arr[i] == arr[j]) {
count++;
visit[j] = true;
OUTPUT:
RESULT:
Thus all the JAVA programs to practice the implementation and usage of arrays have been
successfully executed
Aim:
ALGORITHM:
SOURCE CODE:
import java.io.InputStreamReader;
import java.io.IOException;
arr[i][j] = (i + 1) * (j + 1);
System.out.println("Generated 2D Array:");
System.out.println();
OUTPUT:
RESULT:
Thus all the JAVA programs to practice the implementation and usage of arrays have been
successfully executed
i) Counting the even numbers in a nD array
Aim:
ALGORITHM:
o Prompt the user to enter values for each row in the current layer.
SOURCE CODE
import java.io.InputStreamReader;
import java.io.IOException;
if (arr[i][j][k] % 2 == 0) {
count++;
OUTPUT:
RESULT:
Thus all the JAVA programs to practice the implementation and usage of arrays have been
successfully executed
j) Matrix Multiplication
Aim:
ALGORITHM:
Prompt the user to input the number of columns n for Matrix A and rows n for Matrix
B (since the number of columns of Matrix A should be equal to the number of rows
of Matrix B).
Declare Matrix C with dimensions [m][p] (to store the result of the multiplication).
o Prompt the user to enter the elements for the row, and split the input string into an
array.
o For each column j from 0 to n - 1, assign the values from the input array to A[i][j].
o Prompt the user to enter the elements for the row, and split the input string into an
array.
o For each column j from 0 to p - 1, assign the values from the input array to B[i][j].
Initialize C[i][j] to 0.
SOURCE CODE:
import java.io.InputStreamReader;
import java.io.IOException;
int m = Integer.parseInt(br.readLine());
System.out.print("Enter number of columns for Matrix A (rows for Matrix B): ");
int n = Integer.parseInt(br.readLine());
int p = Integer.parseInt(br.readLine());
A[i][j] = Integer.parseInt(input[j]);
B[i][j] = Integer.parseInt(input[j]);
System.out.println();
OUTPUT:
RESULT:
Thus all the JAVA programs to practice the implementation and usage of arrays have been
successfully executed
i)Store & display library books categorized by sections and shelves 3D array
Aim:
To write a JAVA program to Store and display lib books categorized by sections and
shelves 3D array.
ALGORITHM:
import java.io.InputStreamReader;
import java.io.IOException;
int S = Integer.parseInt(br.readLine());
int SH = Integer.parseInt(br.readLine());
int B = Integer.parseInt(br.readLine());
books[i][j][k] = br.readLine();
OUTPUT:
RESULT:
Thus, all the JAVA programs to practice the implementation and usage of Arrays
have been successfully executed.
AIM:
To implement a Singly Linked List in Java that allows performing basic operations such
as:
4. Updating an element.
Algorithm:
1. Insert First:
Create a new node, point it to the current head, then update head.
2. Insert Last:
Create a new node, traverse to the last node, link it to the new node.
3. Insert at Index:
Traverse to the (index - 1) node, link the new node in between.
4. Delete First:
Update head to head.next.
5. Delete Last:
Traverse to second last node, set its next to null.
6. Delete at Index:
Traverse to (index - 1) node, skip the next node in the link.
7. Search:
Traverse the list, check if any node's data matches the key.
8. Update:
Traverse and replace the value of the matching node.
9. Get Size:
Traverse the list, count nodes, return count.
10. Display:
Traverse from head, print each node’s data.
SOURCE CODE:
import java.io.InputStreamReader;
import java.io.IOException;
class Node {
int data;
Node next;
this.data = data;
this.next = null;
Node head;
int size = 0;
head = newNode;
size++;
if (head == null) {
head = newNode;
} else {
temp = temp.next;
temp.next = newNode;
size++;
System.out.println("Invalid Index!");
return;
if (index == 0) {
insertFirst(data);
return;
temp = temp.next;
newNode.next = temp.next;
temp.next = newNode;
size++;
if (head == null) {
System.out.println("List is empty!");
return;
head = head.next;
size--;
if (head == null) {
System.out.println("List is empty!");
return;
}
if (head.next == null) {
head = null;
} else {
temp = temp.next;
temp.next = null;
size--;
System.out.println("Invalid Index!");
return;
if (index == 0) {
deleteFirst();
return;
temp = temp.next;
temp.next = temp.next.next;
size--;
if (temp.data == key) {
return true;
temp = temp.next;
return false;
if (temp.data == oldVal) {
temp.data = newVal;
return;
temp = temp.next;
}
public int getSize() {
return size;
if (head == null) {
System.out.println("List is empty.");
return;
temp = temp.next;
System.out.println("NULL");
while (true) {
System.out.println("\nChoose an operation:");
System.out.println("7. Search");
System.out.println("8. Update");
System.out.println("10. Display");
System.out.println("11. Exit");
switch (choice) {
case 1:
list.insertFirst(val1);
break;
case 2:
list.insertLast(val2);
break;
case 3:
System.out.print("Enter value to insert: ");
list.insertAtIndex(val3, index);
break;
case 4:
list.deleteFirst();
break;
case 5:
list.deleteLast();
break;
case 6:
list.deleteAtIndex(delIndex);
break;
case 7:
if (list.search(searchVal)) {
break;
case 8:
list.update(oldVal, newVal);
break;
case 9:
break;
case 10:
list.display();
break;
case 11:
System.out.println("Exiting...");
return;
default:
System.out.println("Invalid choice! Please enter a valid option.");
OUTPUT:
RESULT:
Thus all the JAVA programs to practice the implementation and usage of linked list have
been successfully executed
AIM:
To implement a Doubly Linked List in Java that supports:
Algorithm:
Insert First: Create a new node, set it as head and link the old head.
Insert Last: Create a new node, set it as tail and link the previous tail.
Search: Traverse the list and check if node's value matches key.
SOURCE CODE:
import java.io.InputStreamReader;
import java.io.IOException;
class Node {
int data;
this.data = data;
this.prev = null;
this.next = null
int size = 0;
if (head == null)
} else {
newNode.next = head;
head.prev = newNode;
head = newNode;
size++;
if (tail == null) {
head = tail = newNode;
} else {
tail.next = newNode;
newNode.prev = tail;
tail = newNode; }
size++;
System.out.println("Invalid Index!");
return;
size++;
System.out.println("Invalid Index!");
return;
if (index == 0) {
insertFirst(data);
return;
}
if (index == size) {
insertLast(data);
return;
temp = temp.next;
newNode.next = temp.next;
newNode.prev = temp;
temp.next.prev = newNode;
temp.next = newNode;
size++;
if (head == null) {
System.out.println("List is empty!");
return;
if (head == tail) {
} else {
head = head.next;
head.prev = null;
}
size--;
if (tail == null) {
System.out.println("List is empty!");
return;
if (head == tail) {
} else {
tail = tail.prev;
tail.next = null;
size--;
System.out.println("Invalid Index!");
return;
if (index == 0) {
deleteFirst();
return;
}
if (index == size - 1) {
deleteLast();
return;
temp = temp.next;
temp.prev.next = temp.next;
temp.next.prev = temp.prev;
size--;
if (temp.data == key) {
return true;
temp = temp.next;
return false;
if (temp.data == oldVal) {
temp.data = newVal;
return;
temp = temp.next;
return size;
if (head == null) {
System.out.println("List is empty.");
return;
System.out.print("Forward: ");
temp = temp.next;
}
System.out.println("NULL");
if (tail == null) {
System.out.println("List is empty.");
return;
System.out.print("Backward: ");
temp = temp.prev;
System.out.println("NULL");
while (true) {
System.out.println("\nChoose an operation:");
System.out.println("7. Search");
System.out.println("8. Update");
System.out.println("12. Exit");
switch (choice) {
case 1:
list.insertFirst(val1);
break;
case 2:
list.insertLast(val2);
break;
case 3:
System.out.print("Enter value to insert: ");
list.insertAtIndex(val3, index);
break;
case 4:
list.deleteFirst();
break;
case 5:
list.deleteLast();
break;
case 6:
list.deleteAtIndex(delIndex);
break;
case 7:
if (list.search(searchVal)) {
break;
case 8:
list.update(oldVal, newVal);
break;
case 9:
break;
case 10:
list.displayForward();
break;
case 11:
list.displayBackward();
break;
case 12:
System.out.println("Exiting...");
return;
default:
OUTPUT:
RESULT:
Thus all the JAVA programs to practice the implementation and usage of linked list have
been successfully executed
AIM:
Algorithm:
Delete at Index
If invalid → error.
If invalid/empty → error.
Go to index, loop with do-while until back to start.
SOURCE CODE:
import java.io.InputStreamReader;
import java.io.IOException;
class Node {
int data;
Node next;
Node(int data) {
this.data = data;
this.next = null;
int size = 0;
System.out.println("Invalid index!");
return;
if (head == null) {
newNode.next = head;
} else if (index == 0) {
newNode.next = head;
head = newNode;
tail.next = head;
} else {
temp = temp.next;
newNode.next = temp.next;
temp.next = newNode;
if (index == size) {
tail = newNode;
tail.next = head;
size++;
}
public void deleteAtIndex(int index) {
System.out.println("Invalid index!");
return;
if (head == tail) {
} else if (index == 0) {
head = head.next;
tail.next = head;
} else {
temp = temp.next;
temp.next = temp.next.next;
if (index == size - 1) {
tail = temp;
tail.next = head;
size--;
}
public void displayFromIndex(int index) {
return;
temp = temp.next;
do {
temp = temp.next;
System.out.println("(back to start)");
while (true) {
System.out.println("\n--- Circular Singly Linked List ---");
System.out.println("4. Exit");
switch (choice) {
case 1:
list.insertAtIndex(val, idx);
break;
case 2:
list.deleteAtIndex(del);
break;
case 3:
break;
case 4:
System.out.println("Exiting...");
return;
default:
System.out.println("Invalid choice!");
OUTPUT:
RESULT:
Thus all the JAVA programs to practice the implementation and usage of linked list have
been successfully executed
d) Circular Doubly Linked List
AIM:
Insert at Index
Delete at Index
2. Go to index node.
SOURCE CODE:
import java.io.InputStreamReader;
import java.io.IOException;
class Node {
int data;
Node next;
Node prev;
Node(int data) {
this.data = data;
int size = 0;
// Insert at given index
System.out.println("Invalid index!");
return;
if (head == null) {
head = newNode;
} else if (index == 0) {
newNode.next = head;
newNode.prev = tail;
head.prev = newNode;
tail.next = newNode;
head = newNode;
} else {
temp = temp.next;
}
newNode.next = temp.next;
newNode.prev = temp;
temp.next.prev = newNode;
temp.next = newNode;
size++;
return;
if (size == 1) {
head = null;
} else if (index == 0) {
head = head.next;
head.prev = tail;
tail.next = head;
} else {
temp.prev.next = temp.next;
temp.next.prev = temp.prev;
size--;
return;
temp = temp.next;
do {
temp = temp.next;
} while (temp != start);
System.out.println("(back to start)");
return;
temp = temp.next;
do {
temp = temp.prev;
System.out.println("(back to start)");
while (true) {
System.out.println("5. Exit");
switch (choice) {
case 1:
list.insertAtIndex(val, idx);
break;
case 2:
list.deleteAtIndex(delIdx);
break;
case 3:
list.displayForwardFrom(fIndex);
break;
case 4:
list.displayBackwardFrom(bIndex);
break;
case 5:
System.out.println("Exiting...");
return;
default:
System.out.println("Invalid choice!");
OUTPUT:
RESULT:
Thus, the Linked List implementations, including Singly, Doubly, Circular Singly,
and Circular Doubly Linked Lists, were successfully developed and tested. All core
operations such as insertion, deletion, searching, updating, and traversal were performed
accurately without any errors.
a) Bubble Sort
AIM:
To implement the Bubble Sort algorithm in Java to sort an array of integers in ascending
order.
ALGORITHM:
Step 8: Print:
4. Modified 2D array.
SOURCE CODE:
import java.io.InputStreamReader;
import java.util.Arrays;
arr[i] = Integer.parseInt(br.readLine());
boolean swap;
swap = false;
int t = arr[j];
arr[j + 1] = t;
swap = true;
if (!swap) break;
System.out.println(Arrays.toString(arr));
}
OUTPUT:
RESULT:
Thus all the JAVA programs to practice the implementation and usage of Sorting techniques
have been successfully executed
b) Insertion Sort
AIM:
ALGORITHM:
SOURCE CODE:
import java.io.InputStreamReader;
import java.util.Arrays;
arr[i] = Integer.parseInt(br.readLine());
int j = i - 1;
arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = key;
OUTPUT:
RESULT:
Thus all the JAVA programs to practice the implementation and usage of Sorting techniques
have been successfully executed
c) Selection Sort
AIM:
ALGORITHM:
o Assume the current index as min. o Compare with every other element after it.
o If any smaller element is found, update min.
SOURCE CODE:
import java.io.InputStreamReader;
import java.util.Arrays;
System.out.println("Enter size:");
arr[i] = Integer.parseInt(br.readLine());
}
for (int i = 0; i < size - 1; i++) {
int min = i;
min = j;
int t = arr[min];
arr[min] = arr[i];
arr[i] = t;
OUTPUT:
RESULT:
Thus all the JAVA programs to practice the implementation and usage of Sorting techniques
have been successfully executed
d) Merge Sort
AIM:
ALGORITHM:
Step 3: Divide the array into two halves recursively until each subarray has one element.
o Compare elements and insert them in sorted order back into the main array.
SOURCE CODE:
import java.io.InputStreamReader;
import java.io.IOException;
int n = Integer.parseInt(br.readLine());
array[i] = Integer.parseInt(br.readLine());
System.out.println("Original array:");
printArray(array);
System.out.println("Sorted array:");
printArray(array);
public static void merge(int[] arr, int left, int middle, int right) {
int n1 = middle - left + 1;
int i = 0, j = 0, k = left;
arr[k++] = L[i++];
} else {
arr[k++] = R[j++];
arr[k++] = L[i++];
arr[k++] = R[j++];
}
public static void printArray(int[] arr) {
System.out.println();
OUTPUT:
RESULT:
Thus all the JAVA programs to practice the implementation and usage of Sorting
techniques have been successfully executed
e) Quick Sort
AIM:
ALGORITHM:
Step 5: Move i to the right until an element greater than pivot is found.
Step 6: Move j to the left until an element smaller than or equal to pivot is found.
Step 9: Recursively apply Quick Sort on subarrays to the left and right of pivot.
SOURCE CODE:
import java.io.InputStreamReader;
import java.util.Arrays;
while (i <= j) {
while (i <= j && arr[i] <= pivot) i++;
return j;
arr[i] = arr[j];
arr[j] = tem;
arr[i] = Integer.parseInt(br.readLine());
}
quicksort(arr, 0, size - 1);
OUTPUT:
RESULT:
Thus, all sorting algorithms Bubble Sort, Insertion Sort, Selection Sort, Merge
Sort, and Quick Sort were successfully implemented. Each algorithm correctly sorted the
array elements in ascending order and displayed the sorted results without any errors.
To write a java programe to swap the pair of nodes in the linked list
Algoritham :
Step 7: After the loop ends, update head = dummy.next (new head of the list).
SOURCE CODE:
class Node {
int data;
Node next;
this.data = data;
this.next = null;
}
public class SwapPairsInLinkedList {
Node head;
return;
dummy.next = head;
prev.next = second;
first.next = second.next;
second.next = first;
prev = first;
head = first.next;
head = dummy.next;
}
if (head == null) {
head = newNode;
return;
temp = temp.next;
temp.next = newNode;
temp = temp.next;
System.out.println("NULL");
list.insertLast(2);
list.insertLast(3);
list.insertLast(4);
System.out.println("Original List:");
list.display();
list.swapPairs();
list.display();
}}
Output:
Result:
If there’s an odd node at the end (in other examples), it stays in its place.
b) Reverse the linked list without any change in the head and tail.
Aim:
Reversing the linked list without changing the head and tail nodes.
Algoritham:
Step-1(Check edge cases): If the list has less than 3 nodes, do nothing.
Step-2(Identify key nodes) : Set prev = null, current = head.next, and tail = head.
Move tail to the last node.
Source Code:
class Node {
int data;
Node next;
this.data = data;
this.next = null;
Node head;
// Function to insert at end
if (head == null) {
head = newNode;
return;
temp = temp.next;
temp.next = newNode;
return;
tail = tail.next;
}
current.next = prev;
prev = current;
current = nextNode;
current.next = prev;
head.next = current;
temp = temp.next;
System.out.println("NULL");
list.insertLast(2);
list.insertLast(3);
list.insertLast(4);
list.insertLast(5);
System.out.println("Original List:");
list.display();
list.reverseExceptHeadTail();
list.display();
Output:
Result:
The first node (1) and last node (5) remain unchanged.
Aim:
Rearranging the student names in the linked list based on the roll.no in the array.
Algorithm:
Step-1(Initialize): Create an array rollNumbers[] for roll numbers and a linked list for
names.
Step-3(Sort): Use Bubble Sort to sort rollNumbers[] and swap names in parallel.
Step-4(Rebuild List): Clear the current list and insert names back in sorted order.
SOURCE CODE:
class Node {
String name;
Node next;
this.name = name;
this.next = null;
}
Node head;
int[] rollNumbers;
this.rollNumbers = rolls;
if (head == null) {
head = newNode;
return;
temp = temp.next;
temp.next = newNode;
if (temp != null) {
temp = temp.next;
int n = rollNumbers.length;
int idx = 0;
names[idx++] = temp.name;
temp = temp.next;
rollNumbers[j + 1] = tempRoll;
names[j + 1] = tempName;
insertLast(name);
list.insertLast("ab");
list.insertLast("bc");
list.insertLast("cd");
list.insertLast("de");
System.out.println("Before Sorting:");
list.display();
list.rearrange();
list.display();
Output:
Result:
The list is sorted by roll numbers, and the corresponding names are reordered to
match the sorted roll numbers.
The final output will show the roll numbers and names in ascending order of roll
numbers.