0

In this program I'm attempting to visually represent a selection sort using an array of rectangle objects. If the program worked as intended, the rectangles would sort themselves from least to greatest, with two rectangles switching spots and the array being repainted each time through the selection sort loop. As it is now, the program swaps a single rectangle in the array (on the far right) then continues to repaint the entire array over and over.

I've miscalculated something in my selection sort -- I'd appreciate anyone pointing me in the right direction to solve this! Thanks.

Main Method

import javax.swing.JFrame;
import java.awt.event.*;

public class Sorting
{
    public static void main (String[] args)
    {
        JFrame frame = new JFrame ("Sorting!");
        frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

        SortPanel panel = new SortPanel ();

        frame.getContentPane().add(panel);
        frame.pack();
        frame.setVisible(true);
    }
}

Panel

import javax.swing.JPanel;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Random;


public class SortPanel extends JPanel
{
    Random gen = new Random();
    int[] SortArray = new int[51];
    int X = 20;
    int Y = 500;
    Timer SortTimer;
    int small, temp, index;

    public SortPanel()
    {
        SortTimer = new Timer (800, new SortListener());
        SortTimer.start();

        setBackground (Color.white);
        setPreferredSize (new Dimension (1000, 500));


        for(int count = 0; count < SortArray.length; count ++)
        {
        int someNums = gen.nextInt(91)+10;
        SortArray[count] = someNums;
        }

    }


    public void paintComponent (Graphics page)
    {
        super.paintComponent (page);

        for (index=0; index < SortArray.length-1; index++)
        {
            SortArray[small] = SortArray[index];
            page.setColor (RandomColor());
            page.fillRect (X,Y,20,(SortArray[index]*-5));
            X = (index * 20);
        }

    }

private class SortListener implements ActionListener
    {
        public void actionPerformed (ActionEvent event)
        {

            for (int sort = SortArray[index]+1; sort < SortArray.length; sort++)
                if((SortArray[sort]) < (SortArray[small]))
                        SortArray[small]=SortArray[sort];

                int temp = SortArray[small];
                SortArray[small] = SortArray[index];
                SortArray[index] = temp;
                repaint();
        }


    }


    public static Color RandomColor()
        {
            Random gen = new Random();

            int Red = gen.nextInt(256);
            int Green = gen.nextInt(256);
            int Blue = gen.nextInt(256);

            Color MyColor = new Color(Red,Green,Blue);
            return MyColor;
        }


}
2
Contributors
1
Reply
3
Views
4 Years
Discussion Span
Last Post by wen_cai
0

Can you explain your version of a selection sort? From what I can remember is that selection sort goes through the list to find the smallest value and swap the value with the current pointer value.

If you are having the same understanding as me. I think you need is another for loop to find the smallest value to swap.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.