书中关于气泡排序的错误代码

时间:2019-07-07 02:27:17

标签: java intellij-idea applet bubble-sort

我正在从book(Java(第二版)的数据结构和算法-LaFore)中运行示例代码,该代码的运行情况与预期的不同。在第三章中,提供了用于在气泡排序上运行视觉效果的示例代码。但是,适用于该applet的唯一按钮是“ New”和“ Step”。我正在尝试修复IntelliJ中的代码,但断点正在获得取消功能。我不确定这是否意味着代码永远不会运行。我如何显示断点。还要看图片。

BubbleSort.java

import java.applet.Applet;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.EventObject;

public class BubbleSort extends Applet
    implements Runnable, ActionListener
{

    public void init()
    {
        thePersonGroup = new groupBS(groupSize, order);
        setLayout(new FlowLayout(2));
        newButton = new Button("New");
        add(newButton);
        newButton.addActionListener(this);
        sizeButton = new Button("Size");
        add(sizeButton);
        sizeButton.addActionListener(this);
        drawButton = new Button("Draw");
        add(drawButton);
        drawButton.addActionListener(this);
        runButton = new Button("Run");
        add(runButton);
        runButton.addActionListener(this);
        stepButton = new Button("Step");
        add(stepButton);
        stepButton.addActionListener(this);
        aWidth = thePersonGroup.getAppletWidth();
        aHeight = thePersonGroup.getAppletHeight();
        offscreenImage = createImage(aWidth, aHeight);
        offscreenGraphics = offscreenImage.getGraphics();
        thePersonGroup.setDrawMode(2);
        runFlag = false;
    }

    public void paint(Graphics g)
    {
        thePersonGroup.draw(offscreenGraphics);
        g.drawImage(offscreenImage, 0, 0, this);
    }

    public void update(Graphics g)
    {
        paint(g);
    }

    public void actionPerformed(ActionEvent actionevent)
    {
        if(actionevent.getSource() == newButton)
        {
            runFlag = false;
            order = order != 1 ? 1 : 2;
            thePersonGroup = new groupBS(groupSize, order);
        } else
        if(actionevent.getSource() == sizeButton)
        {
            runFlag = false;
            groupSize = groupSize != 10 ? 10 : 100;
            thePersonGroup = new groupBS(groupSize, order);
        } else
        if(actionevent.getSource() == drawButton)
        {
            runFlag = false;
            thePersonGroup.setDrawMode(2);
        } else
        if(actionevent.getSource() == runButton)
            runFlag = true;
        else
        if(actionevent.getSource() == stepButton)
        {
            runFlag = false;
            thePersonGroup.sortStep();
            thePersonGroup.setDrawMode(1);
        }
        repaint();
    }

    public void run()
    {
        for(Thread thread = Thread.currentThread(); runner == thread;)
            if(runFlag && !thePersonGroup.getDone())
            {
                thePersonGroup.sortStep();
                repaint();
                thePersonGroup.setDrawMode(1);
                int i = groupSize != 10 ? 75 : 250;
                try
                {
                    Thread.sleep(i);
                }
                catch(InterruptedException _ex) { }
            }

    }

    public void start()
    {
        if(runner == null)
        {
            runner = new Thread(this);
            runner.start();
        }
    }

    public void stop()
    {
        runner = null;
    }

    public BubbleSort()
    {
        groupSize = 10;
        order = 1;
    }

    private Image offscreenImage;
    private Graphics offscreenGraphics;
    private int aWidth;
    private int aHeight;
    private Thread runner;
    private int groupSize;
    private groupBS thePersonGroup;
    private boolean runFlag;
    private int order;
    private Button newButton;
    private Button sizeButton;
    private Button drawButton;
    private Button runButton;
    private Button stepButton;
}

groupBS.java

import java.awt.Color;
import java.awt.Graphics;

class groupBS
{

    public groupBS(int i, int j)
    {
        aSize = i;
        initOrder = j;
        theArray = new personBS[aSize];
        if(aSize == 100)
        {
            barWidth = 2;
            barSeparation = 1;
        } else
        {
            barWidth = 20;
            barSeparation = 10;
        }
        outer = aSize - 1;
        inner = 0;
        comps = 0;
        swaps = 0;
        swapFlag = false;
        doneFlag = false;
        drawMode = 2;
        if(initOrder == 1)
        {
            for(int k = 0; k < aSize; k++)
            {
                int i1 = (int)(Math.random() * 199D);
                int k1 = (int)(Math.random() * 254D);
                int i2 = (int)(Math.random() * 254D);
                int k2 = (int)(Math.random() * 254D);
                newColor = new Color(k1, i2, k2);
                theArray[k] = new personBS(i1, newColor);
            }

            return;
        }
        for(int l = 0; l < aSize; l++)
        {
            int j1 = 199 - (199 * l) / aSize;
            int l1 = 255 - j1;
            int j2 = 85 * (l % 3);
            int l2 = j1;
            newColor = new Color(l1, j2, l2);
            theArray[l] = new personBS(j1, newColor);
        }

    }

