From 7e4882132d8f2b39ef93a634d0d05bb44b63a530 Mon Sep 17 00:00:00 2001 From: Yash Kiradoo Date: Sat, 12 Oct 2024 07:18:10 +0000 Subject: [PATCH 1/2] Add Block Sort algorithm implementation --- .../com/thealgorithms/sorts/BlockSort.java | 75 +++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 src/main/java/com/thealgorithms/sorts/BlockSort.java diff --git a/src/main/java/com/thealgorithms/sorts/BlockSort.java b/src/main/java/com/thealgorithms/sorts/BlockSort.java new file mode 100644 index 000000000000..eb5de42907cb --- /dev/null +++ b/src/main/java/com/thealgorithms/sorts/BlockSort.java @@ -0,0 +1,75 @@ +package com.thealgorithms.sorts; + +import java.util.Arrays; + +/** + * Implementation of Block Sort algorithm that implements the SortAlgorithm interface. + * + * Block Sort is a distribution sorting algorithm that divides the array into blocks, + * sorts each block, and merges the blocks to produce a sorted array. + * + * The method works as follows: + *
    + *
  1. Divide the input array into blocks.
  2. + *
  3. Sort each block individually.
  4. + *
  5. Merge the sorted blocks to produce a fully sorted array.
  6. + *
+ */ +public class BlockSort implements SortAlgorithm { + + @Override + public > T[] sort(T[] array) { + return blockSort(array); + } + + /** + * Sorts an array using the Block Sort algorithm. + * + * @param arr the array to be sorted. + * @param the type of elements to be sorted, must be comparable. + * @return the sorted array. + */ + private > T[] blockSort(T[] arr) { + if (arr.length <= 1) { + return arr; // Already sorted + } + + int blockSize = (int) Math.sqrt(arr.length); // Block size is the square root of the array size + int numBlocks = (arr.length + blockSize - 1) / blockSize; // Calculate number of blocks + + // Create an array of blocks + T[][] blocks = (T[][]) new Comparable[numBlocks][]; + + // Populate the blocks + for (int i = 0; i < numBlocks; i++) { + int start = i * blockSize; + int end = Math.min(start + blockSize, arr.length); + blocks[i] = Arrays.copyOfRange(arr, start, end); + Arrays.sort(blocks[i]); // Sort each block + } + + // Merge the sorted blocks + return mergeBlocks(blocks); + } + + /** + * Merges sorted blocks into a single sorted array. + * + * @param blocks the array of sorted blocks. + * @param the type of elements in the blocks, must be comparable. + * @return the merged sorted array. + */ + private > T[] mergeBlocks(T[][] blocks) { + int totalLength = Arrays.stream(blocks).mapToInt(b -> b.length).sum(); + T[] result = (T[]) new Comparable[totalLength]; + int index = 0; + + for (T[] block : blocks) { + for (T element : block) { + result[index++] = element; // Add each element from the block to the result array + } + } + + return result; + } +} From c0cb4871961cf293e07cd7f7079bb9c4b8842fa6 Mon Sep 17 00:00:00 2001 From: Yash Kiradoo Date: Sat, 12 Oct 2024 07:22:28 +0000 Subject: [PATCH 2/2] Added BlockSort Implementation --- src/main/java/com/thealgorithms/sorts/BlockSort.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/sorts/BlockSort.java b/src/main/java/com/thealgorithms/sorts/BlockSort.java index eb5de42907cb..d2a318e5000f 100644 --- a/src/main/java/com/thealgorithms/sorts/BlockSort.java +++ b/src/main/java/com/thealgorithms/sorts/BlockSort.java @@ -69,7 +69,6 @@ private > T[] mergeBlocks(T[][] blocks) { result[index++] = element; // Add each element from the block to the result array } } - return result; } }