-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuickSort.java
More file actions
38 lines (34 loc) · 977 Bytes
/
QuickSort.java
File metadata and controls
38 lines (34 loc) · 977 Bytes
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
package Sorting;
import java.util.Arrays;
import static Util.Swap.swap;
public class QuickSort {
public static void quickSort(int [] nums, int start, int end){
if (start >= end){
return;
}
int mid = nums[(start + end) / 2];
int left = start;
int right = end;
while (left <= right){
while(left <= right && nums[left] < mid){
left += 1;
}
while(left <= right && nums[right] > mid){
right -= 1;
}
if(left <= right){
swap(nums, left, right);
left += 1;
right -= 1;
}
}
quickSort(nums, start, right);
quickSort(nums, left, end);
}
public static void main(String[] args) {
int [] nums = {
5,1,1,2,0,0};
quickSort(nums, 0, nums.length - 1);
System.out.println(Arrays.toString(nums));
}
}