File tree Expand file tree Collapse file tree 1 file changed +33
-0
lines changed Expand file tree Collapse file tree 1 file changed +33
-0
lines changed Original file line number Diff line number Diff line change
1
+ public class QuickSort {
2
+
3
+ public static int partition (int [] arr , int low , int high ){
4
+ int pivot = arr [high ]; // taking pivot at rightmost pos
5
+ int start_ptr = low - 1 ;
6
+ for (int i = low ; i < high ; i ++){
7
+ if (arr [i ] < pivot ){
8
+ start_ptr ++;
9
+
10
+ int tmp = arr [i ];
11
+ arr [i ] = arr [start_ptr ];
12
+ arr [start_ptr ] = tmp ;
13
+ }
14
+ }
15
+
16
+ // swaping the last element i.e. "pivot"
17
+ int temp = arr [high ];
18
+ arr [high ] = arr [start_ptr + 1 ];
19
+ arr [start_ptr + 1 ] = temp ;
20
+
21
+ return start_ptr + 1 ;
22
+ }
23
+
24
+ public static void sort (int [] arr , int low , int high ){
25
+ if (low < high ){
26
+ int pi_index = partition (arr , low ,high );
27
+
28
+ partition (arr , low , pi_index - 1 );
29
+ partition (arr , pi_index + 1 , high );
30
+ }
31
+ }
32
+
33
+ }
You can’t perform that action at this time.
0 commit comments