"Fundamentals of Algorithmic Design and analysis" 14, fast sequencing

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.