2017-01-01 71 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