Skip to content

Commit 81daef5

Browse files
committed
Randomize Quick Sort Code Formating
1 parent 3f3e319 commit 81daef5

File tree

2 files changed

+47
-8
lines changed

2 files changed

+47
-8
lines changed

src/main/java/com/thealgorithms/randomized/RandomizedQuickSort.java

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,18 @@
11
package com.thealgorithms.randomized;
22

3+
/**
4+
* This class implements the Randomized QuickSort algorithm.
5+
* It selects a pivot randomly to improve performance on sorted or nearly sorted data.
6+
*/
37
public class RandomizedQuickSort {
48

9+
/**
10+
* Sorts the array using the randomized quicksort algorithm.
11+
*
12+
* @param arr the array to sort
13+
* @param low the starting index of the array
14+
* @param high the ending index of the array
15+
*/
516
public static void randomizedQuickSort(int[] arr, int low, int high) {
617
if (low < high) {
718
int pivotIndex = partition(arr, low, high);
@@ -10,6 +21,14 @@ public static void randomizedQuickSort(int[] arr, int low, int high) {
1021
}
1122
}
1223

24+
/**
25+
* Partitions the array around a pivot chosen randomly.
26+
*
27+
* @param arr the array to partition
28+
* @param low the starting index
29+
* @param high the ending index
30+
* @return the index of the pivot after partitioning
31+
*/
1332
private static int partition(int[] arr, int low, int high) {
1433
int pivotIndex = low + (int) (Math.random() * (high - low + 1));
1534
int pivotValue = arr[pivotIndex];
@@ -25,10 +44,16 @@ private static int partition(int[] arr, int low, int high) {
2544
return storeIndex;
2645
}
2746

47+
/**
48+
* Swaps two elements in the array.
49+
*
50+
* @param arr the array in which elements are to be swapped
51+
* @param i the first index
52+
* @param j the second index
53+
*/
2854
private static void swap(int[] arr, int i, int j) {
2955
int temp = arr[i];
3056
arr[i] = arr[j];
3157
arr[j] = temp;
3258
}
33-
3459
}

src/test/java/com/thealgorithms/randomized/RandomizedQuickSortTest.java

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,41 @@
44

55
import org.junit.jupiter.api.Test;
66

7+
/**
8+
* Unit tests for the RandomizedQuickSort class.
9+
*/
710
public class RandomizedQuickSortTest {
11+
12+
/**
13+
* Tests sorting of an array with multiple elements, including duplicates.
14+
*/
815
@Test
9-
public void testRandomizedQuickSort() {
16+
public void testRandomizedQuickSort_multipleElements() {
1017
int[] arr = {3, 6, 8, 10, 1, 2, 1};
11-
RandomizedQuickSort.randomizedQuickSort(arr, 0, arr.length - 1);
1218
int[] expected = {1, 1, 2, 3, 6, 8, 10};
19+
RandomizedQuickSort.randomizedQuickSort(arr, 0, arr.length - 1);
1320
assertArrayEquals(expected, arr);
1421
}
22+
23+
/**
24+
* Tests sorting of an empty array.
25+
*/
1526
@Test
16-
public void testRandomizedQuickSortEmptyArray() {
27+
public void testRandomizedQuickSort_emptyArray() {
1728
int[] arr = {};
18-
RandomizedQuickSort.randomizedQuickSort(arr, 0, arr.length - 1);
1929
int[] expected = {};
30+
RandomizedQuickSort.randomizedQuickSort(arr, 0, arr.length - 1);
2031
assertArrayEquals(expected, arr);
2132
}
33+
34+
/**
35+
* Tests sorting of an array with a single element.
36+
*/
2237
@Test
23-
public void testRandomizedQuickSortSingleElementArray() {
38+
public void testRandomizedQuickSort_singleElement() {
2439
int[] arr = {5};
25-
RandomizedQuickSort.randomizedQuickSort(arr, 0, arr.length - 1);
2640
int[] expected = {5};
41+
RandomizedQuickSort.randomizedQuickSort(arr, 0, arr.length - 1);
2742
assertArrayEquals(expected, arr);
2843
}
29-
3044
}

0 commit comments

Comments
 (0)