.NET > Algorithm >> Algorithm in CSharp Views : 25697
Rate This Article :

Bubble Sort

       public void BubbleSort(int[] intArray)

        {

            Console.WriteLine("==========UnSorted Array Input===============");

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

            {

                Console.WriteLine(intArray[i]);

            }

 

            for (int i = intArray.Length - 1; i > 0; i--)

            {

                for (int j = 0; j <= i - 1; j++)

                {

                    if (intArray[j] > intArray[j + 1])

                    {

                        int highValue = intArray[j];

 

                        intArray[j] = intArray[j + 1];

                        intArray[j + 1] = highValue;

                    }

                }

            }

 

            Console.WriteLine("==========Sorted Array Using BubbleSort===============");

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

            {

                Console.WriteLine(intArray[i]);

            }

        }

Bubble Sort Output:

Bubble sort algorithm example.GIF


About Author
Raj Kumar
Total Posts 31
Developer in .Net!
Comment this article
Name*
Email Address* (Will not be shown on this website.)
Comments*
Enter Image Text*
   
View All Comments
Lars
Hi - very good example - but.. would you be able to show me how you would make a bubblesort on this one in C#: "bubblesort cities after size of population? So that it sorts on population? I guess you should use a KeyValuePair with a string and an int? An example on the result should be: Indiana 477 Tennessee 6898 Ohio 15545 Thanks in advance :)
mathavan
i want simple example for Bubble Sort in .net
  Privacy   Terms Of Use   Advertise With Us   Contact Us
© 2016 Developerin.Net. All rights reserved.
Trademarks and Article Images mentioned in this site may belongs to Microsoft and other respective trademark owners.
Articles, Tutorials and all other content offered here is for educational purpose only and its author copyrights.