From 3f3e319716fb7366cb43dfa2484cfb8838db2610 Mon Sep 17 00:00:00 2001 From: Vibhu Khera Date: Wed, 7 May 2025 00:41:09 +0530 Subject: [PATCH 01/10] Randomize Quick Sort Addition --- .../randomized/RandomizedQuickSort.java | 34 +++++++++++++++++++ .../randomized/RandomizedQuickSortTest.java | 30 ++++++++++++++++ 2 files changed, 64 insertions(+) create mode 100644 src/main/java/com/thealgorithms/randomized/RandomizedQuickSort.java create mode 100644 src/test/java/com/thealgorithms/randomized/RandomizedQuickSortTest.java diff --git a/src/main/java/com/thealgorithms/randomized/RandomizedQuickSort.java b/src/main/java/com/thealgorithms/randomized/RandomizedQuickSort.java new file mode 100644 index 000000000000..13ebe0cd59f5 --- /dev/null +++ b/src/main/java/com/thealgorithms/randomized/RandomizedQuickSort.java @@ -0,0 +1,34 @@ +package com.thealgorithms.randomized; + +public class RandomizedQuickSort { + + public static void randomizedQuickSort(int[] arr, int low, int high) { + if (low < high) { + int pivotIndex = partition(arr, low, high); + randomizedQuickSort(arr, low, pivotIndex - 1); + randomizedQuickSort(arr, pivotIndex + 1, high); + } + } + + private static int partition(int[] arr, int low, int high) { + int pivotIndex = low + (int) (Math.random() * (high - low + 1)); + int pivotValue = arr[pivotIndex]; + swap(arr, pivotIndex, high); // Move pivot to end + int storeIndex = low; + for (int i = low; i < high; i++) { + if (arr[i] < pivotValue) { + swap(arr, storeIndex, i); + storeIndex++; + } + } + swap(arr, storeIndex, high); // Move pivot to its final place + return storeIndex; + } + + private static void swap(int[] arr, int i, int j) { + int temp = arr[i]; + arr[i] = arr[j]; + arr[j] = temp; + } + +} diff --git a/src/test/java/com/thealgorithms/randomized/RandomizedQuickSortTest.java b/src/test/java/com/thealgorithms/randomized/RandomizedQuickSortTest.java new file mode 100644 index 000000000000..efd56a633121 --- /dev/null +++ b/src/test/java/com/thealgorithms/randomized/RandomizedQuickSortTest.java @@ -0,0 +1,30 @@ +package com.thealgorithms.randomized; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + +import org.junit.jupiter.api.Test; + +public class RandomizedQuickSortTest { + @Test + public void testRandomizedQuickSort() { + int[] arr = {3, 6, 8, 10, 1, 2, 1}; + RandomizedQuickSort.randomizedQuickSort(arr, 0, arr.length - 1); + int[] expected = {1, 1, 2, 3, 6, 8, 10}; + assertArrayEquals(expected, arr); + } + @Test + public void testRandomizedQuickSortEmptyArray() { + int[] arr = {}; + RandomizedQuickSort.randomizedQuickSort(arr, 0, arr.length - 1); + int[] expected = {}; + assertArrayEquals(expected, arr); + } + @Test + public void testRandomizedQuickSortSingleElementArray() { + int[] arr = {5}; + RandomizedQuickSort.randomizedQuickSort(arr, 0, arr.length - 1); + int[] expected = {5}; + assertArrayEquals(expected, arr); + } + +} From 81daef5fbdfd76cd273f7042c629f957fb51f3c0 Mon Sep 17 00:00:00 2001 From: Vibhu Khera Date: Wed, 7 May 2025 01:03:10 +0530 Subject: [PATCH 02/10] Randomize Quick Sort Code Formating --- .../randomized/RandomizedQuickSort.java | 27 +++++++++++++++++- .../randomized/RandomizedQuickSortTest.java | 28 ++++++++++++++----- 2 files changed, 47 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/thealgorithms/randomized/RandomizedQuickSort.java b/src/main/java/com/thealgorithms/randomized/RandomizedQuickSort.java index 13ebe0cd59f5..9b36233c58a0 100644 --- a/src/main/java/com/thealgorithms/randomized/RandomizedQuickSort.java +++ b/src/main/java/com/thealgorithms/randomized/RandomizedQuickSort.java @@ -1,7 +1,18 @@ package com.thealgorithms.randomized; +/** + * This class implements the Randomized QuickSort algorithm. + * It selects a pivot randomly to improve performance on sorted or nearly sorted data. + */ public class RandomizedQuickSort { + /** + * Sorts the array using the randomized quicksort algorithm. + * + * @param arr the array to sort + * @param low the starting index of the array + * @param high the ending index of the array + */ public static void randomizedQuickSort(int[] arr, int low, int high) { if (low < high) { int pivotIndex = partition(arr, low, high); @@ -10,6 +21,14 @@ public static void randomizedQuickSort(int[] arr, int low, int high) { } } + /** + * Partitions the array around a pivot chosen randomly. + * + * @param arr the array to partition + * @param low the starting index + * @param high the ending index + * @return the index of the pivot after partitioning + */ private static int partition(int[] arr, int low, int high) { int pivotIndex = low + (int) (Math.random() * (high - low + 1)); int pivotValue = arr[pivotIndex]; @@ -25,10 +44,16 @@ private static int partition(int[] arr, int low, int high) { return storeIndex; } + /** + * Swaps two elements in the array. + * + * @param arr the array in which elements are to be swapped + * @param i the first index + * @param j the second index + */ private static void swap(int[] arr, int i, int j) { int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } - } diff --git a/src/test/java/com/thealgorithms/randomized/RandomizedQuickSortTest.java b/src/test/java/com/thealgorithms/randomized/RandomizedQuickSortTest.java index efd56a633121..6dce33dc9b2e 100644 --- a/src/test/java/com/thealgorithms/randomized/RandomizedQuickSortTest.java +++ b/src/test/java/com/thealgorithms/randomized/RandomizedQuickSortTest.java @@ -4,27 +4,41 @@ import org.junit.jupiter.api.Test; +/** + * Unit tests for the RandomizedQuickSort class. + */ public class RandomizedQuickSortTest { + + /** + * Tests sorting of an array with multiple elements, including duplicates. + */ @Test - public void testRandomizedQuickSort() { + public void testRandomizedQuickSort_multipleElements() { int[] arr = {3, 6, 8, 10, 1, 2, 1}; - RandomizedQuickSort.randomizedQuickSort(arr, 0, arr.length - 1); int[] expected = {1, 1, 2, 3, 6, 8, 10}; + RandomizedQuickSort.randomizedQuickSort(arr, 0, arr.length - 1); assertArrayEquals(expected, arr); } + + /** + * Tests sorting of an empty array. + */ @Test - public void testRandomizedQuickSortEmptyArray() { + public void testRandomizedQuickSort_emptyArray() { int[] arr = {}; - RandomizedQuickSort.randomizedQuickSort(arr, 0, arr.length - 1); int[] expected = {}; + RandomizedQuickSort.randomizedQuickSort(arr, 0, arr.length - 1); assertArrayEquals(expected, arr); } + + /** + * Tests sorting of an array with a single element. + */ @Test - public void testRandomizedQuickSortSingleElementArray() { + public void testRandomizedQuickSort_singleElement() { int[] arr = {5}; - RandomizedQuickSort.randomizedQuickSort(arr, 0, arr.length - 1); int[] expected = {5}; + RandomizedQuickSort.randomizedQuickSort(arr, 0, arr.length - 1); assertArrayEquals(expected, arr); } - } From f40ad893083a6c79fc5419627e1bae596959ef94 Mon Sep 17 00:00:00 2001 From: Vibhu Khera Date: Wed, 7 May 2025 01:11:25 +0530 Subject: [PATCH 03/10] Randomize Quick Sort Addition --- .../com/thealgorithms/randomized/RandomizedQuickSort.java | 5 +++++ .../thealgorithms/randomized/RandomizedQuickSortTest.java | 6 +++--- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/thealgorithms/randomized/RandomizedQuickSort.java b/src/main/java/com/thealgorithms/randomized/RandomizedQuickSort.java index 9b36233c58a0..c65719b61128 100644 --- a/src/main/java/com/thealgorithms/randomized/RandomizedQuickSort.java +++ b/src/main/java/com/thealgorithms/randomized/RandomizedQuickSort.java @@ -3,9 +3,14 @@ /** * This class implements the Randomized QuickSort algorithm. * It selects a pivot randomly to improve performance on sorted or nearly sorted data. + * @author Vibhu Khera */ public class RandomizedQuickSort { + private RandomizedQuickSort() { + throw new UnsupportedOperationException("Utility class"); + } + /** * Sorts the array using the randomized quicksort algorithm. * diff --git a/src/test/java/com/thealgorithms/randomized/RandomizedQuickSortTest.java b/src/test/java/com/thealgorithms/randomized/RandomizedQuickSortTest.java index 6dce33dc9b2e..ec3d5a0b3546 100644 --- a/src/test/java/com/thealgorithms/randomized/RandomizedQuickSortTest.java +++ b/src/test/java/com/thealgorithms/randomized/RandomizedQuickSortTest.java @@ -13,7 +13,7 @@ public class RandomizedQuickSortTest { * Tests sorting of an array with multiple elements, including duplicates. */ @Test - public void testRandomizedQuickSort_multipleElements() { + public void testRandomizedQuickSortMultipleElements() { int[] arr = {3, 6, 8, 10, 1, 2, 1}; int[] expected = {1, 1, 2, 3, 6, 8, 10}; RandomizedQuickSort.randomizedQuickSort(arr, 0, arr.length - 1); @@ -24,7 +24,7 @@ public void testRandomizedQuickSort_multipleElements() { * Tests sorting of an empty array. */ @Test - public void testRandomizedQuickSort_emptyArray() { + public void testRandomizedQuickSortEmptyArray() { int[] arr = {}; int[] expected = {}; RandomizedQuickSort.randomizedQuickSort(arr, 0, arr.length - 1); @@ -35,7 +35,7 @@ public void testRandomizedQuickSort_emptyArray() { * Tests sorting of an array with a single element. */ @Test - public void testRandomizedQuickSort_singleElement() { + public void testRandomizedQuickSortSingleElement() { int[] arr = {5}; int[] expected = {5}; RandomizedQuickSort.randomizedQuickSort(arr, 0, arr.length - 1); From 92213dcedd9e911631945d4c228c696b38aedaf5 Mon Sep 17 00:00:00 2001 From: Vibhu Khera Date: Wed, 7 May 2025 01:16:13 +0530 Subject: [PATCH 04/10] Randomize Quick Sort Run Fixes for final class --- .../java/com/thealgorithms/randomized/RandomizedQuickSort.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/randomized/RandomizedQuickSort.java b/src/main/java/com/thealgorithms/randomized/RandomizedQuickSort.java index c65719b61128..354e2d1fe51b 100644 --- a/src/main/java/com/thealgorithms/randomized/RandomizedQuickSort.java +++ b/src/main/java/com/thealgorithms/randomized/RandomizedQuickSort.java @@ -5,7 +5,7 @@ * It selects a pivot randomly to improve performance on sorted or nearly sorted data. * @author Vibhu Khera */ -public class RandomizedQuickSort { +public final class RandomizedQuickSort { private RandomizedQuickSort() { throw new UnsupportedOperationException("Utility class"); From 4798de965cf5a4456b235d4ba72171b7a3ae31e5 Mon Sep 17 00:00:00 2001 From: Vibhu Khera Date: Thu, 8 May 2025 23:36:19 +0530 Subject: [PATCH 05/10] Swap function update to return for i == j --- .../randomized/RandomizedQuickSort.java | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/thealgorithms/randomized/RandomizedQuickSort.java b/src/main/java/com/thealgorithms/randomized/RandomizedQuickSort.java index 354e2d1fe51b..28427445449e 100644 --- a/src/main/java/com/thealgorithms/randomized/RandomizedQuickSort.java +++ b/src/main/java/com/thealgorithms/randomized/RandomizedQuickSort.java @@ -50,13 +50,16 @@ private static int partition(int[] arr, int low, int high) { } /** - * Swaps two elements in the array. - * - * @param arr the array in which elements are to be swapped - * @param i the first index - * @param j the second index - */ + * Swaps two elements in the array, only if the indices are different. + * + * @param arr the array in which elements are to be swapped + * @param i the first index + * @param j the second index + */ private static void swap(int[] arr, int i, int j) { + if (i == j) { + return; + } int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; From 9bd674940ed3889e67666b199814838edca127a8 Mon Sep 17 00:00:00 2001 From: Vibhu Khera Date: Thu, 8 May 2025 23:40:52 +0530 Subject: [PATCH 06/10] Clang formatting for swap function --- .../com/thealgorithms/randomized/RandomizedQuickSort.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/main/java/com/thealgorithms/randomized/RandomizedQuickSort.java b/src/main/java/com/thealgorithms/randomized/RandomizedQuickSort.java index 28427445449e..4ca3e7bdf683 100644 --- a/src/main/java/com/thealgorithms/randomized/RandomizedQuickSort.java +++ b/src/main/java/com/thealgorithms/randomized/RandomizedQuickSort.java @@ -57,9 +57,7 @@ private static int partition(int[] arr, int low, int high) { * @param j the second index */ private static void swap(int[] arr, int i, int j) { - if (i == j) { - return; - } + if (i == j) return; int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; From 65ddffc74b52ee2ae09be4c6c3bdd31a8b246d70 Mon Sep 17 00:00:00 2001 From: Vibhu Khera Date: Thu, 8 May 2025 23:42:54 +0530 Subject: [PATCH 07/10] Clang formatting for swap function --- .../randomized/RandomizedQuickSort.java | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/thealgorithms/randomized/RandomizedQuickSort.java b/src/main/java/com/thealgorithms/randomized/RandomizedQuickSort.java index 4ca3e7bdf683..a8b88ca8ab04 100644 --- a/src/main/java/com/thealgorithms/randomized/RandomizedQuickSort.java +++ b/src/main/java/com/thealgorithms/randomized/RandomizedQuickSort.java @@ -50,14 +50,14 @@ private static int partition(int[] arr, int low, int high) { } /** - * Swaps two elements in the array, only if the indices are different. - * - * @param arr the array in which elements are to be swapped - * @param i the first index - * @param j the second index - */ + * Swaps two elements in the array, only if the indices are different. + * + * @param arr the array in which elements are to be swapped + * @param i the first index + * @param j the second index + */ private static void swap(int[] arr, int i, int j) { - if (i == j) return; + if (i == j) return; // Skip if indices are the same int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; From 1ba58d2c085e7fb86aa468c910a7cd454847f76b Mon Sep 17 00:00:00 2001 From: Vibhu Khera Date: Thu, 8 May 2025 23:48:31 +0530 Subject: [PATCH 08/10] Build issue fix for swap function --- .../com/thealgorithms/randomized/RandomizedQuickSort.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/randomized/RandomizedQuickSort.java b/src/main/java/com/thealgorithms/randomized/RandomizedQuickSort.java index a8b88ca8ab04..38a814f48c9e 100644 --- a/src/main/java/com/thealgorithms/randomized/RandomizedQuickSort.java +++ b/src/main/java/com/thealgorithms/randomized/RandomizedQuickSort.java @@ -57,7 +57,10 @@ private static int partition(int[] arr, int low, int high) { * @param j the second index */ private static void swap(int[] arr, int i, int j) { - if (i == j) return; // Skip if indices are the same + // Skip if indices are the same + if (i == j) { + return; + } int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; From cab6e2a87889a610d41cb7ed43513cccb69e3460 Mon Sep 17 00:00:00 2001 From: Vibhu Khera Date: Thu, 8 May 2025 23:49:53 +0530 Subject: [PATCH 09/10] Build issue fix for swap function --- .../com/thealgorithms/randomized/RandomizedQuickSort.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/main/java/com/thealgorithms/randomized/RandomizedQuickSort.java b/src/main/java/com/thealgorithms/randomized/RandomizedQuickSort.java index 38a814f48c9e..c9009ee97685 100644 --- a/src/main/java/com/thealgorithms/randomized/RandomizedQuickSort.java +++ b/src/main/java/com/thealgorithms/randomized/RandomizedQuickSort.java @@ -58,9 +58,7 @@ private static int partition(int[] arr, int low, int high) { */ private static void swap(int[] arr, int i, int j) { // Skip if indices are the same - if (i == j) { - return; - } + if (i == j) {return;} int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; From 04543d6369abc4e332865f750bd6d53de667b3fb Mon Sep 17 00:00:00 2001 From: Vibhu Khera Date: Thu, 8 May 2025 23:51:11 +0530 Subject: [PATCH 10/10] Build issue fix for swap function --- .../com/thealgorithms/randomized/RandomizedQuickSort.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/randomized/RandomizedQuickSort.java b/src/main/java/com/thealgorithms/randomized/RandomizedQuickSort.java index c9009ee97685..e9af223a0622 100644 --- a/src/main/java/com/thealgorithms/randomized/RandomizedQuickSort.java +++ b/src/main/java/com/thealgorithms/randomized/RandomizedQuickSort.java @@ -58,7 +58,9 @@ private static int partition(int[] arr, int low, int high) { */ private static void swap(int[] arr, int i, int j) { // Skip if indices are the same - if (i == j) {return;} + if (i == j) { + return; + } int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp;