public class Quick{

    private static void swap(Comparable[] a, int i, int j){
        Comparable tmp=a[i];
        a[i]=a[j];
        a[j]=tmp;
    }

    private static int partition(Comparable[] a,int lo,int hi){
        int i=lo;
        int j=hi+1;
        Comparable pivot=a[lo];
        while (true){
            while (a[++i].compareTo(pivot)<0) if (i==hi) break;
            while (a[--j].compareTo(pivot)>0) if (j==lo) break;
            if (i>=j) break;
            swap(a,i,j);
        }
        swap(a,lo,j);
        return j;
    }

    private static void sort(Comparable[] a,int lo,int hi){
        if (hi<=lo) return;
        int j=partition(a,lo,hi);
        sort(a,lo,j-1);
        sort(a,j+1,hi);
    } 

    public static void sort(Comparable[] a){
        // call rcursive version
        StdRandom.shuffle(a);
        sort(a,0,a.length-1);
    }

     public static void main(String[] args){
        Student[] s=new Student[9];
        s[0]=new Student("Bob",20);
        s[1]=new Student("Dan",25);
        s[2]=new Student("First 19",19);
        s[3]=new Student("Tony",18);
        s[4]=new Student("Ron",22);
        s[5]=new Student("Max",23);
        s[6]=new Student("Second 19",19);
        s[7]=new Student("Ann",18);
        s[8]=new Student("Third 19",19);
        Quick.sort(s);
        for (int i=0;i<s.length;i++)
            System.out.println(s[i]);
    }

}