Archives: Logical Programs

Arrays.sort() in Java with Examples

Arrays.sort() is a built-in method in Java’s java.util package that provides an efficient and convenient way to sort arrays. This method uses the Dual-Pivot Quicksort algorithm for primitive types and TimSort for objects. Arrays.sort() Key Points Arrays.sort() Method Signature Arrays.sort() method Examples in Java 1. Sorting Primitive Arrays Demonstrates sorting an integer array in ascending …

Arrays.sort() in Java with Examples Read More »

Heap Sort in Java

Heap Sort is a comparison-based sorting algorithm that uses a binary heap data structure. It is an efficient algorithm with a worst-case time complexity of O(n logā” n). How Does Heap Sort Work? Heap Sort works by leveraging the properties of a binary heap to sort an array. Here’s how it works step by step: …

Heap Sort in Java Read More »

Quick Sort in Java

Quick Sort is a highly efficient divide-and-conquer sorting algorithm. It works by selecting a “pivot” element, partitioning the array such that elements smaller than the pivot are placed to its left and elements larger are placed to its right, and then recursively sorting the subarrays. How Quick Sort Works? Steps of the Quick Sort Algorithm: …

Quick Sort in Java Read More »

Merge Sort in Java

Merge Sort is a divide-and-conquer sorting algorithm that splits an array into smaller subarrays, sorts each subarray, and then merges them back together to form a single sorted array. It is one of the most efficient sorting algorithms, particularly for large datasets. How Merge Sort Works? Understand it with an Example: Input Array: [38, 27, …

Merge Sort in Java Read More »

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 »