SHOPTAK ROY
ROLL: 2285225
[Link] 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 < [Link]; i++) {
[Link]("Element at index " + i + ": " + numbers[i]);
}
}
}
[Link] 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};
[Link](“Array elements using for loop:”);
For (int I = 0; I < [Link]; i++) {
[Link](“Element “ + (I + 1) + “: “ + numbers[i]);
}
Double sum = 0;
For (int I = 0; I < [Link]; i++) {
Sum += numbers[i];
}
[Link](“Sum of array elements: “ + sum)
Double max = numbers[0];
For (int I = 1; I < [Link]; i++) {
If (numbers[i] > max) {
Max = numbers[i];
}
}
[Link](“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
[Link] Sort In Java Program.
public class BubbleSort {
public static void bubbleSort(int[] arr) {
int n = [Link];
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 < [Link]; i++) {
[Link](arr[i] + " ");
}
[Link]();
}
public static void main(String[] args) {
int[] arr = {64, 34, 25, 12, 22, 11, 90};
[Link]("Original Array:");
printArray(arr);
bubbleSort(arr);
[Link]("Sorted Array:");
printArray(arr);
}
}
Output
Original Array:
64 34 25 12 22 11 90
Sorted Array:
11 12 22 25 34 64 90
[Link] 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 < [Link]; i++) {
[Link](arr[i] + " ");
}
[Link]();
}
public static void main(String[] args) {
int[] arr = {64, 34, 25, 12, 22, 11, 90};
[Link]("Original Array:");
printArray(arr);
quickSort(arr, 0, [Link] - 1);
[Link]("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 ([Link] < 2) {
Return; // Base case: single element is already sorted
}
Int mid = [Link] / 2;
Int[] left = new int[mid];
Int[] right = new int[[Link] – mid];
[Link](arr, 0, left, 0, mid);
[Link](arr, mid, right, 0, [Link] – 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 < [Link] && j < [Link]) {
If (left[i] <= right[j]) {
Arr[k++] = left[i++];
} else {
Arr[k++] = right[j++];
}
}
While (I < [Link]) {
3
SHOPTAK ROY
ROLL: 2285225
Arr[k++] = left[i++];
}
While (j < [Link]) {
Arr[k++] = right[j++];
}
}
Public static void printArray(int[] arr) {
For (int I = 0; I < [Link]; i++) {
[Link](arr[i] + “ “);
}
[Link]();
}
Public static void main(String[] args) {
Int[] arr = {64, 34, 25, 12, 22, 11, 90};
[Link](“Original Array:”);
printArray(arr);
mergeSort(arr);
[Link](“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 < [Link]; 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 < [Link]; i++) {
[Link](arr[i] + “ “);
}
[Link]();
}
4
SHOPTAK ROY
ROLL: 2285225
Public static void main(String[] args) {
Int[] arr = {64, 34, 25, 12, 22, 11, 90};
[Link](“Original Array:”);
printArray(arr);
insertionSort(arr);
[Link](“Sorted Array:”);
printArray(arr);
}
}
Output
Original Array:
64 34 25 12 22 11 90
Sorted Array:
11 12 22 25 34 64 90
[Link] Sort In Java Program.
import [Link];
public class HeapSort {
public static void heapSort(int[] arr) {
int n = [Link];
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 };
[Link]("Original array: " + [Link](arr));
heapSort(arr);
[Link]("Sorted array: " + [Link](arr));
}
}
Output
Original array: [12, 11, 13, 5, 6, 7]
Sorted array: [5, 6, 7, 11, 12, 13]
[Link] Sort In Java Program.
Public class SelectionSort {
Public static void selectionSort(int[] arr) {
Int n = [Link];
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) {
[Link](I + “ “);
}
[Link]();
}
Public static void main(String[] args) {
Int[] arr = {64, 25, 12, 22, 11};
[Link](“Unsorted array:”);
printArray(arr);
selectionSort(arr);
[Link](“Sorted array:”);
printArray(arr);
}
}
Output
6
SHOPTAK ROY
ROLL: 2285225
Unsorted array:
64 25 12 22 11
Sorted array:
11 12 22 25 64
[Link] Search Program In Java .
Public class BinarySearch {
Public static int binarySearch(int[] arr, int target) {
Int left = 0;
Int right = [Link] – 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) {
[Link](I + “ “);
}
[Link]();
}
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
[Link](“Array:”);
printArray(arr);
int target = 7;
int result = binarySearch(arr, target);
if (result == -1) {
[Link](“Element not found”);
} else {
[Link](“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
[Link] Search Program In Java.
Import [Link];
Public class LinearSearch {
Public static int linearSearch(int[] arr, int target) {
For (int I = 0; I < [Link]; 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([Link]);
[Link](“Enter the number of elements: “);
Int n = [Link]();
Int[] arr = new int[n];
[Link](“Enter the elements of the array:”);
For (int I = 0; I < n; i++) {
Arr[i] = [Link]();
}
[Link](“Enter the element to search: “);
Int target = [Link]();
Int result = linearSearch(arr, target);
If (result == -1) {
[Link](“Element not found in the array.”);
} else {
[Link](“Element found at index: “ + result);
}
[Link]();
}
}
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
[Link] List Data Addition Program In Java.
Import [Link];
Public class LinkedListAddition {
Public static void main(String[] args) {
LinkedList<Integer> list = new LinkedList<>();
[Link](10); // Adds 10 to the end of the list
[Link](20); // Adds 20 to the end of the list
8
SHOPTAK ROY
ROLL: 2285225
[Link](30); // Adds 30 to the end of the list
[Link](5); // Adds 5 to the beginning of the list
[Link](40); // Adds 40 to the end of the list
[Link](“Linked List: “ + list);
[Link](2, 25); // Adds 25 at index 2
[Link](“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]
[Link] Linked List Data Separation Program in Java.
class LinkedList {
static class Node {
int data;
Node next;
Node(int data) {
[Link] = data;
[Link] = 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 ([Link] != null) {
current = [Link];
}
[Link] = newNode;
}
}
public void printList() {
Node current = head;
while (current != null) {
[Link]([Link] + " ");
current = [Link];
}
[Link]();
}
public void separateEvenOdd() {
Node evenHead = null, evenTail = null;
Node oddHead = null, oddTail = null;
Node current = head;
while (current != null) {
if ([Link] % 2 == 0) {
9
SHOPTAK ROY
ROLL: 2285225
if (evenHead == null) {
evenHead = evenTail = current; // Initialize even list
} else {
[Link] = current;
evenTail = [Link];
}
} else {
if (oddHead == null) {
oddHead = oddTail = current; // Initialize odd list
} else {
[Link] = current; // Add to the odd list
oddTail = [Link];
}
}
current = [Link];
}
if (evenTail != null) [Link] = null;
if (oddTail != null) [Link] = null;
[Link]("Even List: ");
printList(evenHead);
[Link]("Odd List: ");
printList(oddHead);
}
private void printList(Node node) {
Node current = node;
while (current != null) {
[Link]([Link] + " ");
current = [Link];
}
[Link]();
}
public static void main(String[] args) {
LinkedList list = new LinkedList();
[Link](1);
[Link](2);
[Link](3);
[Link](4);
[Link](5);
[Link](6);
[Link](7);
[Link]("Original List: ");
[Link]();
[Link]();
}
}
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
[Link] Data Addition Program In Java.
Import [Link];
Public class StackDataAddition {
Public static void main(String[] args) {
Stack<Integer> stack = new Stack<>();
[Link](10);
[Link](20);
[Link](30);
[Link](40);
[Link](50);
Int sum = 0;
While (![Link]()) {
Sum += [Link]();
}
[Link](“The sum of the elements in the stack is: “ +
sum);
}
}
Output
The sum of the elements in the stack is: 150
[Link] Data Separation Program In Java.
import [Link];
public class StackDataAddition {
public static void main(String[] args) {
Stack<Integer> stack = new Stack<>();
[Link](10);
[Link](20);
[Link](30);
[Link](40);
[Link](50);
int sum = 0;
while (![Link]()) {
sum += [Link]();
}
[Link]("The sum of the elements in the stack is: " +
sum);
}
}
Output
Even numbers stack: [30, 20, 10]
Odd numbers stack: [35, 25, 15]
[Link] Data Addition Program In Java.
import [Link];
import [Link];
public class QueueExample {
public static void main(String[] args) {
11
SHOPTAK ROY
ROLL: 2285225
Queue<Integer> queue = new LinkedList<>();
[Link](10); // Adds 10 to the queue
[Link](20); // Adds 20 to the queue
[Link](30); // Adds 30 to the queue
[Link](40); // Adds 40 to the queue
[Link]("Queue after additions: " + queue);
}
}
Output
Queue after additions: [10, 20, 30, 40]
[Link] Data Separation Program In Java.
Import [Link];
Import [Link];
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) {
[Link](number); // Add to even queue
} else {
[Link](number); // Add to odd queue
}
}
[Link](“Even Numbers Queue: “ + evenQueue);
[Link](“Odd Numbers Queue: “ + oddQueue);
}
}
Output
Even Numbers Queue: [2, 4, 6, 8, 10]
Odd Numbers Queue: [1, 3, 5, 7, 9]
[Link] Sort In Java Program.
Public class ShellSort {
Public static void shellSort(int[] arr) {
Int n = [Link];
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 < [Link]; i++) {
[Link](arr[i] + “ “);
}
[Link]();
}
Public static void main(String[] args) {
Int[] arr = { 12, 34, 54, 2, 3 };
[Link](“Original Array:”);
printArray(arr);
shellSort(arr);
[Link](“Sorted Array:”);
printArray(arr);
}
}
Output
Original Array:
12 34 54 2 3
Sorted Array:
2 3 12 34 54
13