2013-04-20 41 views
1

對於這個程序,我想實現一個排序和搜索算法的數組。該數組將填充隨機數字。然後我想繪製每個數組元素作爲一個條(使得像條形圖)。我在GUI中有一個步驟並運行按鈕,該步驟應該使用選擇排序。我遇到的問題是:我只知道如何用循環做選擇排序。但是,我不能使用循環,因爲我必須顯示數組正在逐步排序。任何人都可以告訴我如何做選擇排序沒有循環?我將添加目前爲止的所有代碼,因爲這是我第一次發佈任何內容,而且我想確保我具體。沒有循環的選擇排序

ArrayViewer:

import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.JButton; 
import java.util.Scanner; 
import java.awt.BorderLayout; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

public class ArrayViewer 
{ 
    static int[] array; 
    static int size; 

    public static void main(String[] args) 
    { 

     Scanner in=new Scanner(System.in); 
     //ask for the size of the array until the user enters a size in the right range 
     do 
     { 
      System.out.print("Enter the size of the array (should be between 10 and 80): "); 
      size=in.nextInt(); 
     } 
     while (size<10 || size>80); 

     array= ArrayUtil.randomIntArray(size,100);//create a random array of given size and entries ranging from 0 to 100 
     final ArrayComponent arrayComp= new ArrayComponent(array); //construct an arrayComponent with the random array 
     class ButtonListener implements ActionListener 
     { 
      public void actionPerformed(ActionEvent e) 
      { 
       //I want the selection sort algorithm to go in here so I can just assign this to my stepButton. 


      } 
     } 

     final JFrame frame=new JFrame("Sorting"); //create and setup the frame 
     frame.setSize(1200,300); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     JPanel buttonPanel= new JPanel(); // panel to hold the buttons   
     JPanel panel=new JPanel(new BorderLayout()); // panel to hold the button panel and the array component; uses BorderLayout: read about it in the API 

     JButton stepButton=new JButton("Step"); //button to go through the algorithm step by step 
     JButton runButton=new JButton("Run"); //button to run the algorithm 
     ActionListener listener = new ButtonListener(); 
     stepButton.addActionListener(listener); 

     buttonPanel.add(stepButton); 
     buttonPanel.add(runButton); 
     panel.add(buttonPanel,BorderLayout.PAGE_START); //add the buttonPanel at the top of the panel 
     panel.add(arrayComp,BorderLayout.CENTER); //add the arraycoponent object in the center of teh panel 
     frame.add(panel); 
     frame.setVisible(true);   
     //print the entries in the array 
     //System.out.println(arrayComp); 

    } 
} 

ArrayComponent:

import javax.swing.JComponent; 
import java.awt.Rectangle; 
import java.awt.Graphics; 
import java.awt.Graphics2D; 
import java.awt.Dimension; 
import java.awt.Color; 

public class ArrayComponent extends JComponent 
{ 
    private int[] theArray; 
    final int dx=6; //the width of thebars (as well as the with of the spaces between bars) 
    private int space=0; 
    int index1 =0; 
    int index2 =0; 
    public ArrayComponent(int[] a) 
    { 
     theArray=a;  
     space=600-12*theArray.length/2; //space amount on the horizontal axes to center the graph          
     //600 is the frame width in the viewer program 
     //For each bar 12 units on the horixzontal axis is used including the space following it. 
     //something.addActionListener(new ButtonListener()); 
    } 

    public void setIndices(int i, int j) 
    { 
     index1 = i; 
     index2= j; 
    } 

    public void paintComponent(Graphics g) 
    {   
     Graphics2D pen= (Graphics2D) g; 
     for (int k=0;k<theArray.length;k++) 
     { 
      pen.drawRect(space+2*k*dx,5,dx,theArray[k]); 
      //space: initial space 
      //2*k*dx: the (horizontal) distance of te kth bar from the start of the graph 
      //5: bars are located on y=5 
      //dx: the width of the bars 
      //theArray[k]: height of the kth bar 
     } 
     pen.fillRect(space+2*index1*dx,5,dx,theArray[index1]); 
     pen.fillRect(space+2*index2*dx,5,dx,theArray[index2]); 
    } 

    public String toString() 
    { 
     String str=("array=["); 
     int k=0; 
     for (k=0;k<theArray.length-1;k++) 
      str=str+theArray[k]+", ";    
     str=str+theArray[k]+"]"; 
     return str; 
    } 

} 

ArrayUtil(創建隨機陣列):

import java.util.Random; 

/** 
    This class contains utility methods for array manipulation. 
*/ 
public class ArrayUtil 
{ 
    private static Random generator = new Random(); 

    /** 
     Creates an array filled with random values. 
     @param length the length of the array 
     @param n the number of possible random values 
     @return an array filled with length numbers between 
     0 and n - 1 
    */ 
    public static int[] randomIntArray(int length, int n) 
    { 
     int[] a = new int[length];  
     for (int i = 0; i < a.length; i++) 
     a[i] = generator.nextInt(n); 

     return a; 
    } 
} 

很抱歉,如果該信息是冗長。該程序已經繪製了數組,它只是不對它們進行排序。謝謝您的幫助。

+0

你是什麼意思你「必須顯示數組正在逐步排序」? – 2013-04-20 18:06:46

+0

即使使用循環,仍然可以逐步排列數組。事實上,你不能使用循環來做到這一點(分步或逐步顯示)。 – user93353 2013-04-20 18:12:07

+0

所以如果數組是{5,4,3,2,1}並且用戶他是Step按鈕。它會按照它應該切換5和1,但是我想在那裏停下來,並且不再排序,直到用戶再次點擊步驟按鈕。 – Champ 2013-04-20 18:14:53

回答

1

activeIndex引用下一個要排序的數組元素的索引(從值0開始)。

編寫一個方法,比如說stepSelectionSort,它只執行選擇排序和返回的一個步驟。排序從數組[activeIndex]開始。 {0,4,3,2,5} - > activeIndex = 0 - > Step.click - > stepSelectionSort()將數組元素排序爲0 - > {1,4,3,2,5} - >繪製() - > activeIndix = 1 - > Step.click - > stepSelectionSort()排序數組元素1.

+0

這將做到這一點。基本上你有一個循環,但你記得位置(類數據 - 實例成員,而不是本地int),並且每次只執行一次迭代(break和return); – tgkprog 2013-04-20 20:34:33

0
  • 暫時編寫代碼以做一個循環
  • 內部充滿排序採取環路中的所有代碼,並把它在一個方法中
  • 刪除循環&每次用戶點擊「Step」時調用該方法一次GUI按鈕