Skip to content

Commit c99087e

Browse files
committed
Add Javadoc comments
1 parent 91c104d commit c99087e

File tree

1 file changed

+46
-3
lines changed

1 file changed

+46
-3
lines changed

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

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,22 @@
1+
package com.thealgorithms.sorts;
12
import java.util.Random;
23

4+
/**
5+
* Implementation of the Randomized QuickSort algorithm.
6+
* This algorithm sorts an array by choosing a random pivot element,
7+
* which improves the average performance over traditional QuickSort.
8+
*/
39
public class RandomizedQuickSort {
410

511
private static final Random rand = new Random();
612

13+
/**
14+
* Sorts the array in-place using Randomized QuickSort algorithm.
15+
*
16+
* @param arr the array to be sorted
17+
* @param low the starting index of the segment to sort
18+
* @param high the ending index of the segment to sort
19+
*/
720
public static void randomizedQuickSort(int[] arr, int low, int high) {
821
if (low < high) {
922
int pivotIndex = randomizedPartition(arr, low, high);
@@ -12,15 +25,33 @@ public static void randomizedQuickSort(int[] arr, int low, int high) {
1225
}
1326
}
1427

28+
/**
29+
* Chooses a random pivot, swaps it with the last element,
30+
* then partitions the array around this pivot.
31+
*
32+
* @param arr the array to partition
33+
* @param low the starting index of the segment to partition
34+
* @param high the ending index of the segment to partition
35+
* @return final index position of the pivot element
36+
*/
1537
private static int randomizedPartition(int[] arr, int low, int high) {
1638
int pivotIndex = low + rand.nextInt(high - low + 1);
17-
swap(arr, pivotIndex, high); // Move pivot to end
39+
swap(arr, pivotIndex, high); // Move pivot to end
1840
return partition(arr, low, high);
1941
}
2042

43+
/**
44+
* Partitions the array segment such that elements less than or equal
45+
* to the pivot are to the left, and greater are to the right.
46+
*
47+
* @param arr the array to partition
48+
* @param low the starting index of the segment to partition
49+
* @param high the ending index of the segment to partition
50+
* @return the final position of the pivot element
51+
*/
2152
private static int partition(int[] arr, int low, int high) {
2253
int pivot = arr[high];
23-
int i = low - 1; // index of smaller element
54+
int i = low - 1; // index of smaller element
2455
for (int j = low; j < high; j++) {
2556
if (arr[j] <= pivot) {
2657
i++;
@@ -31,13 +62,25 @@ private static int partition(int[] arr, int low, int high) {
3162
return i + 1;
3263
}
3364

65+
/**
66+
* Swaps two elements in the array.
67+
*
68+
* @param arr the array containing elements to swap
69+
* @param i index of first element
70+
* @param j index of second element
71+
*/
3472
private static void swap(int[] arr, int i, int j) {
3573
int temp = arr[i];
3674
arr[i] = arr[j];
3775
arr[j] = temp;
3876
}
3977

40-
// Test the algorithm
78+
/**
79+
* Main method for basic demonstration.
80+
* Sorts a sample array and prints the sorted output.
81+
*
82+
* @param args command-line arguments (not used)
83+
*/
4184
public static void main(String[] args) {
4285
int[] arr = {10, 7, 8, 9, 1, 5};
4386
randomizedQuickSort(arr, 0, arr.length - 1);

0 commit comments

Comments
 (0)