我的排序算法只进行一次

时间:2016-05-04 05:54:54

标签: java arrays sorting

我正在编写一个程序,通过使用两种不同的算法对2个相同值的数组进行排序。每次单击按钮时,它会循环遍历数组。我的问题是它只在你第一次点击按钮时排序。每隔一段时间都没有发生。我把一个计数增量器,看看代码是否正在运行,它是。我很难过。任何帮助将不胜感激。

我修改了选择排序。

public class TwoSortsPanel extends JPanel
{
private int width = 10, gap = 3, x = 200, y = 50;
private int[] ranArr1 = new int[15], ranArr2 = new int[15];
private Color blue = Color.blue, red = Color.red, pink = Color.pink, gray =     Color.gray;
public static int count = 0, indexSel = 0;
{
    for(int i = 0; i < ranArr1.length-1; i++)
    {    
        ranArr1[i] = (int) (Math.random() * 15);
        ranArr2[i] = ranArr1[i];
    }

}   
public TwoSortsPanel()
{
    printArray(ranArr1);
    setBackground(pink);
    setPreferredSize (new Dimension (400,300));
    JButton sort = new JButton ("Sort");
    sort.addActionListener(new ButtonListener());
    add (sort);        
}
public static void printArray(int[] c)
{
    System.out.println(Arrays.toString(c));
}

public void paintComponent (Graphics page)
{
    super.paintComponent(page);
    page.drawString("" + count, 10, 12);

    if (insertion() == false || selection() == false)
    {            
        int yPlus = 50;
        for(int i = 0; i < ranArr1.length; i++)
        {
            page.setColor(blue);

            page.fillRect(x, yPlus, ranArr1[i]*10, width);
            yPlus += width + gap;
        }
        yPlus = 50;
        for(int i = 0; i < ranArr2.length; i++)
        {
            page.setColor(red);

            page.fillRect(x, yPlus, -ranArr2[i]*10, width);
            yPlus += width + gap;
        }            
    }
    else
    {
        int yPlus = 50;
        for(int i = 0; i < ranArr1.length; i++)
        {
            page.setColor(gray);
            page.fillRect(x, yPlus, ranArr1[i]*10, width);
            yPlus += width + gap;
        }
        yPlus = 50;
        for(int i = 0; i < ranArr2.length; i++)
        {
            page.setColor(gray);
            page.fillRect(x, yPlus, -ranArr2[i]*10, width);
            yPlus += width + gap;
        }
    }

}
private class ButtonListener implements ActionListener
{
    public void actionPerformed (ActionEvent e)
    {
        displaySelection(ranArr1);
        displayInsertion(ranArr2);
        count++;
        printArray(ranArr1);
        repaint();
    }
}
    public static void displaySelection(int[] a)
{        
    int temp;
    int min = indexSel;
    for (int scan = indexSel+1; scan < a.length ; scan++)
        if (a[scan] < a[min])
           min = scan;

    temp = a[min];
    a[min] = a[indexSel];
    a[indexSel] = temp;
    count++;
    indexSel++;
}
public static void displayInsertion(int[] b)
{
    int index = 1;
    int key = b[index];
    int position = index;

    while (position > 0 && b[position-1] > key )
    {
       b[position] = b[position-1];
       position--;
    }

    b[position] = key;
}
public boolean selection()
{
    for (int i = 0; i < ranArr1.length-1; i++)
    {
        if(ranArr1[i] > ranArr1[i+1])
        {
            return false;
        }
    }
    return true;
}
public boolean insertion()
{
    for (int i = 0; i < ranArr2.length-1; i++)
    {
        if(ranArr2[i] > ranArr2[i+1])
        {
            return false;
        }
    }
    return true;
}

}

对不起。我不希望帖子看起来太忙,我猜它失去了意义。

0 个答案:

没有答案