快排java
阿新 • • 發佈:2018-11-24
package leetcode; public class quikSort { public static int[] quickSort(int[] list1,int left,int right){ if(left>right){ return null; } int i = left; int j = right; int pivot=list1[left]; while (i<j){ while (pivot<=list1[j] && i<j){ j--; } if (i<j){ list1[i++]=list1[j]; } while (pivot>list1[i] && i<j){ i--; } if (i<j){ list1[j--]=list1[i]; } } list1[i]=pivot; quickSort(list1,left,i-1); quickSort(list1,i+1,right); return list1; } public static void main(String[] args){ int list2[]={10,13,9,5,14,18}; int len = list2.length-1; int [] list3 = quickSort(list2,0,len); for (int i=0;i<len+1;i++){ System.out.print(list2[i]+"-"); } } }
結果:
5-9-10-13-14-18-
Process finished with exit code 0
不理解的可以看看:https://blog.csdn.net/morewindows/article/details/6684558
時間複雜度:nlogn