Archives: Logical Programs

Insertion Sort in Java

Insertion Sort is a simple and efficient comparison-based sorting algorithm that works similarly to how you might sort playing cards in your hands. It builds the sorted array one element at a time by placing each new element into its correct position within the already sorted part of the array. Insertion Sort Algorithm Steps: Understand …

Insertion Sort in Java Read More »

Selection Sort in Java

Selection Sort is a simple comparison-based sorting algorithm. It repeatedly selects the smallest (or largest) element from the unsorted part of the array and places it in the correct position in the sorted part. How Does It Work? Steps of the Algorithm: Understand it with an Example: Input Array: [64, 25, 12, 22, 11] Output: …

Selection Sort in Java Read More »

Find GCD of N Numbers in Java

The Greatest Common Divisor (GCD), also known as the Greatest Common Factor (GCF) or Highest Common Factor (HCF), is the largest positive integer that divides two or more integers without leaving a remainder. Key Points about GCD (Greatest Common Divisor) Examples of Greatest Common Divisor (GCD) Example 1: GCD of Two Numbers (12 and 18) …

Find GCD of N Numbers in Java Read More »

Java Program to Get Array Input

This post shows you multiple approaches to get array input in Java. 1. Using a Static Array (Hardcoded Values) This is the simplest way to initialize values directly in the array. OUTPUT: Array elements: 10 20 30 40 50 2. Using Scanner (User Input from Console) This approach lets the user input array elements during …

Java Program to Get Array Input Read More »

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 »