2017-12-04 4 views
0

一度にすべてのバーをソート全部一度に。バーのデモでは、並べ替え、私はここに受け入れ答えでこのコードを見つけ-Java

import java.awt.Dimension; 
import java.awt.Graphics; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.util.Arrays; 
import java.util.Collections; 

import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.SwingUtilities; 
import javax.swing.Timer; 

public class ShakerSortAnimate extends JPanel { 
private static final int NUM_OF_ITEMS = 20; 
private static final int DIM_W = 400; 
private static final int DIM_H = 400; 
private static final int HORIZON = 350; 
private static final int VERT_INC = 15; 
private static final int HOR_INC = DIM_W/NUM_OF_ITEMS; 

private JButton startButton; 
private Timer timer = null; 
private JButton resetButton; 

Integer[] list; 
int currentIndex = NUM_OF_ITEMS - 1; 

public ShakerSortAnimate() { 
    list = initList(); 

    timer = new Timer(200, new ActionListener() { 
     public void actionPerformed(ActionEvent e) { 
      if (isSortingDone()) { 
       ((Timer) e.getSource()).stop(); 
       startButton.setEnabled(false); 
      } else { 
       sortOnlyOneItem(); 
      } 
      repaint(); 
     } 
    }); 

    //button to run the program 
    startButton = new JButton("Start"); 
    startButton.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent e) { 
      timer.start(); 
     } 
    }); 

    //resets screen 
    resetButton = new JButton("Reset"); 
    resetButton.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent e) { 
      list = initList(); 
      currentIndex = NUM_OF_ITEMS - 1; 
      repaint(); 
      startButton.setEnabled(true); 
     } 
    }); 
    add(startButton); 
    add(resetButton); 
} 

//boolean checks when array is sorted 
public boolean isSortingDone() { 
    return currentIndex == 0; 
} 

//initializes the array 
public Integer[] initList() { 
    Integer[] nums = new Integer[NUM_OF_ITEMS]; 
    for (int i = 1; i <= nums.length; i++) { 
     nums[i - 1] = i; 
    } 
    Collections.shuffle(Arrays.asList(nums)); //shuffles array 
    return nums; 
} 

//draws each bar 
public void drawItem(Graphics g, int item, int index) { 
    int height = item * VERT_INC; 
    int y = HORIZON - height; 
    int x = index * HOR_INC; 
    g.fillRect(x, y, HOR_INC, height); 
} 


//My shaker sort code 
public void sortOnlyOneItem() 
{ 
    boolean swapped = true; 
    int start = 0; 
    int end = currentIndex; 

    while (swapped==true) 
    { 
     swapped = false; 

     for (int i = start; i < end; ++i) 
     { 
      if (list[i] > list[i + 1]) 
      { 
       int temp = list[i]; 
       list[i] = list[i+1]; 
       list[i+1] = temp; 
       swapped = true; 
      } 
     } 

     if (swapped==false) 
      break; 

     swapped = false; 

     end = end-1; 

     for (int i = end; i >=start; i--) 
     { 
      if (list[i] > list[i+1]) 
      { 
       int temp = list[i]; 
       list[i] = list[i+1]; 
       list[i+1] = temp; 
       swapped = true; 
      } 
     } 

     start = start + 1; 
    } 

    currentIndex--; //currentIndex is updated each time shaker sort runs 
} 






//draws all bars 
@Override 
protected void paintComponent(Graphics g) { 
    super.paintComponent(g); 
    for (int i = 0; i < list.length; i++) { 
     drawItem(g, list[i], i); 
    } 
} 

@Override 
public Dimension getPreferredSize() { 
    return new Dimension(DIM_W, DIM_H); 
} 

public static void main(String[] args) { 
    SwingUtilities.invokeLater(new Runnable() { 
     public void run() { 
      JFrame frame = new JFrame("Sort"); 
      frame.add(new ShakerSortAnimate()); 
      frame.pack(); 
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
      frame.setLocationRelativeTo(null); 
      frame.setVisible(true); 
     } 
    }); 
} 
    } 

は、私は私のシェーカーソートコードはそれぞれの比較ではなく、全体の事のためにそれを行うには持っていることを認識んが、正直なところ、私もそれをコーディングを開始する方法がわかりません。ここであなたの誰かがシェイカーソートで各比較をコーディングする方法を知っている場合は、私を助けることができますか?

Btw、私はあなたもこれを実行しようとすることができますので、全体を投稿しました。

ありがとうございます!

+1

