标签:
package sort; public class QuickSort { public static void quickSort(int[] list){ quickSort(list,0,list.length-1); } //注意这里是private private static void quickSort(int[] list,int first ,int last){ //这里有一个if条件 别忘了 if(last>first){ int pivotIndex = partition(list, first, last); quickSort(list,first,pivotIndex-1);//这里是first和pivotIndex-1 quickSort(list,pivotIndex+1,last);//这里是pivoeIndex+1 } } //注意这里是的返回值是int private static int partition(int[] list,int first,int last){ int pivot = list[first]; int low = first+1; int high = last; while(low<high){ while(low<= high&& list[low]<=pivot) low++; while(low<=high&&list[high]>pivot) high--; if(high>low){ //这里面要注意的是比较的是下标而不是数组值 ,因为此时数组值就是hign的比low的小了 int temp= list[high]; //自己连交换条件都写错了 list[high] = list[low]; list[low]=temp; } } while(high>first&&list[high]>=pivot) //注意这里面是大于等于pivot high--; if(pivot>list[high]) { list[first]=list[high]; list[high]=pivot; return high; } else return first; } public static void main(String[] args){ int[] list = {2,3,2,5,6,1,3,14,12}; quickSort(list); for(int i=0;i<list.length;i++) System.out.print(list[i]+" "); } }
标签:
原文地址:http://www.cnblogs.com/kisty/p/5851248.html