Temolate for sort classes(p245)

//只是排序模板

import edu.princeton.cs.algs4.*;

public class Example
{
    public static void sort(Comparable[] a)
    {
        //See Algorithms 2.1, 2.2, 2.3, 2.4, 2.5, or 2.7
    }
    
    private static boolean less(Comparable v, Comparable w)
    {
        return v.compareTo(w) < 0;
    }
    
    private static void exch(Comparable[] a, int i, int j)
    {
        Comparable t = a[i];
        a[i] = a[j];
        a[j] = t;
    }
    
    private static void show(Comparable[] a)
    {
        for(int i = 0; i < a.length; i++)
            StdOut.print(a[i] + " ");
        StdOut.println();
    }
    
    public static boolean isSorted(Comparable[] a)
    {
        for(int i = 1; i < a.length; i++)
            if(less(a[i], a[i-1])) return false;
        return true;
    }
    
    public static void main(String[] args)
    {
        String[] a = In.readStrings();//CTRL + d
        sort(a);
        assert isSorted(a);
        show(a);
    }
}

猜你喜欢

转载自www.cnblogs.com/w-j-c/p/9101170.html