2.3.16

question:

Best case. Write a program that produces a best-case array (with no dumplicates) for sort() in ALGORITHM 2.5: an array of N items with distinct keys having the property that every partition will produce subarrays that differ in size by at most 1 (the same subarray sizes that would happen for an array of N equal keys). (For the purposes of this exercise, ignore the initial shuffle.)

answer:

//官网答案

H A C B F E G D L I K J N M O

我的代码

import edu.princeton.cs.algs4.*;

public class Quicksort
{
    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;
    }
    
    public static int partition(Comparable[] a, int lo, int hi)
    {
        Comparable v = a[lo];//应该是a[lo]而不是a[0]
        int i = lo, j = hi + 1;
        while(true)
        {
            while(less(a[++i],v))
            {
                if(i >= hi)
                    break;
            }
            while(less(v,a[--j]))
            {
                if(j <= lo)
                    break;
            }
            if(i >= j)
                break;
            exch(a,i,j);
        }
        exch(a,lo,j);
        if(lo == hi)
            StdOut.printf("%3d     % 3d ", lo, hi);
        else
            StdOut.printf("%3d %3d % 3d ", lo, j, hi);
        for(int t = 0; t < lo; t++)
            StdOut.print("  ");
        show(a,lo,hi);
        return j;
    }
    
    public static void sort(Comparable[] a)
    {
       // StdRandom.shuffle(a);
        sort(a, 0, a.length-1);
    }

    public static void sort(Comparable[] a, int lo, int hi)
    {
        if(lo >= hi)
            return;
        int mid = partition(a,lo,hi);
        sort(a,lo,mid-1);
        sort(a,mid+1,hi);
    }
    
    public static void show(Comparable[] a, int lo, int hi)
    {
        for(int i = lo; i <= hi; i++)
            StdOut.print(a[i] + " ");
        StdOut.println();
    }
    
    public static void main(String[] args)
    {
        //H A C B F E G D L I K J N M O
        String[] a = In.readStrings();
        sort(a);
        StdOut.print("            ");
        show(a, 0, a.length-1);
    }
}

import edu.princeton.cs.algs4.*;

public class Quicksort
{
    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;
    }
    
    public static int partition(Comparable[] a, int lo, int hi)
    {
        Comparable v = a[lo];//应该是a[lo]而不是a[0]
        int i = lo, j = hi + 1;
        while(true)
        {
            while(less(a[++i],v))
            {
                if(i >= hi)
                    break;
            }
            while(less(v,a[--j]))
            {
                if(j <= lo)
                    break;
            }
            if(i >= j)
                break;
            exch(a,i,j);
        }
        exch(a,lo,j);
        if(lo == hi)
            StdOut.printf("%3d     % 3d ", lo, hi);
        else
            StdOut.printf("%3d %3d % 3d ", lo, j, hi);
        for(int t = 0; t < lo; t++)
            StdOut.print("  ");
        show(a,lo,hi);
        return j;
    }
    
    public static void sort(Comparable[] a)
    {
       // StdRandom.shuffle(a);
        sort(a, 0, a.length-1);
    }

    public static void sort(Comparable[] a, int lo, int hi)
    {
        if(lo >= hi)
            return;
        int mid = partition(a,lo,hi);
        sort(a,lo,mid-1);
        sort(a,mid+1,hi);
    }
    
    public static void show(Comparable[] a, int lo, int hi)
    {
        for(int i = lo; i <= hi; i++)
            StdOut.print(a[i] + " ");
        StdOut.println();
    }
    
    public static void main(String[] args)
    {
        //H A C B F E G D L I K J N M O
        String[] a = In.readStrings();
        sort(a);
        StdOut.print("            ");
        show(a, 0, a.length-1);
    }
}

猜你喜欢

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