排序算法类的模板

 1 public class Example {
 2     //数组排序算法
 3     public static void sort(Comparable[] a){
 4 
 5     }
 6 
 7     private static boolean less(Comparable v, Comparable w)
 8     {
 9         return v.compareTo(w) < 0;
10     }
11     //互换位置
12     private static void exch(Comparable[] a, int i, int j)
13     {
14         Comparable t = a[i];
15         a[i] = a[j];
16         a[j] = t;
17     }
18     //展示数组
19     private static void show(Comparable[] a)
20     {
21         for(int i = 0; i < a.length; i++)
22         {
23             System.out.println(a[i] + " ");
24         }
25     }
26     //判断数组元素是否有序
27     public static boolean isSorted(Comparable[] a)
28     {
29         for(int i = 1; i < a.length; i++)
30         {
31             if(less(a[i], a[i-1])){
32                 return false;
33             }
34         }
35         return true;
36     }
37 
38     public static void main(String[] agrs)
39     {
40         String[] a = {"1","2","3","4","5"};
41         sort(a);
42         assert isSorted(a);
43         show(a);
44     }
45 }

 

posted @ 2016-07-28 11:13  道格  阅读(123)  评论(0)    收藏  举报