2017-01-01 89 views
-1

我有一個學校的任務,我有點卡住了這個過程。如何重新排列數組?

這個想法是在C#中創建一個程序,您可以在這裏看到插入排序算法是如何工作的,並且我正在使用帶有隨機生成數字的按鈕數組。

它用於比較的顏色爲綠色,交換爲紅色。

爲什麼按鈕保持着色?

public partial class Form1 : Form 
{ 
    Button[] but; 
    int[] A; 
    int nr_den = 0; 
    int s1 = 0; 
    int s2 = 0; 

    public Form1() 
    { 
     InitializeComponent(); 
    } 

    private void button1_Click(object sender, EventArgs e) 
    { 
     nr_den = Convert.ToInt16(textBox1.Text); 
     s1 = Convert.ToInt16(textBox2.Text); 
     s2= Convert.ToInt16(textBox3.Text); 
     A = new int[nr_den+1]; 
     Random r = new Random(); 

     for (int i = 1; i <= nr_den; i++) 
     { 

      A[i] = r.Next(s1, s2); 
     } 

     but = new Button[nr_den + 1]; 

     for (int i = 1; i <= nr_den; i++) 
     { 
      but[i] = new Button(); 
      but[i].Text = A[i].ToString(); 
      but[i].Width = 40; 
      but[i].Height = 40; 
      flowLayoutPanel1.Controls.Add(but[i]); 
     } 
    } 

    public void exchange(int[] A, int m, int n) 
    { 
     string s; 
     int temp; 
     but[m].BackColor = Color.Red; 
     System.Threading.Thread.Sleep(400); 
     but[n].BackColor = Color.Pink; 
     System.Threading.Thread.Sleep(400); 

     temp = A[m]; 
     s = but[m].Text; 
     A[m] = A[n]; 
     but[m].Text = but[n].Text; 
     A[n] = temp; 
     but[n].Text = s; 

     but[m].Refresh(); 

     but[n].Refresh(); 
    } 

    public void sort(int[] A) 
    { 
     int i, j; 
     int N = A.Length; 

     for (j = 1; j < N; j++) 
     { 
      for (i = j; i > 0 && A[i] < A[i - 1]; i--) 
      { 
       but[i-1].BackColor = Color.Green; 
       System.Threading.Thread.Sleep(400); 
       but[i].BackColor = Color.GreenYellow; 
       System.Threading.Thread.Sleep(400); 
       but[i].Refresh(); 
       but[i - 1].Refresh(); 
       exchange(A, i, i - 1); 
      } 
     } 
    } 

    private void button2_Click(object sender, EventArgs e) 
    { 
     for (int i=1;i<=nr_den;i++) 
     richTextBox1.Text += A[i]+ " "; 

     richTextBox1.Text += " \n"; 

     sort(A); 
    } 

    private void button3_Click(object sender, EventArgs e) 
    { 
     flowLayoutPanel1.Controls.Clear(); 
    } 
} 

form

+0

代替重新排列按鈕本身的,它會更容易重新排列的背襯陣列和每次通過後更新按鈕。 – Abion47

+0

如果你把線程休眠,它不會重新繪製你剛纔做的 – Plutonix

+0

@ Abion47我排序的A數組,但我不知道如何更新按鈕數組。着色是由排序陣列中的按鈕位置完成的 *對不起我的英語我知道我不是很擅長它 – ClaW212

回答

1

恢復色完成後

public void sort(int[] A) 
    { 
     int i, j; 
     int N = A.Length; 

     for (j = 1; j < N; j++) 
     { 
      for (i = j; i > 0 && A[i] < A[i - 1]; i--) 
      { 
       but[i - 1].BackColor = Color.Green; 
       System.Threading.Thread.Sleep(400); 
       but[i].BackColor = Color.GreenYellow; 
       System.Threading.Thread.Sleep(400); 
       but[i].Refresh(); 
       but[i - 1].Refresh(); 
       exchange(A, i, i - 1); 
       but[i-1].BackColor = SystemColors.Control; 
       but[i].BackColor = SystemColors.Control; 
      } 
     } 
    } 

過程中恢復彩色

public void sort(int[] A) 
    { 
     int i, j; 
     int N = A.Length; 

     for (j = 1; j < N; j++) 
     { 
      for (i = j; i > 0 && A[i] < A[i - 1]; i--) 
      { 
       but[i - 1].BackColor = Color.Green; 
       System.Threading.Thread.Sleep(400); 
       but[i].BackColor = Color.GreenYellow; 
       System.Threading.Thread.Sleep(400); 
       but[i].Refresh(); 
       but[i - 1].Refresh(); 
       exchange(A, i, i - 1); 
       but[i-1].BackColor = SystemColors.Control; 
       but[i].BackColor = SystemColors.Control; 
       System.Threading.Thread.Sleep(400); 
       but[i].Refresh(); 
       but[i - 1].Refresh(); 
      } 
     } 
    } 
+0

非常感謝你!我真的讚賞你的幫助! – ClaW212