3
3
import java .util .Arrays ;
4
4
5
5
/**
6
- * Created by IntelliJ IDEA .
6
+ * The basic Quick Sort. See http://www.csanimated.com/animation.php?t=Quicksort to learn more .
7
7
*
8
8
* @author rampatra
9
9
* @since 7/21/15
10
- * @time: 4:12 PM
11
- * @see: http://www.csanimated.com/animation.php?t=Quicksort
12
10
*/
13
11
public class QuickSort {
14
12
@@ -18,32 +16,30 @@ public class QuickSort {
18
16
* pivot element to its right and finally places the pivot element
19
17
* at its correct position.
20
18
*
21
- * @param ar
19
+ * @param arr
22
20
* @param startIndex
23
21
* @param endIndex
24
22
* @return position of the pivot element
25
23
*/
26
- public static int partition (int [] ar , int startIndex , int endIndex ) {
24
+ public static int partition (int [] arr , int startIndex , int endIndex ) {
27
25
int pivot = endIndex , temp ;
28
26
29
27
for (int i = startIndex ; i < endIndex ; i ++) {
30
- /**
31
- * if ith element is smaller than pivot element then
32
- * swap it with the last larger element known
33
- */
34
- if (ar [i ] < ar [pivot ]) {
28
+ // if ith element is smaller than pivot element then
29
+ // swap it with the last larger element known
30
+ if (arr [i ] < arr [pivot ]) {
35
31
// swap a[startIndex] with a[i]
36
- temp = ar [startIndex ];
37
- ar [startIndex ] = ar [i ];
38
- ar [i ] = temp ;
32
+ temp = arr [startIndex ];
33
+ arr [startIndex ] = arr [i ];
34
+ arr [i ] = temp ;
39
35
startIndex ++;
40
36
}
41
37
}
42
38
43
39
// place the pivot element in its correct position
44
- temp = ar [startIndex ];
45
- ar [startIndex ] = ar [pivot ];
46
- ar [pivot ] = temp ;
40
+ temp = arr [startIndex ];
41
+ arr [startIndex ] = arr [pivot ];
42
+ arr [pivot ] = temp ;
47
43
48
44
return startIndex ;
49
45
}
@@ -57,31 +53,52 @@ public static int partition(int[] ar, int startIndex, int endIndex) {
57
53
* Best Case: O(nlogn)
58
54
* Worst Case: O(n*n)
59
55
*
60
- * @param ar
56
+ * @param arr
61
57
* @param startIndex
62
58
* @param endIndex
63
59
*/
64
- public static void quickSort (int [] ar , int startIndex , int endIndex ) {
60
+ public static void quickSort (int [] arr , int startIndex , int endIndex ) {
65
61
if (startIndex < endIndex ) {
66
- int partition = partition (ar , startIndex , endIndex );
67
- quickSort (ar , startIndex , partition - 1 );
68
- quickSort (ar , partition + 1 , endIndex );
62
+ int partition = partition (arr , startIndex , endIndex );
63
+ quickSort (arr , startIndex , partition - 1 );
64
+ quickSort (arr , partition + 1 , endIndex );
69
65
}
70
66
}
71
67
72
68
/**
73
69
* Wrapper method to quick sort the entire array.
74
70
*
75
- * @param a
71
+ * @param arr the input array to sort
76
72
*/
77
- public static void quickSort (int [] a ) {
78
- quickSort (a , 0 , a .length - 1 );
73
+ public static void quickSort (int [] arr ) {
74
+ quickSort (arr , 0 , arr .length - 1 );
79
75
}
80
76
81
- public static void main (String a [] ) {
77
+ public static void main (String [] a ) {
82
78
int [] ar = {3 , 2 , 1 , 6 , 4 , 9 , 7 , 8 };
83
79
System .out .println (Arrays .toString (ar ));
84
80
quickSort (ar );
85
81
System .out .println (Arrays .toString (ar ));
82
+
83
+ System .out .println ("------" );
84
+
85
+ ar = new int []{3 , 2 , 6 , 8 , 4 , 3 , 7 , 8 };
86
+ System .out .println (Arrays .toString (ar ));
87
+ quickSort (ar );
88
+ System .out .println (Arrays .toString (ar ));
89
+
90
+ System .out .println ("------" );
91
+
92
+ ar = new int []{4 , 4 , 4 , 4 , 4 , 4 , 4 };
93
+ System .out .println (Arrays .toString (ar ));
94
+ quickSort (ar );
95
+ System .out .println (Arrays .toString (ar ));
96
+
97
+ System .out .println ("------" );
98
+
99
+ ar = new int []{};
100
+ System .out .println (Arrays .toString (ar ));
101
+ quickSort (ar );
102
+ System .out .println (Arrays .toString (ar ));
86
103
}
87
104
}
0 commit comments