vendredi 17 juillet 2020

How do I determine whether value in array is in its correct sorted position?

I have a selection-sort algorithm that sorts 47 different vertical red bars into an order, lowest to highest. During the sorting, I want the bars that are placed in the correct position to turn blue. The way I want to do this is by making an if-statement in my drawBar method. I need to determine if a bar is smallest and at index 0, and if it is, then change the color to blue using Expo. Then if the second smallest bar is at index 1, the third smallest at index 2, and so on. I have tried to implement this in many different ways in my drawBar method, but nothing turns blue. All the bars remain red and I have no error messages. How do I fix this if statement to get what I want stated above? Here is my code:

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

public class Lab15st extends Applet
{

    private int numBars;        // number of bars to be sorted
    private int barHeight[];    // array of bar heights
    private int sortDelay;      // delay between comparison iteration

    public void init()
    {
        numBars = 47;
        sortDelay = 50;
        barHeight = new int[numBars];
        getBarValues();
    }
    public void getBarValues()
    {
        Random rand = new Random(3333);
        for (int k = 0; k < numBars; k++)
            barHeight[k] = rand.nextInt(591) + 10;  // range of 10..600
    }
    public void paint(Graphics g)
    {
        showFrame(g);
        displayBars(g);
        sortBars(g);
    }
    public void showFrame(Graphics g)
    {
        Expo.setBackground(g,Expo.black);
        Expo.setColor(g,Expo.white);
        Expo.fillRectangle(g,20,20,980,630);
    }
    public void drawBar(Graphics g, int k)
    {
        for (int p = 0; p < numBars; p++)
        {
        int smallest = findSmallestItemIndex(p);
        int y2 = 630;
        int x1 = 35 + k * 20;
        int y1 = y2 - barHeight[k];
        int x2 = x1 + 10;
        if(barHeight[i] == smallest && barHeight[i] == barHeight[p])
        Expo.setColor(g,Expo.blue);
        else
        Expo.setColor(g,Expo.red);
        Expo.fillRectangle(g,x1,y1,x2,y2);
        }
    }
    public void eraseBar(Graphics g, int k) // not used in 80 point version
    {
        int y2 = 630;
        int x1 = 35 + k * 20;
        int y1 = y2 - barHeight[k];
        int x2 = x1 + 10;
        Expo.setColor(g,Expo.white);
        Expo.fillRectangle(g,x1,y1,x2,y2);
    }

    public void displayBars(Graphics g)
    {  
      Expo.setColor(g,Expo.red);
        for (int k = 0; k < numBars; k++)
            drawBar(g,k);
    }

    public void swap(Graphics g, int m, int n) 
    {                                          
     Expo.delay(sortDelay);
     
     eraseBar(g,m);
     eraseBar(g,n);
     
     int temp = barHeight[m];
     barHeight[m] = barHeight[n];
     barHeight[n] = temp;
     
     drawBar(g,m);
     drawBar(g,n);
   }
    public void sortBars(Graphics g)        
    {                                       
     for (int p = 0; p < numBars; p++)
      {
       int smallest = findSmallestItemIndex(p);
       if (barHeight[p] != barHeight[smallest])
         swap(g,p,smallest);
      }
   }
   public int getItem(int index) { 
     return barHeight[index]; 
    }
   public int findSmallestItemIndex(int start)
   {
     int smallest = start;
     for (int k = start+1; k < numBars; k++)
     if (barHeight[k] < barHeight[smallest])
       smallest = k;
     return smallest;
   }
}

Aucun commentaire:

Enregistrer un commentaire