快速排序的链表方法
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] arr = {5,7,9,2,3,1,5,7,11,23,42,11,22};
Node link = null;
for(int i = 0; i < arr.length; i++) {
Node x = new Node();
x.value = arr[i];
x.next = link;
link = x;
}
String result = m1(link);
System.out.println(result);
}
public static String m1(Node link) {
if(link.next == null) {
return link.value + "";
}else {
int mid = link.value;
link = link.next;
Node link1 = null; //存储比mid小的
Node link2 = null; // 存储大于等于的
while( link != null ) {
if(link.value < mid) {
Node c = new Node();
c.value = link.value;
c.next = link1;
link1 = c;
}else {
Node c = new Node();
c.value = link.value;
c.next = link2;
link2 = c;
}
link = link.next;
}
String re = "";
if(link1 != null) {
re += m1(link1);
}
re += "," + mid;
if(link2 != null ) {
re += "," + m1(link2);
}
return re;
}
}
}
快速排序的算法内容是:
每次取第一个作为基准数,比这个数大的方法放在这个数后面,比这个数小的放在这个数前面,然后将两个数组再次重复操作,多次之后可以得到准确的排列。
链表的快排中对于下一次的循环是这么开始的
递归结束的标志