Introduction to Bubble Sort in C#

Basically Bubble Sort is a way to sort an algorithms in a form of list in certain order, which could be an order from the biggest number to smaller for example or the other way round. So below i’m going to give an example of Bubble Sort.


 int[] number = { 89, 76, 45, 92, 67, 12, 99 };

            bool flag bool= true;

            int temp;

            int numLength = number.Length;

            //sorting an array

            for (int i = 1; (i <= (numLength – 1)) && flag; i++)

            { 

                flag = false; 

                for (int j = 0; j < (numLength – 1); j++)

                {

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

                    {

                        temp = number[j];

                        number[j] = number[j + 1];

                        number[j + 1] = temp;

                        flag = true;

                    }

                }

            }

            //Sorted array 

            foreach (int num in number)

            {

                Console.Write(“\t {0}”,num);

            }

            Console.Read();


The results of the source code after you debug it should look like this.

99        92        89        76        67        45        12

Leave a Reply