2.2.6

question:

Write a program to compute the exact value of the number of array accesses used by top-down mergesort and by bottom-up mergesort. Use your program to plot the values for N form 1 to 512, and to compare the exact values with the upper bound 6NlgN.

answer:

//我不知道访问aux的次数要不要加上去,我算法分析感觉学的不太好

//感觉写的不正确,QAQ

import edu.princeton.cs.algs4.*;

public class Merge
{
    private static Comparable[] aux;
    
    public static int count = 0;
    
    public static void sort(Comparable[] a)
    {
        aux = new Comparable[a.length];
        sortTD(a, 0, a.length-1);
    }
    
    private static boolean less(Comparable v, Comparable w)
    {
        return v.compareTo(w) < 0;
    }
    
    public static void merge(Comparable[] a, int lo, int mid, int hi)
{
    int i = lo, j = mid + 1;
    
    for(int k = lo; k <= hi; k++)//复制
    {
        aux[k] = a[k];
        count++;
    }
    for(int k = lo; k <= hi; k++)//把a[]填满就退出循环
    {
        if(i > mid)//左侧已用完,则只能从右侧拿
            a[k] = aux[j++];
        else if(j > hi)//右侧已用完,则只能从左侧拿
            a[k] = aux[i++];  
        else if(less(aux[i], aux[j]))//当前左侧比右侧小,从右侧拿
            a[k] = aux[i++];
        else//反之当前右侧比左侧小,从左侧拿
            a[k] = aux[j++]; 
        count++;
    }
}
    
    private static void sortTD(Comparable[] a, int lo, int hi)
    {
        if(hi <= lo)
            return;
        int mid = lo + (hi - lo) / 2;//不用(a+b)/2是因为a+b可能超过int的上界,防止溢出
        sortTD(a,lo,mid);//排左侧
        sortTD(a,mid+1,hi);//排右侧
        merge(a,lo,mid,hi);//归并
    }
    
    public static void sortBU(Comparable[] a)
    {
        int N = a.length;
        aux = new Comparable[N];
        for(int sz = 1; sz < N; sz+=sz)//每次merge的大小size,从1开始到小于N的最大size
            for(int lo = 0; lo < N-sz; lo += sz+sz)//左sz和右sz进行归并,所以lo+=2sz
            merge(a, lo, lo+sz-1, Math.min(lo+sz+sz-1, N-1));//取min是怕最后边界的那个lo+sz+sz-1超过数组大小N-1,防止数组越界
    }
    
    public static void main(String[] args)
    {
        int N = 1;
        StdDraw.setXscale(0,512);
        StdDraw.setYscale(0,20000);
        StdDraw.setPenRadius(0.01);
        while(N <= 512)
        {
           Double[] a = new Double[N];
           Double[] b = new Double[N];
           for(int t = 0; t < N; t++)
               a[t] = StdRandom.uniform();
           for(int t = 0; t < N; t++)
               b[t] = a[t];
           
           //top-down 红色
           sort(a);
           StdOut.print(count + " ");
           StdDraw.setPenColor(StdDraw.BOOK_RED);
           StdDraw.point(N,count);
           
           count = 0;
           
           //bottom-up 绿色
           sortBU(b);
           StdOut.print(count + " ");
           StdDraw.setPenColor(StdDraw.GREEN);
           StdDraw.point(N,count);
           
           //上界 黑色
           StdOut.print(6 * N * Math.log(N));   
           StdDraw.setPenColor(StdDraw.BLACK);
           StdDraw.point(N,6 * N * Math.log(N));
           
           count = 0;
           N++;
        }
    }
}

猜你喜欢

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