public class Solution {
/**
* @param A an integer array
* @return void
*/
public void sortIntegers(int[] A) {
// Write your code here
if (A == null || A.length == 0) {
return;
}
quickSort(A, 0, A.length - 1);
}
private void quickSort(int[] A, int start, int end) {
int pivot = A[start + (end - start) / 2];
int index = partition(A, pivot, start, end);
if (start < index - 1) {
quickSort(A, start, index - 1);
}
if (index < end) {
quickSort(A, index, end);
}
}
private int partition(int[] A, int pivot, int start, int end) {
while (start <= end) {
while (start <= end && A[start] < pivot) {
start++;
}
while (start <= end && A[end] > pivot) {
end--;
}
if (start <= end) {
swap(A, start, end);
start++;
end--;
}
}
return start;
}
private void swap(int A[], int i, int j) {
int temp = A[i];
A[i] = A[j];
A[j] = temp;
}
}
2016年9月12日星期一
Quick Sort
订阅:
博文评论 (Atom)
没有评论:
发表评论