1
Answer

c# bubble sort problem

RD Design

RD Design

13y
1.4k
1
Hi,

I am developing a c# application to show how bubble sort is working.this is like a demonstration.Anyway my program is working
fine and its sorting the things well.But I have a problem Threading is not working fine.Here is the code

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace Algorithm_Simulator
{
    public partial class Form1 : Form
    {
        Random random = new Random();
        int[] array = new int[6];
        int Size = 6;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
           
                RandomNumber(0, 100);
            
        }

        private void RandomNumber(int min, int max)
        {
            int no;
            

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

                if (i == 1)
                {
                   
                    no = random.Next(min, max);
                    btn1.Text = no.ToString();
                    array[0] = no;
                }
                if (i == 2)
                {
                 
                    no = random.Next(min, max);
                    btn2.Text = no.ToString();
                    array[1] = no;
                }
                if (i == 3){
                 
                    no = random.Next(min, max);
                    btn3.Text = no.ToString();
                    array[2] = no;
                    }
                if (i == 4)
                {
                    no = random.Next(min, max);
                    btn4.Text = no.ToString();
                    array[3] = no;
                }
                if (i == 5)
                {
                    no = random.Next(min, max);
                    btn5.Text = no.ToString();
                    array[4] = no;
                }
                if (i == 6)
                {
                    no = random.Next(min, max);
                    btn6.Text = no.ToString();
                    array[5] = no;
                }
                
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            BubbleSort(array,Size);
        }
        public void BubbleSort(int[] array, int Size)
        {
          int temp ;
           for (int pass = 1; pass < Size; pass++)
           {
               
        for (int i = 0; i < Size-pass; i++)
               {
            if (array [i] > array [i+1])
            {
                if (i == 0)
                {
                    btn1.BackColor = Color.Yellow;
                    Thread.Sleep(2000);
                    btn2.BackColor = Color.Yellow;
                    
                }
                if (i == 1)
                {

                    btn2.BackColor = Color.Yellow;
                    btn3.BackColor = Color.Yellow;
                }
                if (i == 2)
                {

                    btn3.BackColor = Color.Yellow;
                    btn4.BackColor = Color.Yellow;
                }
                if (i == 3)
                {
                    btn4.BackColor = Color.Yellow;
                    btn5.BackColor = Color.Yellow;
                }
                if (i == 4)
                {
                    btn5.BackColor = Color.Yellow;
                    btn6.BackColor = Color.Yellow;
                }
                if (i == 5)
                {
                    btn6.BackColor = Color.Yellow;
                }

                Thread.Sleep(2000); // this Thread not working
                temp = array[i] ;
                

                array[i] = array [i+1] ;
                Thread.Sleep(2000); // this Thread not working

                array [i+1] = temp ;
                Thread.Sleep(2000); // this Thread not working
                  }
               }
           }
                a();
               
        }
        public void a()
        {
            btn1.Text = array[0].ToString();
            
            btn2.Text = array[1].ToString();
            btn3.Text = array[2].ToString();
            btn4.Text = array[3].ToString();
           
            b();
           
        }
        public void b()
        {
            btn5.Text = array[4].ToString();
            btn6.Text = array[5].ToString();
        }
    }
}


please tell me how do I solve this? where I should put the thread
Thanks
Answers (1)
0
Vulpes
NA 98.3k 1.5m 13y
You'll need to call Application.DoEvents() before Thread.Sleep(), otherwise the color changes will be cached by GDI+ and won't take effect until the method ends.

Here's how I'd do it. I'm assuming that the buttons all have different colors to start with and you want them to retain their colors when they are in sorted order. You don't need to deal with the case i == 5 as it will never be called:

        public void SwapColor(Button b1, Button b2)
        {
            Color tempColor = b1.BackColor;
            b1.BackColor = b2.BackColor;
            b2.BackColor = tempColor;
        }

        public void BubbleSort(int[] array, int Size)
        {
            int temp;

            for (int pass = 1; pass < Size; pass++)
            {
                for (int i = 0; i < Size - pass; i++)
                {
                    if (array[i] > array[i + 1])
                    {
                        if (i == 0)
                        {
                            SwapColor(btn1, btn2);
                        }
                        if (i == 1)
                        {
                            SwapColor(btn2, btn3);                       
                        }
                        if (i == 2)
                        {
                            SwapColor(btn3, btn4);
                        }
                        if (i == 3)
                        {
                            SwapColor(btn4, btn5);
                        }
                        if (i == 4)
                        {
                            SwapColor(btn5, btn6);
                        }
                       
                        Application.DoEvents();
                        Thread.Sleep(1000); // 2000 is a bit long 
                        temp = array[i];
                        array[i] = array[i + 1];
                        array[i + 1] = temp;
                    }
                }
            }
            a();
        }