java实现快速排序:
public class testQuickSort { public static void main(String[] args) { int array[] = {1,5,3,4,2}; sort(array,0,array.length-1); for (int a : array){ System.out.println(a); } } public static void sort(int array[],int start , int end){ if (start < end){ int position = quickSort(array,start,end); sort(array,start,position-1); sort(array,position+1,end); } } public static int quickSort(int array[],int start,int end){ int position = array[start];//将第一位数作为中轴 while(start < end){ while (start<end && position<array[end]) end--; array[start] = array[end];//如果该位置的数小于中轴则交换 while (start<end && position>array[start]) start++; array[end] = array[start];//如果该位置的数大于中轴则交换 } array[start] = position; return start; } }运行结果:
