-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuickSortRecursion.java
More file actions
51 lines (33 loc) · 1.06 KB
/
QuickSortRecursion.java
File metadata and controls
51 lines (33 loc) · 1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
public class Solution {
public static void quick(int arr[], int low , int high){
if(low < high){
int pi = partition(arr, low, high);
quick(arr, low, pi-1);
quick(arr, pi + 1, high);
}
}
public static int partition(int arr[], int low, int high){
//pivot initiliz n-1 element index
int pivot = arr[high];
// i initiliz -1
int i = low - 1;
for(int j=low; j<high; j++){
if(arr[j] < pivot){
i++;
//Swap the element
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
i++;
//Swap tha pivot element
int temp = arr[i];
arr[i] = arr[high];
arr[high] = temp;
return i;
}
public static void quickSort(int[] arr) {
quick(arr, 0, arr.length-1);
}
}