Bubble Sort Program Using C#

I will explain in this article how to write a programme for doing a Bubble Sort using C#. This type of question might be asked by an interviewer in a.Net position related interview.

My intent for this article is to explain how to answer a question that is often asked in an interview, which is: Write a programme that does a Bubble Sort using C#. A candidate new to the interview can become totally confused because the first problem is that the candidate does not know what a bubble sort is.

So, considering the preceding requirement and numerous emails sent to me from beginners, I have decided to write this article with the basics and especially focus on the beginner, student, or whoever might face this type of question in an interview.


So let us start with the basics.

What is a Sorting?


Sorting means arranging the items (numbers, etc.) in a specified manner, in other words, ascending or descending, etc.

Example

10,1,8,2 unordered
1,2,8,10 ordered

I hope you now understand the sorting.

What does Bubble Sort Mean?

To change the position of numbers or other items from right to left, left to right, or any position as you wish. In other words, changing an unordered sequence into an ordered sequence is called a "Bubble Sort."

Example

Suppose I have the given input as:

4,5,3,2,1

Then the given numbers after the Bubble Sort are :

1,2,3,4,5

Now that we completely understand what is meant by "bubble sorting," next I will explain how to do it step-by-step, as in the following:
    1.   Open Visual Studio from Start - All programs - Microsoft Visual Studio.
    2.  Then go to to "File" - "New" - "Project..." then select Visual C# - Windows - Console  application.
    3.    After that specify the name such as Bubble Sort or whatever name you wish and the location of the project and click on the "OK" button. The new project is created.
And use the following code in the program.cs class file: 

using System; 
namespace BubbleSortInCSharp
{
    class bubblesort
    {
        static void Main(string[] args)
        { 
            int[] a = { 3, 2, 5, 4, 1 }; // passing numbers through array 
            int t; 
            for (int p = 0; p <= a.Length - 2; p++)
            {
                for (int i = 0; i <= a.Length - 2; i++)
                {
                    if (a[i] > a[i + 1])
                    {
                        t = a[i + 1];
                        a[i + 1] = a[i];
                        a[i] = t;
                    }
                } 
            }
            Console.WriteLine("This Application Created by vithal wadje for C# corner");
            Console.WriteLine("The Sorted array");
            foreach (int aa in a)                         //writting array
                Console.Write(aa + " "); 
            Console.Read();
        }
    }
}


The output of the above programme will be:


sortedarry.gif


From the above output, it's clear that Bubble Sort means arranging the items in systematic order or as you wish to represent them.

Summary

I hope this article is useful for students and beginners or their requirements. If you have any suggestions, then please contact me. 

Post a Comment

www.CodeNirvana.in

Protected by Copyscape
Copyright © Compilemode