33import java .util .Arrays ;
44
55/**
6- * Created by IntelliJ IDEA .
6+ * The basic Quick Sort. See http://www.csanimated.com/animation.php?t=Quicksort to learn more .
77 *
88 * @author rampatra
99 * @since 7/21/15
10- * @time: 4:12 PM
11- * @see: http://www.csanimated.com/animation.php?t=Quicksort
1210 */
1311public class QuickSort {
1412
@@ -18,32 +16,30 @@ public class QuickSort {
1816 * pivot element to its right and finally places the pivot element
1917 * at its correct position.
2018 *
21- * @param ar
19+ * @param arr
2220 * @param startIndex
2321 * @param endIndex
2422 * @return position of the pivot element
2523 */
26- public static int partition (int [] ar , int startIndex , int endIndex ) {
24+ public static int partition (int [] arr , int startIndex , int endIndex ) {
2725 int pivot = endIndex , temp ;
2826
2927 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 ]) {
3531 // 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 ;
3935 startIndex ++;
4036 }
4137 }
4238
4339 // 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 ;
4743
4844 return startIndex ;
4945 }
@@ -57,31 +53,52 @@ public static int partition(int[] ar, int startIndex, int endIndex) {
5753 * Best Case: O(nlogn)
5854 * Worst Case: O(n*n)
5955 *
60- * @param ar
56+ * @param arr
6157 * @param startIndex
6258 * @param endIndex
6359 */
64- public static void quickSort (int [] ar , int startIndex , int endIndex ) {
60+ public static void quickSort (int [] arr , int startIndex , int endIndex ) {
6561 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 );
6965 }
7066 }
7167
7268 /**
7369 * Wrapper method to quick sort the entire array.
7470 *
75- * @param a
71+ * @param arr the input array to sort
7672 */
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 );
7975 }
8076
81- public static void main (String a [] ) {
77+ public static void main (String [] a ) {
8278 int [] ar = {3 , 2 , 1 , 6 , 4 , 9 , 7 , 8 };
8379 System .out .println (Arrays .toString (ar ));
8480 quickSort (ar );
8581 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 ));
86103 }
87104}
0 commit comments