Search code examples
javasortinginsertion-sortselection-sort

twosorts is only giving me an issue


it keeps making the selection sort off by at least 1, it sorts the insertion sort fine, but it seems like the selection sort needs more time, i cant figure it out... its not doing a proper sort of the selection sort method.i think that my error is either in the reorderselection method.

import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import java.util.Random;

public class TwoSorts extends Applet {

private final int APPLET_WIDTH = 800, APPLET_HEIGHT = 600; // to make the applet size
private final int colNum = 15; // how many columns
int[] insertion = new int[colNum]; //insertion array for column heights
int[] selection = new int[colNum]; // selection array for column heights
public Random generator; // random numbers class
public int randomNum; // random number
public Button butn1 = new Button("Sort Arrays"); // buttons
public int selectionCount = 0; // how many times press the sort button
boolean selectionFlag, insertionFlag; // to stop looping when done

//**********************************************************************
//* initiates everything
//**********************************************************************
public void init()
{

  setBackground (Color.black);
  setSize(APPLET_WIDTH, APPLET_HEIGHT);
  generator = new Random();

  for (int column = 0; column < colNum; column++) // Creates the two arrays
  {
    randomNum = generator.nextInt(100) + 15;
    insertion[column] = randomNum;
    selection[column] = randomNum;
  }

  butn1.addActionListener(new Butn1Handler());
  butn1.setBackground(Color.blue);
  add(butn1);
}

//*************************************************************************
// Draws the columns
//************************************************************************
public void paint(Graphics g)
{
  g.setColor(Color.white); // debugging code
  g.drawString ("Count: " + selectionCount, 100, 495);

  g.drawString("Selection Sort", 25, 220);
  g.drawString("Insertion Sort", 25, 420);

  int xs = 50, ys = 100, width = 40, heights = 0; // for the loops
  int xi = 50, yi = 300, heighti = 0;

  if ( insertionFlag == false && selectionFlag == false)
  {
    for (int count = 0; count < colNum + 1; count++ )
    {
      g.setColor(Color.green);   
      heights = selection[count];
      heighti = insertion[count];
      g.fillRect(xs, ys, width, heights);
      g.fillRect(xi, yi, width, heighti);
      xs = xs + width + 2;
      xi = xi + width + 2;
    }
  }
  else
  {
    g.setColor(Color.white);
    g.drawString ("Sort is Done!", 5, 495);
    for (int count = 0; count < colNum + 1; count++ )
    {
      g.setColor(Color.gray);
      heights = selection[count];
      heighti = insertion[count];
      g.fillRect(xs, ys, width, heights);
      g.fillRect(xi, yi, width, heighti);
      xs = xs + width + 2;
      xi = xi + width + 2;
    }  
  }
}
//*****************************************************************************
//* Method to sort the array by Selection method
//******************************************************************************
public void reorderSelection()
{
  int min = selectionCount;
  int temp = 0;

   for (int scan = (selectionCount); scan < selection.length; scan++)
   { 
     if (selection[scan] < selection[min])
     min = scan;

       temp = selection[min];
       selection[min] = selection[selectionCount];
       selection[selectionCount] = temp;
   }
}
//*****************************************************************************
//* Method to sort the arrary by Insertion method
//******************************************************************************
public void reorderInsertion()
{
  int key = insertion[selectionCount];
  int position = selectionCount;
  while (position > 0 && key < (insertion[position-1]))
  {
    insertion[position] = insertion[position-1];
    position--;
  }
  insertion[position] = key;
}

//-----------------------------------------------------
// Button 1 Listener and instructions
//-----------------------------------------------------
public class Butn1Handler implements ActionListener
{
  public void actionPerformed(ActionEvent e)
  {
   reorderSelection();
   reorderInsertion();
   repaint();
   selectionCount++;
     if (selectionCount > 14)
       selectionFlag = true;
         if (selectionCount > 15)
           insertionFlag = true;

   }
 }
}

Solution

  • Your selection sort method is incorrect. You need an inner loop.

    public void reorderSelection()
    {
        int min = selectionCount;
        int temp = 0;
    
        for( int scan = selectionCount ; scan < selection.length - 1 ; scan++ )
        {
            min = scan;
    
            for(int i = scan + 1; i < selection.length ; i++)
            {
                if(selection[min] > selection[i])  min = i;
            }
    
            if(min != scan)
            {
                temp = selection[scan];
                selection[scan] = selection[min];
                selection[min] = temp;
            }
        }
    }