を供給するアルゴリズムを模倣する最善の努力をするだけで、オーバー一切の請求をしません擬似ループである 'Timer'で世話をしてください。別の反復が必要かどうかを判断する必要があります。もしそうなら、 'sortOnlyOneItem'メソッドを呼び出します。これはまた、 'swapped'、' start'と 'end'がインスタンスフィールドである必要があることを意味します。 – MadProgrammer

答えて

1

だから、あなたはこれを必要とする...のみコールごとに一度実行するには

public void sortOnlyOneItem() 
{ 
    boolean swapped = true; 
    int start = 0; 
    int end = currentIndex; 

    while (swapped==true) 
    { 
     swapped = false; 

     for (int i = start; i < end; ++i) 
     { 
      if (list[i] > list[i + 1]) 
      { 
       int temp = list[i]; 
       list[i] = list[i+1]; 
       list[i+1] = temp; 
       swapped = true; 
      } 
     } 

     if (swapped==false) 
      break; 

     swapped = false; 

     end = end-1; 

     for (int i = end; i >=start; i--) 
     { 
      if (list[i] > list[i+1]) 
      { 
       int temp = list[i]; 
       list[i] = list[i+1]; 
       list[i+1] = temp; 
       swapped = true; 
      } 
     } 

     start = start + 1; 
    } 

    currentIndex--; //currentIndex is updated each time shaker sort runs 
} 

。今のところそれをやっていないのは、while-loopなら、それを取り除く必要があるからです。そうすることで

swappedtrueあるとswappedはまだ何かのように私たちをもたらしますtrue

ある場合currentIndexだけ減算する必要がある場合には、第二のループにのみ実行する必要があります...

public void sortOnlyOneItem() { 
    boolean swapped = true; 
    int start = 0; 
    int end = currentIndex; 

    for (int i = start; i < end; ++i) { 
     if (list[i] > list[i + 1]) { 
      int temp = list[i]; 
      list[i] = list[i + 1]; 
      list[i + 1] = temp; 
      swapped = true; 
     } 
    } 

    if (swapped) { 
     swapped = false; 

     end = end - 1; 

     for (int i = end; i >= start; i--) { 
      if (list[i] > list[i + 1]) { 
       int temp = list[i]; 
       list[i] = list[i + 1]; 
       list[i + 1] = temp; 
       swapped = true; 
      } 
     } 
    } 

    if (swapped) { 
     currentIndex--; //currentIndex is updated each time shaker sort runs 
    } 
} 

2つのfor-loopが複数の変更を加えることができるので、まだ正しくはありません。

代わりに、我々は唯一の最も、二つの変更、開始時に1と終了、何かのように見えるかもしれませんが、

1つに、作る反復を必要とする...

protected void swap(int a, int b) { 
    int tmp = list[a]; 
    list[a] = list[b]; 
    list[b] = tmp; 
} 

int endIndex = NUM_OF_ITEMS - 1; 

//My shaker sort code 
public void sortOnlyOneItem() { 

    int startIndex = 0; 
    while (startIndex < NUM_OF_ITEMS - 1 && list[startIndex] < list[startIndex + 1]) { 
     startIndex++; 
    } 

    if (startIndex < NUM_OF_ITEMS - 1 && list[startIndex] > list[startIndex + 1]) { 
     swap(startIndex, startIndex + 1); 
     int end = endIndex; 
     while (end > 0 && list[end - 1] < list[end]) { 
      end--; 
     } 
     if (end > 0 && list[end - 1] > list[end]) { 
      swap(end - 1, end); 
     } else { 
      endIndex--; 
     } 
    } else { 
     endIndex = 0; 
    } 
} 

これは基本的に、可能であれば(startendで)変更可能な2つのインデックスと、swapを探します。変更を行わずにstartから最後まで反復処理ができると、並べ替えが完了します。

これが正確であるかどうさて、私はそれはあなたが `ステートメントを(スワップ)しながら、`、これはする必要がループする必要がありますが

+0

ありがとうございました!しかし、このコードはShakerのソートの仕方にはうまくいかず、理由はわかりません。 – TheDeadline

+1

これは私が期待していたものの一部ですが、アルゴリズムを単一のステップに私は前に "シェーカー"のソートを実装したことはありませんでした。何が提示されているのか、デバイスがあなたのニーズを満たすためのアルゴリズムの基本的なアイデアを取る必要があります。 – MadProgrammer

+0

助けてくれてありがとう:DD – TheDeadline

関連する問題