Learning Links: Sitting on the toilet see algorithm: Quick Sort
Java code:
1 classquicksort{2 int[] Sortans;3 4 QuickSort () {}5 6QuickSort (int[] nums) {7QSort (nums,0,nums.length-1);8sortans=nums;9 }Ten One protected voidQSort (int[] Nums,intAintb) { A if(a<C) { - intpos=patition (nums,a,b); -QSort (nums,a,pos-1); theQSort (nums,pos+1, b); - } - } - intPatition (int[] Nums,intAintb) { + intinit=A; - while(b>a) { + while(Nums[b]>nums[init] && b>a) { Ab--;//Right Sentry left shift at } - while(Nums[init]>=nums[a] && b>a) {//Nums[init]>=nums[a] -a++;//left Sentry Right move through "=, in the first cycle, the Sentinel leaves the base number - } - if(a<b) Swap (NUMS,A,B); - } inSwap (NUMS,INIT,A);//Base Number Homing - returnA; to } + - voidSwapint[] Nums,intAintb) { the inttmp=Nums[a]; *nums[a]=Nums[b]; $nums[b]=tmp;Panax Notoginseng } - PublicString toString () { the inti; +String str=NewString (""); A for(i=0;i<sortans.length;i++) str+=string.valueof (Sortans[i]) + ""; thestr+= "\ n"; + returnstr; - } $ $}
Quick Sort QuickSort