Skip to content

Commit 0eb0af0

Browse files
committed
add quickSort
1 parent 0256587 commit 0eb0af0

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
2+
public class Recursion2_QuickSort {
3+
4+
5+
public static int partition (int[] arr, int low, int high){
6+
int pivot = arr[high];
7+
//int i = (low-1);
8+
int count = low-1;
9+
for(int j = low; j<high; j++){
10+
11+
if (arr[j]<pivot){
12+
count++;
13+
14+
//swap arr[i] and arr[j]
15+
16+
int temp = arr[count];
17+
arr[count] = arr[j];
18+
arr[j] = temp;
19+
}
20+
}
21+
22+
//swap arr[i+1] and pivot
23+
24+
int temp = arr[count+1];
25+
arr[count+1] = arr[high];
26+
arr[high] = temp;
27+
28+
return count+1;
29+
}
30+
31+
32+
public static void sort(int[] arr, int low, int high){
33+
if (low<high){
34+
int pivot = partition(arr, low, high);
35+
sort(arr, low, pivot-1);
36+
sort(arr, pivot+1, high);
37+
}
38+
}
39+
40+
public static void quickSort(int[] input) {
41+
/* Your class should be named Solution
42+
* Don't write main().
43+
* Don't read input, it is passed as function argument.
44+
* No need to print or return the output.
45+
* Taking input and printing output is handled automatically.
46+
*/
47+
int start = 0;
48+
int end = input.length-1;
49+
sort(input, start, end);
50+
51+
}
52+
53+
54+
}

0 commit comments

Comments
 (0)