Archives: Logical Programs

Selection Sort in Java

Selection Sort is a comparison-based sorting algorithm. It works by dividing the array into two parts: a sorted part and an unsorted part. The algorithm repeatedly selects the smallest (or largest, depending on sorting order) element from the unsorted part and moves it to the end of the sorted part. Steps of Selection Sort Initialization: …

Selection Sort in Java Read More »

Bubble Sort in Java

Bubble Sort is a simple comparison-based sorting algorithm. It works by repeatedly stepping through the list to be sorted, comparing each pair of adjacent items, and swapping them if they are in the wrong order. This process is repeated until the list is sorted. Steps of Bubble Sort Detailed Example Let’s sort the array [64, …

Bubble Sort in Java Read More »

Tower of Hanoi Game in Java

Problem: Write a program to solve the Tower of Hanoi puzzle, which involves moving disks from one peg to another while obeying certain rules. The Tower of Hanoi puzzle is a timeless challenge where players move disks between pegs following strict rules. It’s a captivating game that highlights key concepts like recursion and problem-solving strategies. …

Tower of Hanoi Game in Java Read More »

Queue Implementation using LinkedList

Problem Statement: Write a Java program to implement a basic queue using LinkedList to enqueue and dequeue elements. Here’s a basic implementation of a queue using a LinkedList in Java to enqueue and dequeue elements. OUTPUT: Queue Size: 3Dequeued Element: 1Dequeued Element: 2Queue Size after Dequeue: 1 Explanation: This basic queue implementation provides the fundamental …

Queue Implementation using LinkedList Read More »

Create a Custom LinkedList in Java

Problem Statement: Write a Java program to create a custom LinkedList class with methods to add elements, remove elements, find the length, and reverse the list. Here’s a custom linked list class in Java with methods to add elements, remove elements, find the length, and reverse the list. OUTPUT: Original Linked List:1 -> 2 -> …

Create a Custom LinkedList in Java Read More »

Swap two elements in ArrayList

Problem Statement: Write a Java program to swap the positions of two elements in an ArrayList given their indices. Here’s a Java program that demonstrates how to swap the positions of two elements in an ArrayList given their indices. OUTPUT: Original List: [Apple, Banana, Cherry, Date, Fig]List after swapping elements at index 1 and index …

Swap two elements in ArrayList Read More »