package com.charles.algorithm;
public class QuickSort {
public static void main(String[] args) {
int[] data = new int[] { 1, 4, 7, 9, 0 };
quickSort(data, data.length - 1, 0);
for (int index : data) {
System.out.print(index + " ");
}
}
//make the first item to be index
public static void quickSort(int[] data, int high, int low) {
if (low >= high) {
return;
}
int h = high, l = low;
int index = data[low];
while (low < high) {
while (low < high && data[high] >= index) {
high--;
}
data[low] = data[high];
while (low < high && data[low] <= index) {
low++;
}
data[high] = data[low];
}
data[low] = index;
quickSort(data, h, low + 1);
quickSort(data, low - 1, l);
}
//make the middle item to be index
public static void quickSort2(int[] data, int high, int low) {
if (low >= high) {
return;
}
int h = high, l = low;
int middle = (high + low) / 2;
int index = data[middle];
while (low < high) {
while (low < high && data[low] <= index) {
low++;
}
data[middle] = data[high];
data[high] = data[low];
while (high > low && data[high] >= index) {
high--;
}
data[low] = data[high];
}
data[low] = index;
quickSort2(data, h, low + 1);
quickSort2(data, low - 1, l);
}
}
原文:http://www.cnblogs.com/julygift/p/7196963.html