快速排序基本思想:http://blog.csdn.net/stushan/article/details/51172253
package com.shan.quickSort;
import java.util.Comparator;
public class QuickGe {
public static <E extends Comparable<E>> void quickSort(E[] list) {
quickSort(list, 0, list.length - 1);
}
private static <E extends Comparable<E>> void quickSort(E[] list, int first, int last) {
if(first >= last) return;
int pivotIndex = partition(list, first, last);
quickSort(list, first, pivotIndex - 1);
quickSort(list, pivotIndex + 1, last);
}
/** Partition the array list[first..last] */
private static <E extends Comparable<E>> int partition(E[] list, int first, int last) {
E v = list[first];
int lo = first,hi=last + 1;
while(true){
while(list[++lo].compareTo(v) <= 0)if(lo == last)break;
while(list[--hi].compareTo(v) > 0)if(lo == first)break;
if(lo >= hi)break;
swap(list,lo,hi);
}
swap(list,first,hi);
return hi;
}
private static <E> void swap(E[] list, int lo, int hi) {
E temp = list[lo];
list[lo] = list[hi];
list[hi] = temp;
}
public static <E> void quickSort(E[] list, Comparator<? super E> comparator) {
quickSort(list, 0, list.length - 1, comparator);
}
private static <E> void quickSort(E[] list, int first, int last, Comparator<? super E> comparator) {
if(first >= last)return;
int pivotIndex = partition(list, first, last, comparator);
quickSort(list, first, pivotIndex - 1, comparator);
quickSort(list, pivotIndex + 1, last, comparator);
}
/** Partition the array list[first..last] */
private static <E> int partition(E[] list, int first, int last, Comparator<? super E> comparator) {
E v = list[first];
int lo = first,hi=last + 1;
while(true){
while(comparator.compare(list[++lo], v) <= 0)if(lo == last)break;
while(comparator.compare(list[--hi], v) > 0)if(lo == first)break;
if(lo >= hi)break;
swap(list,lo,hi);
}
swap(list,first,hi);
return hi;
}
public static void main(String[] args) {
Integer[] list = new Integer[15];
for (int i = 0; i < 15; i++) {
list[i] = (int) (Math.random() * 10);
}
quickSort(list);
for (int i = 0; i < list.length; i++) {
System.out.print(list[i] + " ");
}
System.out.println();
Circle[] list1 = { new Circle(2), new Circle(3), new Circle(2), new Circle(5), new Circle(6), new Circle(1),
new Circle(2), new Circle(3), new Circle(14), new Circle(12) };
quickSort(list1, new GeometricObjectComparator());
for (int i = 0; i < list1.length; i++) {
System.out.print(list1[i] + " ");
}
}
}