    public void setDrawMode(int i)
    {
        drawMode = i;
    }

    public int getAppletWidth()
    {
        return 370;
    }

    public int getAppletHeight()
    {
        return 320;
    }

    public boolean getDone()
    {
        return doneFlag;
    }

    public void arrowText(Graphics g, Color color, String s, int i, int j, boolean flag, boolean flag1)
    {
        int k = 35 + i * (barWidth + barSeparation);
        int l = 230 + (j + 1) * 13;
        g.setColor(color);
        if(flag1)
            g.drawString(s, k, l);
        if(flag)
        {
            g.drawLine(k + barWidth / 2, 232, k + barWidth / 2, l - 13);
            g.drawLine(k + barWidth / 2, 232, (k + barWidth / 2) - 3, 237);
            g.drawLine(k + barWidth / 2, 232, k + barWidth / 2 + 3, 237);
        }
    }

    public void drawOneBar(Graphics g, int i)
    {
        int j = theArray[i].getHeight();
        int k = 35 + i * (barWidth + barSeparation);
        int l = 230 - j;
        Color color = theArray[i].getColor();
        g.setColor(Color.lightGray);
        g.fillRect(k, 30, barWidth, 200);
        g.setColor(color);
        g.fill3DRect(k, l, barWidth, j, true);
    }

    public void draw(Graphics g)
    {
        if(drawMode != 2)
        {
            if(swapFlag)
            {
                drawOneBar(g, innerOld);
                drawOneBar(g, innerOld + 1);
                swapFlag = false;
            }
        } else
        {
            g.setColor(Color.lightGray);
            g.fillRect(0, 0, 370, 320);
            for(int i = 0; i < aSize; i++)
                drawOneBar(g, i);

        }
        g.setColor(Color.lightGray);
        g.fillRect(0, 0, 150, 32);
        g.setColor(Color.black);
        g.drawString("Comparisons = " + comps, 10, 28);
        g.drawString("Swaps = " + swaps, 10, 15);
        g.setColor(Color.lightGray);
        g.fillRect(0, 230, 370, 65);
        if(aSize == 10)
        {
            arrowText(g, Color.red, "outer", outer, 3, true, true);
            arrowText(g, Color.blue, "inner", inner, 1, true, true);
            arrowText(g, Color.blue, "inner+1", inner + 1, 1, true, true);
            if(doneFlag)
                arrowText(g, Color.black, "Sort is complete", inner, 2, false, true);
            else
            if(theArray[inner].getHeight() > theArray[inner + 1].getHeight())
                arrowText(g, Color.blue, "Will be swapped", inner, 2, false, true);
            else
                arrowText(g, Color.blue, "Will not be swapped", inner, 2, false, true);
        } else
        {
            arrowText(g, Color.red, "xxx", outer, 3, true, false);
            arrowText(g, Color.blue, "xxx", inner, 1, true, false);
            arrowText(g, Color.blue, "xxx", inner + 1, 1, true, false);
        }
        drawMode = 2;
    }

    public void sortStep()
    {
        if(doneFlag)
            return;
        comps++;
        if(theArray[inner].getHeight() > theArray[inner + 1].getHeight())
        {
            swap(inner, inner + 1);
            swapFlag = true;
            swaps++;
        }
        innerOld = inner;
        inner++;
        if(inner > outer - 1)
        {
            inner = 0;
            outer--;
            if(outer == 0)
                doneFlag = true;
        }
    }

    public void swap(int i, int j)
    {
        personBS personbs = theArray[i];
        theArray[i] = theArray[j];
        theArray[j] = personbs;
    }

    private final int appletWidth = 370;
    private final int appletHeight = 320;
    private final int maxHeight = 200;
    private final int topMargin = 30;
    private final int leftMargin = 10;
    private final int barLeftMargin = 35;
    private final int textHeight = 13;
    private int aSize;
    private personBS theArray[];
    private int barWidth;
    private int barSeparation;
    private int outer;
    private int inner;
    private int innerOld;
    private boolean swapFlag;
    private boolean doneFlag;
    private int comps;
    private int swaps;
    private int initOrder;
    Color newColor;
    private int drawMode;
}

personBS.java

import java.awt.Color;

class personBS
{

public Color getColor()
{
    return color;
}

public int getHeight()
{
    return height;
}

public personBS(int i, Color color1)
{
    height = i;
    color = color1;
}

private int height;
private Color color;
}

breakpoint

0 个答案:

没有答案