Java BlueJ Bubblesort

时间:2017-01-31 17:34:22

标签: java bluej

我对我的Java项目有疑问。在School中,我们开始编写一个bubblesort方法,它应该对整数列表进行排序,然后对它们进行图形化排序。但是,我的问题是我们必须在家里完成它,而且我似乎陷入了一件小事。

这是我Bubblesort班的整个代码:

import java.util.Random;
/**
 * Write a description of class Bubblesort here.
 * 
 * @author Aaron Zwickenpflug
 * @version 30.01.2017
 */
public class Bubblesort {
    private Random random;
    private int maxSquare;
    private int maxRandom;
    private int[] x;
    private Square[] square;

    public Bubblesort() {
        this.random = new Random();

        this.maxSquare = 100;
        this.maxRandom = 200;

        this.x = new int[this.maxSquare];
        this.square = new Square[this.maxSquare];

        for (int i = 0; i < this.maxSquare; i++) {
            this.square[i] = new Square(i, x[i]);
            this.x[i] = random.nextInt(maxRandom);
            this.square[i].changeY(x[i]);
            // System.out.println(this.x[i]);
        }
    }

    public void redraw() {
        for (int i = 0; i < this.maxSquare - 1; i++) {
            this.square[i + 1].changeY(this.x[i]);
        }
    }

    public void sort_bubble() {
        int m;
        for (int i = 1; i < 100; i++) {
            for (int y = 0; y < this.maxSquare - i; y++) {
                if (this.x[y] > this.x[y + 1]) {
                    m = this.x[y];
                    this.x[y] = this.x[y + 1]; 
                    this.x[y + 1] = m;
                    this.square[y + 1].changeY(this.x[i + 1]);
                }
            }
        }
    }

}

以下是Square类:

import java.awt.*;

/**
 * A square that can be manipulated and that draws itself on a canvas.
 * 
 * @author  Michael Kölling and David J. Barnes
 * @version 2016.02.29
 */

public class Square {
    private int xSize;
    private int ySize;
    private int xPosition;
    private int yPosition;
    private String color;
    private boolean isVisible;

    /**
     * Create a new square at default position with default color.
     */
    public Square(int xPos, int height) {
        xSize = 9;
        ySize = height;
        xPosition = xPos * 10;
        yPosition = 1;
        color = "green";
        isVisible = false;
    }

    public void changeY(int x) {
        this.ySize = x;
        makeVisible();
    }

    /**
     * Make this square visible. If it was already visible, do nothing.
     */
    public void makeVisible() {
        isVisible = true;
        draw();
    }

    /**
     * Make this square invisible. If it was already invisible, do nothing.
     */
    public void makeInvisible() {
        erase();
        isVisible = false;
    }

    /**
     * Move the square a few pixels to the right.
     */
    public void moveRight() {
        moveHorizontal(20);
    }

    /**
     * Move the square a few pixels to the left.
     */
    public void moveLeft() {
        moveHorizontal(-20);
    }

    /**
     * Move the square a few pixels up.
     */
    public void moveUp() {
        moveVertical(-20);
    }

    /**
     * Move the square a few pixels down.
     */
    public void moveDown() {
        moveVertical(20);
    }

    /**
     * Move the square horizontally by 'distance' pixels.
     */
    public void moveHorizontal(int distance) {
        erase();
        xPosition += distance;
        draw();
    }

    /**
     * Move the square vertically by 'distance' pixels.
     */
    public void moveVertical(int distance) {
        erase();
        yPosition += distance;
        draw();
    }

    /**
     * Slowly move the square horizontally by 'distance' pixels.
     */
    public void slowMoveHorizontal(int distance) {
        int delta;

        if(distance < 0) {
            delta = -1;
            distance = -distance;
        } else {
            delta = 1;
        }

        for(int i = 0; i < distance; i++) {
            xPosition += delta;
            draw();
        }
    }

    /**
     * Slowly move the square vertically by 'distance' pixels.
     */
    public void slowMoveVertical(int distance) {
        int delta;

        if(distance < 0) {
            delta = -1;
            distance = -distance;
        } else {
            delta = 1;
        }

        for(int i = 0; i < distance; i++) {
            yPosition += delta;
            draw();
        }
    }

    /**
     * Change the size to the new size (in pixels). Size must be >= 0.
     */
    public void changeSize(int newSize) {
        erase();
        xSize = newSize;
        ySize = newSize;
        draw();
    }

    /**
     * Change the color. Valid colors are "red", "yellow", "blue", "green",
     * "magenta" and "black".
     */
    public void changeColor(String newColor) {
        color = newColor;
        draw();
    }

    /**
     * Draw the square with current specifications on screen.
     */
    private void draw() {
        if(isVisible) {
            Canvas canvas = Canvas.getCanvas();
            canvas.draw(this, color,
                        new Rectangle(xPosition, yPosition, xSize, ySize));
            canvas.wait(10);
        }
    }

    /**
     * Erase the square on screen.
     */
    private void erase() {
        if(isVisible) {
            Canvas canvas = Canvas.getCanvas();
            canvas.erase(this);
        }
    }
}

现在,问题出在最后一行:

this.square[y + 1].changeY(this.x[i + 1]);

它应该正常(当我运行程序时)更改2个方块之间的y坐标,并“排序”它。虽然它只会弄乱图形,但正方形开始变为一些奇怪的值。我确实检查了必须排序的列表,并且效果非常好。

这是图形的外观: Bubblesort graphics

我希望有人可以帮我解决问题。 提前致谢, 甲

1 个答案:

答案 0 :(得分:0)

我花了一些时间来查看你的代码,我应该早点认识到这个问题,但是你使用的Canvas课程让我失望了。因此,我使用Java Swing组件而不仅仅是AWT组件重写了您的程序,在我更正了一行并在sort_bubble()方法中添加了一行代码后,我得到了以下结果:

Image of a sorted graph.

问题在于sort_bubble()方法中的元素交换,或者更换了基础int数组中的元素,但未正确更新Square数组:

m = this.x[y];
this.x[y] = this.x[y + 1];
this.x[y + 1] = m;
this.square[y + 1].changeY(this.x[i + 1]);

在您之上[{1}}与x[y]进行交换,但出于某种原因,您需要使用x[y + 1]的值更新square[y + 1]。您对x[i + 1]数组的更新应反映基础Square数组上的交换。因此,int中的交换和更新代码应类似于:

sort_bubble()


希望这个答案有所帮助,抱歉需要一段时间才能回复。如果您对我在本答复中提到的内容有任何疑问,请与我们联系。