RD Design

RD Design

  • NA
  • 25
  • 25.9k

c# bubble sort problem

Aug 26 2011 1:57 AM
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)