Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

How to do Sorting using Bubble Sort algorithm in C# (.net 2.0)

Author: Zunnair
Download Source Code : 850_bubblesort.zip

In this simple article you will learn how to do sorting using bubble sort in VB and C# .net 2.0.

Bubble sort is a one of the simplest sorting algorithms. It belongs to exchanging element category of sorting algorithms. By using this algorithm you can sort your array or collection according to a specified key.

How bubble sort works:

As you know that bubbles are light weight and fly upwards in the air so bubble sort works according to this technique. It starts from the bottom and compares the elements, it continues to compare till the smallest or lightest element does not come on the top. But in next iteration it does not compare those elements which already have been shifted in previous iterations. In all iterations it compares from the bottom and shifts the lightest element on the top.

Complexity of Bubble Sort Algorithm:

This algorithm takes N square time in its worst and average case. So we can say that its complexity is BIG O of n square.

To demonstrate this make a window form based application. Drag one button and one list box on the form. Load an integer array in the list box and than press button to sort.

Now write the following code on Button Click event:

C#

private void btn_Sort_Click(object sender, EventArgs e)

        {

            BubbleSort();

            listBox_elements.Items.Clear();

            for (int x = 0; x < arr.Length; x++)

                listBox_elements.Items.Insert(x, arr.GetValue(x));

        }

VB

Private Sub btn_Sort_Click(ByVal sender As Object, ByVal e As EventArgs)

        BubbleSort()

        listBox_elements.Items.Clear()

        For x As Integer = 0 To arr.Length - 1

            listBox_elements.Items.Insert(x, arr.GetValue(x))

        Next

    End Sub

This is a simple code to call Bubble Sort () Function. After sorting, this code will load the sorted array in list box.

Now write the following code in Bubble Sort () Function:

C#

private void BubbleSort()

        {   int temp=0;

            for (int j = 0; j < arr.Length; j++)

            {

                for (int k = (arr.Length) - 1; k > j; k--)

                {

                    if ((int)(arr.GetValue(k)) < (int)(arr.GetValue(k - 1)))

                    {

                        temp =(int) arr.GetValue(k);

                        arr.SetValue(arr.GetValue(k - 1), k);

                        arr.SetValue(temp, k - 1);

                   

                    }               

                }

            }

        }

VB

Private Sub BubbleSort()

        Dim temp As Integer = 0

        For j As Integer = 0 To arr.Length - 1

            For k As Integer = (arr.Length) - 1 To j + 1 Step -1

                If CInt((arr.GetValue(k))) < CInt((arr.GetValue(k - 1))) Then

                    temp = CInt(arr.GetValue(k))

                    arr.SetValue(arr.GetValue(k - 1), k)

 

                    arr.SetValue(temp, k - 1)

                End If

            Next

        Next

    End Sub

 

.

Now write the following code on FORM LOAD event:

C#

  private void Form1_Load(object sender, EventArgs e)

        {

            this.Text = "DEVASP BUBBLE SORT APPLICATION";

            arr.SetValue(8, 0);

            arr.SetValue(9,1);

            arr.SetValue(2, 2);

            arr.SetValue(4, 3);

            arr.SetValue(7, 4);

            for (int i = 0; i < arr.Length; i++)

                listBox_elements.Items.Insert(i, arr.GetValue(i));          

        }

VB

Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)

        Me.Text = "DEVASP BUBBLE SORT APPLICATION"

        arr.SetValue(8, 0)

        arr.SetValue(9, 1)

        arr.SetValue(2, 2)

        arr.SetValue(4, 3)

        arr.SetValue(7, 4)

        For i As Integer = 0 To arr.Length - 1

            listBox_elements.Items.Insert(i, arr.GetValue(i))

        Next

    End Sub

This simple article tells some how to do sorting using bubble sort in VB and C# .net 2.0.

Article Comments
I have tried this code, and its giving me load errors with the arr? Im not sure why, it is saying the name 'arr' does not exist in the current context. I am new to programming and this is part of a project, so it would be nice to get some help.

Thank you, Ed

Posted on 6/4/2010 6:26:07 PM by Ed Masten

Ok, sorry to bother you again, I figured out my problem with the error message. But, now when i run the program it doesnt allow me to enter numbers to be sorted. It doesnt allow me to do anything, except if I press Sort it shows me x amount of 0s? So I guess my question is how can I allow a user to enter a random amount of numbers and then sort them?

Posted on 6/4/2010 7:08:17 PM by Ed Masten

Sir, I have a simple quation to you, that is ..(Accualy it is not related to this code)..How an array is displayed? Plz tell me the method.....

Posted on 7/31/2010 5:02:27 AM by Bhargab Saha

Ed:

You must Declare arr as an integer if you are only inputting numbers and make it a float if it is to be numbers and letters. If you have a Visual Basic program, then look around in the material and you will find the answers that you seek.

Posted on 8/24/2010 8:25:23 PM by Chris Chartier

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< Array operations (copy, length, read only, fixed size, convert to string) using “ARRAY CLASS” (Non Static functions) in C# (.net 2.0) (Part 2)

Disclaimer - Privacy
© 2002-2017 DevASP.net