Quick sort algorithm is quite like the merge sort, cause both of them use the idea of divide-and-conquer, but be aware, quick sort doesn't conquer it subproblems, cause when the problem is divided into small enough (only 1 element in the sub array), the whole array have already been sorted.
The worst case running time of quick sort is O(n^2), this can happen when the array is already sorted, so the total running time is T(n) = T(n-1) + /theta(n), and we can have T(n) = O(n^2). To avoid such case, we can randomly pick one as the pivot and swap it with the last element in the array, and the running time will be O(n lgn) because T(n) = 2 T(n/2) + /theta(n) .
public class QuickSortRecursive {
public static void main(String[] args) {
QuickSortRecursive qsr = new QuickSortRecursive();
int[] array = {0, -1};
qsr.quickSort(array, 0, array.length - 1);
for (int i : array) {
System.out.print(i + " ");
}
}
public void quickSort(int[] array, int begin, int end) {
if (begin < end) {
//get the pivot position, and divide.
int partitionPosition = partition(array, begin, end);
// the left part of the array.
quickSort(array, begin, partitionPosition - 1);
// the right part of the array.
quickSort(array, partitionPosition + 1, end);
}
}
public int partition(int[] array, int begin, int end) {
Random rd = new Random();
int tempPivot = rd.nextInt(end - begin + 1) + begin;
swap(tempPivot, end, array);
int startPoint = begin;
for (int j = begin; j < end; j++) {
if (array[j] < array[end]) {
swap(startPoint, j, array);
startPoint++;
}
}
swap(end, startPoint, array);
return startPoint;
}
private void swap(int p1, int p2, int[] array)
{
int temp = array[p1];
array[p1] = array[p2];
array[p2] = temp;
}
}
The non-recursive quicksort stores the pair of left index and right index in the stack, and each time the array is divided, the new pair of left and right indices are stored. If the left index is larger than or equal to the right index, we will not store that pair of indices because the number of element in that segment should be only one.
public class QuickSortNonRecursive {
public static void main(String[] args) {
QuickSortNonRecursive qsnr = new QuickSortNonRecursive();
int[] array = {0, 2, -11, 2, 18, 99, 3, 5, 11, 22, 9, 100};
qsnr.quicksort(array);
for (int i : array) {
System.out.print(i + " ");
}
}
public void quicksort(int[] array) {
if (array == null || array.length == 1) return;
//store the pair of start point and end point
Stack<Integer> s = new Stack<Integer>();
s.push(0);
s.push(array.length - 1);
while (!s.empty()) {
int right = s.pop();
int left = s.pop();
//if right <= left, the subarray only contains
// one or null element
if (right <= left) continue;
int i = partition(array, left, right);
//add the the pair of start point and end point
//to stack
if (left < i - 1) {
s.push(left);
s.push(i - 1);
}
if (i + 1 < right) {
s.push(i+1);
s.push(right);
}
}
}
public int partition (int[] array, int left, int right) {
int position = left;
int pointer = left;
while (pointer < right) {
if (array[pointer] < array[right]) {
swap(array, pointer, position);
position++;
}
pointer++;
}
swap(array, position, right);
return position;
}
public void swap(int[] array, int index1, int index2) {
int temp = array[index1];
array[index1] = array[index2];
array[index2] = temp;
}
}
转载请注明出处:http://blog.csdn.net/beiyeqingteng