SHOPTAK ROY
ROLL: 2285225
1.Single Array In Java Program.
public class SingleArrayExample {
public static void main(String[] args) {
int[] numbers = {1, 2, 3, 4, 5};
for (int i = 0; i < numbers.length; i++) {
System.out.println("Element at index " + i + ": " + numbers[i]);
}
}
}
2.Double Array In Java Program.
Public class DoubleArrayExample {
Public static void main(String[] args) {
Double[] numbers = {3.14, 1.618, 2.718, 0.577, 1.414};
System.out.println(“Array elements using for loop:”);
For (int I = 0; I < numbers.length; i++) {
System.out.println(“Element “ + (I + 1) + “: “ + numbers[i]);
}
Double sum = 0;
For (int I = 0; I < numbers.length; i++) {
Sum += numbers[i];
}
System.out.println(“Sum of array elements: “ + sum)
Double max = numbers[0];
For (int I = 1; I < numbers.length; i++) {
If (numbers[i] > max) {
Max = numbers[i];
}
}
System.out.println(“Maximum value in the array: “ + max);
}
}
Output
Array elements using for loop:
Element 1: 3.14
Element 2: 1.618
Element 3: 2.718
Element 4: 0.577
Element 5: 1.414
Sum of array elements: 9.567
Maximum value in the array: 3.14
3.Bubble Sort In Java Program.
public class BubbleSort {
public static void bubbleSort(int[] arr) {
int n = arr.length;
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
1
SHOPTAK ROY
ROLL: 2285225
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
public static void printArray(int[] arr) {
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
System.out.println();
}
public static void main(String[] args) {
int[] arr = {64, 34, 25, 12, 22, 11, 90};
System.out.println("Original Array:");
printArray(arr);
bubbleSort(arr);
System.out.println("Sorted Array:");
printArray(arr);
}
}
Output
Original Array:
64 34 25 12 22 11 90
Sorted Array:
11 12 22 25 34 64 90
4.Quick Sort In Java Program.
public class QuickSort {
public static void quickSort(int[] arr, int low, int high) {
if (low < high) {
int pivotIndex = partition(arr, low, high);
quickSort(arr, low, pivotIndex - 1); // Left part
quickSort(arr, pivotIndex + 1, high); // Right part
}
}
private static int partition(int[] arr, int low, int high) {
int pivot = arr[high];
int i = (low - 1); // Index of smaller element
for (int j = low; j < high; j++) {
if (arr[j] <= pivot) {
i++; // Increment index of smaller element
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
int temp = arr[i + 1];
arr[i + 1] = arr[high];
2
SHOPTAK ROY
ROLL: 2285225
arr[high] = temp;
return i + 1;
}
public static void printArray(int[] arr) {
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
System.out.println();
}
public static void main(String[] args) {
int[] arr = {64, 34, 25, 12, 22, 11, 90};
System.out.println("Original Array:");
printArray(arr);
quickSort(arr, 0, arr.length - 1);
System.out.println("Sorted Array:");
printArray(arr);
}
}
Output
Original Array:
64 34 25 12 22 11 90
Sorted Array:
11 12 22 25 34 64 90
5. Merge Sort In Java Program.
Public class MergeSort {
Public static void mergeSort(int[] arr) {
If (arr.length < 2) {
Return; // Base case: single element is already sorted
}
Int mid = arr.length / 2;
Int[] left = new int[mid];
Int[] right = new int[arr.length – mid];
System.arraycopy(arr, 0, left, 0, mid);
System.arraycopy(arr, mid, right, 0, arr.length – mid);
mergeSort(left);
mergeSort(right);
Merge(arr, left, right);
}
Private static void merge(int[] arr, int[] left, int[] right) {
Int I = 0, j = 0, k = 0;
While (I < left.length && j < right.length) {
If (left[i] <= right[j]) {
Arr[k++] = left[i++];
} else {
Arr[k++] = right[j++];
}
}
While (I < left.length) {
3
SHOPTAK ROY
ROLL: 2285225
Arr[k++] = left[i++];
}
While (j < right.length) {
Arr[k++] = right[j++];
}
}
Public static void printArray(int[] arr) {
For (int I = 0; I < arr.length; i++) {
System.out.print(arr[i] + “ “);
}
System.out.println();
}
Public static void main(String[] args) {
Int[] arr = {64, 34, 25, 12, 22, 11, 90};
System.out.println(“Original Array:”);
printArray(arr);
mergeSort(arr);
System.out.println(“Sorted Array:”);
printArray(arr);
}
}
Output
Original Array:
64 34 25 12 22 11 90
Sorted Array:
11 12 22 25 34 64 90
6. Insertation Sort In Java Program.
Public class InsertionSort {
Public static void insertionSort(int[] arr) {
For (int I = 1; I < arr.length; i++) {
Int key = arr[i]; // The current element to be inserted into the
sorted portion of the array
Int j = I – 1; // Index of the last element in the sorted portion
While (j >= 0 && arr[j] > key) {
Arr[j + 1] = arr[j]; // Shift element to the right
J = j – 1;
}
Arr[j + 1] = key; // Insert the key at the correct position
}
}
Public static void printArray(int[] arr) {
For (int I = 0; I < arr.length; i++) {
System.out.print(arr[i] + “ “);
}
System.out.println();
}
4
SHOPTAK ROY
ROLL: 2285225
Public static void main(String[] args) {
Int[] arr = {64, 34, 25, 12, 22, 11, 90};
System.out.println(“Original Array:”);
printArray(arr);
insertionSort(arr);
System.out.println(“Sorted Array:”);
printArray(arr);
}
}
Output
Original Array:
64 34 25 12 22 11 90
Sorted Array:
11 12 22 25 34 64 90
7.Heap Sort In Java Program.
import java.util.Arrays;
public class HeapSort {
public static void heapSort(int[] arr) {
int n = arr.length;
for (int i = n / 2 - 1; i >= 0; i--) {
heapify(arr, n, i);
}
for (int i = n - 1; i >= 0; i--) {
swap(arr, 0, i);
heapify(arr, i, 0);
}
}
private static void heapify(int[] arr, int n, int i) {
int largest = i; // Initialize largest as root
int left = 2 * i + 1; // left = 2*i + 1
int right = 2 * i + 2; // right = 2*i + 2
if (left < n && arr[left] > arr[largest]) {
largest = left;
}
if (right < n && arr[right] > arr[largest]) {
largest = right;
}
if (largest != i) {
swap(arr, i, largest);
heapify(arr, n, largest);
}
}
private static void swap(int[] arr, int i, int j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
5
SHOPTAK ROY
ROLL: 2285225
public static void main(String[] args) {
int[] arr = { 12, 11, 13, 5, 6, 7 };
System.out.println("Original array: " + Arrays.toString(arr));
heapSort(arr);
System.out.println("Sorted array: " + Arrays.toString(arr));
}
}
Output
Original array: [12, 11, 13, 5, 6, 7]
Sorted array: [5, 6, 7, 11, 12, 13]
8.Selection Sort In Java Program.
Public class SelectionSort {
Public static void selectionSort(int[] arr) {
Int n = arr.length;
For (int I = 0; I < n – 1; i++) {
Int minIndex = I;
For (int j = I + 1; j < n; j++) {
If (arr[j] < arr[minIndex]) {
minIndex = j;
}
}
If (minIndex != i) {
Int temp = arr[minIndex];
Arr[minIndex] = arr[i];
Arr[i] = temp;
}
}
}
Public static void printArray(int[] arr) {
For (int I : arr) {
System.out.print(I + “ “);
}
System.out.println();
}
Public static void main(String[] args) {
Int[] arr = {64, 25, 12, 22, 11};
System.out.println(“Unsorted array:”);
printArray(arr);
selectionSort(arr);
System.out.println(“Sorted array:”);
printArray(arr);
}
}
Output
6
SHOPTAK ROY
ROLL: 2285225
Unsorted array:
64 25 12 22 11
Sorted array:
11 12 22 25 64
9.Binary Search Program In Java .
Public class BinarySearch {
Public static int binarySearch(int[] arr, int target) {
Int left = 0;
Int right = arr.length – 1;
While (left <= right) {
// Find the middle element
Int mid = left + (right – left) / 2;
// Check if the target is present at mid
If (arr[mid] == target) {
Return mid; // Target found, return its index
}
If (arr[mid] < target) {
Left = mid + 1;
}
Else {
Right = mid – 1;
}
}
Return -1;
}
Public static void printArray(int[] arr) {
For (int I : arr) {
System.out.print(I + “ “);
}
System.out.println();
}
Public static void main(String[] args) {
Int[] arr = {1, 3, 5, 7, 9, 11, 13, 15, 17, 19}; // Array must be sorted
for binary search
System.out.println(“Array:”);
printArray(arr);
int target = 7;
int result = binarySearch(arr, target);
if (result == -1) {
System.out.println(“Element not found”);
} else {
System.out.println(“Element found at index: “ + result);
}
}
}
Output
Array:
1 3 5 7 9 11 13 15 17 19
7
SHOPTAK ROY
ROLL: 2285225
Element found at index: 3
10.linear Search Program In Java.
Import java.util.Scanner;
Public class LinearSearch {
Public static int linearSearch(int[] arr, int target) {
For (int I = 0; I < arr.length; i++) {
If (arr[i] == target) {
Return I; // return the index of the element
}
}
Return -1; // return -1 if the target is not found
}
Public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print(“Enter the number of elements: “);
Int n = scanner.nextInt();
Int[] arr = new int[n];
System.out.println(“Enter the elements of the array:”);
For (int I = 0; I < n; i++) {
Arr[i] = scanner.nextInt();
}
System.out.print(“Enter the element to search: “);
Int target = scanner.nextInt();
Int result = linearSearch(arr, target);
If (result == -1) {
System.out.println(“Element not found in the array.”);
} else {
System.out.println(“Element found at index: “ + result);
}
Scanner.close();
}
}
Output
Enter the number of elements: 5
Enter the elements of the array:
31425
Enter the element to search: 4
Element found at index: 2
11.Linked List Data Addition Program In Java.
Import java.util.LinkedList;
Public class LinkedListAddition {
Public static void main(String[] args) {
LinkedList<Integer> list = new LinkedList<>();
List.add(10); // Adds 10 to the end of the list
List.add(20); // Adds 20 to the end of the list
8
SHOPTAK ROY
ROLL: 2285225
List.add(30); // Adds 30 to the end of the list
List.addFirst(5); // Adds 5 to the beginning of the list
List.addLast(40); // Adds 40 to the end of the list
System.out.println(“Linked List: “ + list);
List.add(2, 25); // Adds 25 at index 2
System.out.println(“After adding 25 at index 2: “ + list);
}
}
Output
Linked List: [5, 10, 20, 30, 40]
After adding 25 at index 2: [5, 10, 25, 20, 30, 40]
12.Write Linked List Data Separation Program in Java.
class LinkedList {
static class Node {
int data;
Node next;
Node(int data) {
this.data = data;
this.next = null;
}
}
Node head; // Head of the list
public void insert(int data) {
Node newNode = new Node(data);
if (head == null) {
head = newNode;
} else {
Node current = head;
while (current.next != null) {
current = current.next;
}
current.next = newNode;
}
}
public void printList() {
Node current = head;
while (current != null) {
System.out.print(current.data + " ");
current = current.next;
}
System.out.println();
}
public void separateEvenOdd() {
Node evenHead = null, evenTail = null;
Node oddHead = null, oddTail = null;
Node current = head;
while (current != null) {
if (current.data % 2 == 0) {
9
SHOPTAK ROY
ROLL: 2285225
if (evenHead == null) {
evenHead = evenTail = current; // Initialize even list
} else {
evenTail.next = current;
evenTail = evenTail.next;
}
} else {
if (oddHead == null) {
oddHead = oddTail = current; // Initialize odd list
} else {
oddTail.next = current; // Add to the odd list
oddTail = oddTail.next;
}
}
current = current.next;
}
if (evenTail != null) evenTail.next = null;
if (oddTail != null) oddTail.next = null;
System.out.print("Even List: ");
printList(evenHead);
System.out.print("Odd List: ");
printList(oddHead);
}
private void printList(Node node) {
Node current = node;
while (current != null) {
System.out.print(current.data + " ");
current = current.next;
}
System.out.println();
}
public static void main(String[] args) {
LinkedList list = new LinkedList();
list.insert(1);
list.insert(2);
list.insert(3);
list.insert(4);
list.insert(5);
list.insert(6);
list.insert(7);
System.out.print("Original List: ");
list.printList();
list.separateEvenOdd();
}
}
Output
Original List: 1 2 3 4 5 6 7
Even List: 2 4 6
Odd List: 1 3 5 7
10
SHOPTAK ROY
ROLL: 2285225
13.Stack Data Addition Program In Java.
Import java.util.Stack;
Public class StackDataAddition {
Public static void main(String[] args) {
Stack<Integer> stack = new Stack<>();
Stack.push(10);
Stack.push(20);
Stack.push(30);
Stack.push(40);
Stack.push(50);
Int sum = 0;
While (!stack.isEmpty()) {
Sum += stack.pop();
}
System.out.println(“The sum of the elements in the stack is: “ +
sum);
}
}
Output
The sum of the elements in the stack is: 150
14.Stack Data Separation Program In Java.
import java.util.Stack;
public class StackDataAddition {
public static void main(String[] args) {
Stack<Integer> stack = new Stack<>();
stack.push(10);
stack.push(20);
stack.push(30);
stack.push(40);
stack.push(50);
int sum = 0;
while (!stack.isEmpty()) {
sum += stack.pop();
}
System.out.println("The sum of the elements in the stack is: " +
sum);
}
}
Output
Even numbers stack: [30, 20, 10]
Odd numbers stack: [35, 25, 15]
15.Queue Data Addition Program In Java.
import java.util.LinkedList;
import java.util.Queue;
public class QueueExample {
public static void main(String[] args) {
11
SHOPTAK ROY
ROLL: 2285225
Queue<Integer> queue = new LinkedList<>();
queue.add(10); // Adds 10 to the queue
queue.add(20); // Adds 20 to the queue
queue.add(30); // Adds 30 to the queue
queue.add(40); // Adds 40 to the queue
System.out.println("Queue after additions: " + queue);
}
}
Output
Queue after additions: [10, 20, 30, 40]
16.Queue Data Separation Program In Java.
Import java.util.LinkedList;
Import java.util.Queue;
Public class QueueDataSeparation {
Public static void main(String[] args)
Int[] numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
Queue<Integer> evenQueue = new LinkedList<>();
Queue<Integer> oddQueue = new LinkedList<>();
For (int number : numbers) {
If (number % 2 == 0) {
evenQueue.add(number); // Add to even queue
} else {
oddQueue.add(number); // Add to odd queue
}
}
System.out.println(“Even Numbers Queue: “ + evenQueue);
System.out.println(“Odd Numbers Queue: “ + oddQueue);
}
}
Output
Even Numbers Queue: [2, 4, 6, 8, 10]
Odd Numbers Queue: [1, 3, 5, 7, 9]
17.Shell Sort In Java Program.
Public class ShellSort {
Public static void shellSort(int[] arr) {
Int n = arr.length;
For (int gap = n / 2; gap > 0; gap /= 2) {
For (int I = gap; I < n; i++) {
Int temp = arr[i];
Int j = I;
While (j >= gap && arr[j – gap] > temp) {
Arr[j] = arr[j – gap];
J -= gap;
}
Arr[j] = temp;
}
}
12
SHOPTAK ROY
ROLL: 2285225
}
Public static void printArray(int[] arr) {
For (int I = 0; I < arr.length; i++) {
System.out.print(arr[i] + “ “);
}
System.out.println();
}
Public static void main(String[] args) {
Int[] arr = { 12, 34, 54, 2, 3 };
System.out.println(“Original Array:”);
printArray(arr);
shellSort(arr);
System.out.println(“Sorted Array:”);
printArray(arr);
}
}
Output
Original Array:
12 34 54 2 3
Sorted Array:
2 3 12 34 54
13