Posted on 2008-10-10 15:39
追风舞者 阅读(2044)
评论(4) 编辑 收藏 所属分类:
数据结构与算法
public class Sort {
public static void quickSort(int[] array) {
quickSort(array, 0, array.length - 1);
}
private static void quickSort(int[] array, int low, int high) {
if (low < high) {
int p = partition(array, low, high);
quickSort(array, low, p - 1);
quickSort(array, p + 1, high);
}
}
private static int partition(int[] array, int low, int high) {
int s = array[high];
int i = low - 1;
for (int j = low; j < high; j++) {
if (array[j] < s) {
i++;
swap(array, i, j);
}
}
swap(array, ++i, high);
return i;
}
private static void swap(int[] array, int i, int j) {
int temp;
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
public static void main(String[] args) {
int [] array = {2,5,3,7,4};
quickSort(array);
for(int i = 0;i<array.length;i++){
System.out.println(array[i]);
}
}
}