Package Cn.xf.algorithm.ch04;import Java.util.arraylist;import Java.util.arrays;import java.util.List;import Org.junit.test;public class Quiksort {public int hoarepartition (List data, int left, int. right) {//When the data to be compared to 1 is different, it means that there is only one The data is to be compared if (data = = NULL | | right-left <= 1 | | Left >= right) {return to left;} int sour = (Integer) data.get (left); Start with the first element as the split point int i = left;//left start position int j = right; Right start traversal position do {//As long as I is on the left of J, I keep looping, know I >= jdo {++i;} while ((Integer) data.get (i) < sour);//As long as J is on the right of I loop do {--j;} while ( Integer) Data.get (j) > Sour); this.swap (data, I, j);} while (I < j);//After the Exchange is complete, the split point is then placed to the middle point this.swap (data, I, j); This.swap (data, left, j);//return J;} /** * Exchange data * @param @param index1 * @param index2 */public void Swap (List data, int index1, int index2) {int temp = (Integer) Data.get (index1);d ata.set (index1, Data.get (index2));d ata.set (Index2, temp);} public void Quiks (List data, int left, int. right) {if (left < right) {int mid = This.hoarepartition (data, left, right); Quiks (data, left, mid); Quiks (data, mid + 1, right);}} @Testpublic void Test2 () {List data = arrays.aslist (5,3,1,9,8,2,4,7); quiks (data, 0, data.size ()); for (Object o:data) {Sys Tem.out.print (o + "\ t");}} public static void Test1 (List data) {data = null;} @Testpublic void Test () {List data = arrays.aslist ("1", "2", "3", "4", "5", "6"); Quiksort.test1 (data); for (Object J:data) {System.out.println (j);}}}
Show Results:
"Fundamentals of Algorithmic Design and analysis" 14, fast sequencing