-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathQuickSort.java
More file actions
48 lines (37 loc) · 1.07 KB
/
Copy pathQuickSort.java
File metadata and controls
48 lines (37 loc) · 1.07 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
package Sort;
import java.util.Arrays;
public class QuickSort {
public static void quickSort(int[] nums,int start, int end){
int n = nums.length;
if(n==0 || n==1){
return;
}
if(start >= end){
return;
}
int pivotIndex = partition(nums,start, end);
quickSort(nums, start, pivotIndex-1);
quickSort(nums, pivotIndex+1, end);
}
public static int partition(int[] nums, int start, int end){
int pivot = nums[end];
int current = start;
for(int i=start; i <end; i++){
if(nums[i] < pivot){
int temp = nums[i];
nums[i] = nums[current];
nums[current] = temp;
current++;
}
}
int temp = nums[end];
nums[end] = nums[current];
nums[current] = temp;
return current;
}
public static void main(String[] args){
int[] nums = {4,5,7,6,2,3,1};
quickSort(nums,0,nums.length-1);
System.out.println(Arrays.toString(nums));
}
}