public class Solution {
public void qSort(int[] nums) {
if (nums == null || nums.length <= 1) {
return;
}
qSort(nums, 0, nums.length - 1);
}
private void qSort(int[] a, int l, int r) {
int pivot;
if (l < r) {
pivot = partition(a, l, r);
qSort(a, l, pivot - 1);
qSort(a, pivot + 1, r);
}
}
private int partition(int[] a, int l, int r) {
int pivot = a[l];
while(l < r) {
while (l < r && a[r] >= pivot) {
--r;
}
if (l < r) {
a[l++] = a[r];
}
while (l < r && a[l] <= pivot) {
++l;
}
if (l < r) {
a[r--] = a[l];
}
}
a[l] = pivot;
return l;
}
}
网友评论