From 91c104d3fe0cda2d023c3ba66056c366f9b415fd Mon Sep 17 00:00:00 2001 From: Asitha Date: Sat, 18 Oct 2025 23:33:08 +0530 Subject: [PATCH 1/3] Add Randomized QuickSort algorithm to sorts --- .../sorts/RandomizedQuickSort.java | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 src/main/java/com/thealgorithms/sorts/RandomizedQuickSort.java diff --git a/src/main/java/com/thealgorithms/sorts/RandomizedQuickSort.java b/src/main/java/com/thealgorithms/sorts/RandomizedQuickSort.java new file mode 100644 index 000000000000..23e88f639570 --- /dev/null +++ b/src/main/java/com/thealgorithms/sorts/RandomizedQuickSort.java @@ -0,0 +1,49 @@ +import java.util.Random; + +public class RandomizedQuickSort { + + private static final Random rand = new Random(); + + public static void randomizedQuickSort(int[] arr, int low, int high) { + if (low < high) { + int pivotIndex = randomizedPartition(arr, low, high); + randomizedQuickSort(arr, low, pivotIndex - 1); + randomizedQuickSort(arr, pivotIndex + 1, high); + } + } + + private static int randomizedPartition(int[] arr, int low, int high) { + int pivotIndex = low + rand.nextInt(high - low + 1); + swap(arr, pivotIndex, high); // Move pivot to end + return partition(arr, low, high); + } + + 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++; + swap(arr, i, j); + } + } + swap(arr, i + 1, high); + return i + 1; + } + + private static void swap(int[] arr, int i, int j) { + int temp = arr[i]; + arr[i] = arr[j]; + arr[j] = temp; + } + + // Test the algorithm + public static void main(String[] args) { + int[] arr = {10, 7, 8, 9, 1, 5}; + randomizedQuickSort(arr, 0, arr.length - 1); + System.out.println("Sorted array:"); + for (int num : arr) { + System.out.print(num + " "); + } + } +} From c99087e7a5cfa97070d20706cfc8669f47b8a1bb Mon Sep 17 00:00:00 2001 From: Asitha Date: Sun, 19 Oct 2025 00:20:08 +0530 Subject: [PATCH 2/3] Add Javadoc comments --- .../sorts/RandomizedQuickSort.java | 49 +++++++++++++++++-- 1 file changed, 46 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/thealgorithms/sorts/RandomizedQuickSort.java b/src/main/java/com/thealgorithms/sorts/RandomizedQuickSort.java index 23e88f639570..849367f41d5d 100644 --- a/src/main/java/com/thealgorithms/sorts/RandomizedQuickSort.java +++ b/src/main/java/com/thealgorithms/sorts/RandomizedQuickSort.java @@ -1,9 +1,22 @@ +package com.thealgorithms.sorts; import java.util.Random; +/** + * Implementation of the Randomized QuickSort algorithm. + * This algorithm sorts an array by choosing a random pivot element, + * which improves the average performance over traditional QuickSort. + */ public class RandomizedQuickSort { private static final Random rand = new Random(); + /** + * Sorts the array in-place using Randomized QuickSort algorithm. + * + * @param arr the array to be sorted + * @param low the starting index of the segment to sort + * @param high the ending index of the segment to sort + */ public static void randomizedQuickSort(int[] arr, int low, int high) { if (low < high) { int pivotIndex = randomizedPartition(arr, low, high); @@ -12,15 +25,33 @@ public static void randomizedQuickSort(int[] arr, int low, int high) { } } + /** + * Chooses a random pivot, swaps it with the last element, + * then partitions the array around this pivot. + * + * @param arr the array to partition + * @param low the starting index of the segment to partition + * @param high the ending index of the segment to partition + * @return final index position of the pivot element + */ private static int randomizedPartition(int[] arr, int low, int high) { int pivotIndex = low + rand.nextInt(high - low + 1); - swap(arr, pivotIndex, high); // Move pivot to end + swap(arr, pivotIndex, high); // Move pivot to end return partition(arr, low, high); } + /** + * Partitions the array segment such that elements less than or equal + * to the pivot are to the left, and greater are to the right. + * + * @param arr the array to partition + * @param low the starting index of the segment to partition + * @param high the ending index of the segment to partition + * @return the final position of the pivot element + */ private static int partition(int[] arr, int low, int high) { int pivot = arr[high]; - int i = low - 1; // index of smaller element + int i = low - 1; // index of smaller element for (int j = low; j < high; j++) { if (arr[j] <= pivot) { i++; @@ -31,13 +62,25 @@ private static int partition(int[] arr, int low, int high) { return i + 1; } + /** + * Swaps two elements in the array. + * + * @param arr the array containing elements to swap + * @param i index of first element + * @param j index of second element + */ private static void swap(int[] arr, int i, int j) { int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } - // Test the algorithm + /** + * Main method for basic demonstration. + * Sorts a sample array and prints the sorted output. + * + * @param args command-line arguments (not used) + */ public static void main(String[] args) { int[] arr = {10, 7, 8, 9, 1, 5}; randomizedQuickSort(arr, 0, arr.length - 1); From 5aa966cb44fa44f0c563d6d35fd6e9f216cfc93e Mon Sep 17 00:00:00 2001 From: Asitha Date: Sun, 19 Oct 2025 01:47:45 +0530 Subject: [PATCH 3/3] Fix Checkstyle violations: add private constructor, rename constant, remove trailing spaces, add Javadoc --- .../thealgorithms/sorts/RandomizedQuickSort.java | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/src/main/java/com/thealgorithms/sorts/RandomizedQuickSort.java b/src/main/java/com/thealgorithms/sorts/RandomizedQuickSort.java index 849367f41d5d..09418a5b9973 100644 --- a/src/main/java/com/thealgorithms/sorts/RandomizedQuickSort.java +++ b/src/main/java/com/thealgorithms/sorts/RandomizedQuickSort.java @@ -6,13 +6,12 @@ * This algorithm sorts an array by choosing a random pivot element, * which improves the average performance over traditional QuickSort. */ -public class RandomizedQuickSort { +private class RandomizedQuickSort { - private static final Random rand = new Random(); + private static final Random RAND = new Random(); /** * Sorts the array in-place using Randomized QuickSort algorithm. - * * @param arr the array to be sorted * @param low the starting index of the segment to sort * @param high the ending index of the segment to sort @@ -28,7 +27,6 @@ public static void randomizedQuickSort(int[] arr, int low, int high) { /** * Chooses a random pivot, swaps it with the last element, * then partitions the array around this pivot. - * * @param arr the array to partition * @param low the starting index of the segment to partition * @param high the ending index of the segment to partition @@ -36,14 +34,13 @@ public static void randomizedQuickSort(int[] arr, int low, int high) { */ private static int randomizedPartition(int[] arr, int low, int high) { int pivotIndex = low + rand.nextInt(high - low + 1); - swap(arr, pivotIndex, high); // Move pivot to end + swap(arr, pivotIndex, high); // Move pivot to end return partition(arr, low, high); } /** * Partitions the array segment such that elements less than or equal * to the pivot are to the left, and greater are to the right. - * * @param arr the array to partition * @param low the starting index of the segment to partition * @param high the ending index of the segment to partition @@ -51,7 +48,7 @@ private static int randomizedPartition(int[] arr, int low, int high) { */ private static int partition(int[] arr, int low, int high) { int pivot = arr[high]; - int i = low - 1; // index of smaller element + int i = low - 1; // index of smaller element for (int j = low; j < high; j++) { if (arr[j] <= pivot) { i++; @@ -64,10 +61,9 @@ private static int partition(int[] arr, int low, int high) { /** * Swaps two elements in the array. - * * @param arr the array containing elements to swap * @param i index of first element - * @param j index of second element + * @param j index of second element */ private static void swap(int[] arr, int i, int j) { int temp = arr[i]; @@ -78,7 +74,6 @@ private static void swap(int[] arr, int i, int j) { /** * Main method for basic demonstration. * Sorts a sample array and prints the sorted output. - * * @param args command-line arguments (not used) */ public static void main(String[] args) {