任意长度快排

快排定义:

             借鉴wiki对之的定义:http://en.wikipedia.org/wiki/Quicksort

 

代码实现:

 1 public class Quicsort {
 2     
 3     
 4     public void _quickSort(Integer[] list, int low, int high) {
 5         if (low < high) {
 6             int middle = getMiddle(list, low, high);  
 7             _quickSort(list, low, middle - 1);        //递归调用
 8             _quickSort(list, middle + 1, high);       
 9         }
10         //System.out.println(low);
11         
12     }
13 
14     public void quick(Integer[] str) {
15         if (str.length > 0) {   
16             _quickSort(str, 0, str.length - 1);
17         }
18     }
19     
20     private int getMiddle(Integer[] src, int start, int end) {
21         int tmp = src[start];
22         while (start < end) {
23             while (start < end && src[end] >= tmp) {
24                 end--;
25             }
26             src[start] = src[end];
27             while (start < end && src[start] <= tmp) {
28                 start++;
29             }
30             src[end] = src[start];
31         }   
32         src[start] = tmp; //一次排序
33         return start;     //返回小于tmp的个数即实现分割
34     }
35 
36 
37 }
import java.util.*; 
public class Test{  
      
   
      public static void main(String[] args) {  
          Scanner br=new Scanner(System.in);
          String f; 
          f=br.nextLine();     //把输入的字符串赋给f
          br.close();
          String  k[];
          k=f.split(" ");      //把f通过空格分割成字符串数组赋给字符串数组k
         
          ArrayList<Integer> y = new ArrayList<Integer>(); 
          for(int i=0;i<k.length;i++){
              String m=k[i];
              y.add(Integer.parseInt(m));
              // System.out.println(y.get(i)); 用于测试是否分割成功
              
          }
          Integer [] list ;
          list=(Integer[])y.toArray(new Integer[1]);  //把ArryList转换成Integer数组
          Quicsort qs=new Quicsort();  
             qs.quick(list);  
              for(int i=0;i<list.length;i++){  
                  System.out.print(list[i]+" ");  
              }  
               System.out.println();  

        }  
  
}  

 

posted @ 2014-10-23 17:18  UDLD  阅读(178)  评论(0编辑  收藏  举报