66 * This algorithm sorts an array by choosing a random pivot element,
77 * which improves the average performance over traditional QuickSort.
88 */
9- public class RandomizedQuickSort {
9+ private class RandomizedQuickSort {
1010
11- private static final Random rand = new Random ();
11+ private static final Random RAND = new Random ();
1212
1313 /**
1414 * Sorts the array in-place using Randomized QuickSort algorithm.
15- *
1615 * @param arr the array to be sorted
1716 * @param low the starting index of the segment to sort
1817 * @param high the ending index of the segment to sort
@@ -28,30 +27,28 @@ public static void randomizedQuickSort(int[] arr, int low, int high) {
2827 /**
2928 * Chooses a random pivot, swaps it with the last element,
3029 * then partitions the array around this pivot.
31- *
3230 * @param arr the array to partition
3331 * @param low the starting index of the segment to partition
3432 * @param high the ending index of the segment to partition
3533 * @return final index position of the pivot element
3634 */
3735 private static int randomizedPartition (int [] arr , int low , int high ) {
3836 int pivotIndex = low + rand .nextInt (high - low + 1 );
39- swap (arr , pivotIndex , high ); // Move pivot to end
37+ swap (arr , pivotIndex , high ); // Move pivot to end
4038 return partition (arr , low , high );
4139 }
4240
4341 /**
4442 * Partitions the array segment such that elements less than or equal
4543 * to the pivot are to the left, and greater are to the right.
46- *
4744 * @param arr the array to partition
4845 * @param low the starting index of the segment to partition
4946 * @param high the ending index of the segment to partition
5047 * @return the final position of the pivot element
5148 */
5249 private static int partition (int [] arr , int low , int high ) {
5350 int pivot = arr [high ];
54- int i = low - 1 ; // index of smaller element
51+ int i = low - 1 ; // index of smaller element
5552 for (int j = low ; j < high ; j ++) {
5653 if (arr [j ] <= pivot ) {
5754 i ++;
@@ -64,10 +61,9 @@ private static int partition(int[] arr, int low, int high) {
6461
6562 /**
6663 * Swaps two elements in the array.
67- *
6864 * @param arr the array containing elements to swap
6965 * @param i index of first element
70- * @param j index of second element
66+ * @param j index of second element
7167 */
7268 private static void swap (int [] arr , int i , int j ) {
7369 int temp = arr [i ];
@@ -78,7 +74,6 @@ private static void swap(int[] arr, int i, int j) {
7874 /**
7975 * Main method for basic demonstration.
8076 * Sorts a sample array and prints the sorted output.
81- *
8277 * @param args command-line arguments (not used)
8378 */
8479 public static void main (String [] args ) {
0 commit comments