1+ /**
2+ * Algorithm: Quick Sort
3+ * Description: Efficient divide-and-conquer sorting algorithm using partitioning
4+ * Time Complexity: O(n log n) average case, O(n²) worst case
5+ * Space Complexity: O(log n) for recursion stack
6+ *
7+ * @author Pasan Sulakshana Rathnayake
8+ * @version 1.0
9+ * @since 2025-10-03
10+ */
11+
12+ import java .util .*;
13+
14+ public class QuickSort {
15+
16+ /**
17+ * Sorts an array using quick sort algorithm.
18+ *
19+ * @param arr the array to be sorted
20+ * @throws IllegalArgumentException if array is null
21+ */
22+ public static void quickSort (int [] arr ) {
23+ if (arr == null ) {
24+ throw new IllegalArgumentException ("Array cannot be null" );
25+ }
26+
27+ if (arr .length <= 1 ) {
28+ return ; // Already sorted
29+ }
30+
31+ quickSort (arr , 0 , arr .length - 1 );
32+ }
33+
34+ /**
35+ * Recursive quick sort implementation.
36+ *
37+ * @param arr the array to sort
38+ * @param low starting index
39+ * @param high ending index
40+ */
41+ private static void quickSort (int [] arr , int low , int high ) {
42+ if (low < high ) {
43+ // Partition the array and get pivot index
44+ int pivotIndex = partition (arr , low , high );
45+
46+ // Recursively sort elements before and after partition
47+ quickSort (arr , low , pivotIndex - 1 );
48+ quickSort (arr , pivotIndex + 1 , high );
49+ }
50+ }
51+
52+ /**
53+ * Partitions the array around a pivot element.
54+ *
55+ * @param arr the array to partition
56+ * @param low starting index
57+ * @param high ending index
58+ * @return the final position of the pivot
59+ */
60+ private static int partition (int [] arr , int low , int high ) {
61+ // Choose rightmost element as pivot
62+ int pivot = arr [high ];
63+ int i = low - 1 ; // Index of smaller element
64+
65+ for (int j = low ; j < high ; j ++) {
66+ // If current element is smaller than or equal to pivot
67+ if (arr [j ] <= pivot ) {
68+ i ++;
69+ swap (arr , i , j );
70+ }
71+ }
72+
73+ // Place pivot in correct position
74+ swap (arr , i + 1 , high );
75+ return i + 1 ;
76+ }
77+
78+ /**
79+ * Swaps two elements in the array.
80+ *
81+ * @param arr the array
82+ * @param i first index
83+ * @param j second index
84+ */
85+ private static void swap (int [] arr , int i , int j ) {
86+ int temp = arr [i ];
87+ arr [i ] = arr [j ];
88+ arr [j ] = temp ;
89+ }
90+
91+ /**
92+ * Test the quick sort implementation.
93+ */
94+ public static void main (String [] args ) {
95+ // Test case 1: Regular array
96+ int [] test1 = {64 , 34 , 25 , 12 , 22 , 11 , 90 };
97+ System .out .println ("Original array: " + Arrays .toString (test1 ));
98+ quickSort (test1 );
99+ System .out .println ("Sorted array: " + Arrays .toString (test1 ));
100+
101+ // Test case 2: Already sorted
102+ int [] test2 = {1 , 2 , 3 , 4 , 5 };
103+ System .out .println ("\n Already sorted: " + Arrays .toString (test2 ));
104+ quickSort (test2 );
105+ System .out .println ("After sorting: " + Arrays .toString (test2 ));
106+
107+ // Test case 3: Reverse sorted
108+ int [] test3 = {5 , 4 , 3 , 2 , 1 };
109+ System .out .println ("\n Reverse sorted: " + Arrays .toString (test3 ));
110+ quickSort (test3 );
111+ System .out .println ("After sorting: " + Arrays .toString (test3 ));
112+
113+ // Test case 4: Single element
114+ int [] test4 = {42 };
115+ quickSort (test4 );
116+ assert test4 [0 ] == 42 ;
117+
118+ // Test case 5: Empty array
119+ int [] test5 = {};
120+ quickSort (test5 );
121+ assert test5 .length == 0 ;
122+
123+ System .out .println ("\n All test cases passed! ✅" );
124+ }
125+ }
0 commit comments