Software & Finance





C# - Sorting Algorithm - Bubble Sort





We often using sorting algorithm to sort numbers and strings. Also we have many sorting algorithms. I have explained here on how bubble sort algorithm works. Look at the yellow color high lighting section on output to understand how algorithm works after each iteration.

Click here for C# BubbleSort Algorithm

Click here for C# InsertionSort Algorithm

Click here for C# MergeSort Recursive Algorithm

Click here for C# MergeSort Iterative Algorithm

Click here for C# QuickSort Recursive Algorithm

Click here for C# QuickSort Iterative Algorithm


The complete program and test run output are given below:

 

Source Code


using System;

using System.Collections.Generic;

using System.Text;

 

namespace ConsoleApplication1

{

    class Program

    {

 

      static int BubbleSort()

        {

            Console.Write("\nProgram for Ascending order of Numeric Values using BUBBLE SORT");

            Console.Write("\n\nEnter the total number of elements: ");

            int max = Convert.ToInt32(Console.ReadLine());

 

            int [] numarray = new int[max];

 

            for(int i = 0; i < max; i++)

            {

                Console.Write("\nEnter [" + (i + 1).ToString() + "] element: ");

                numarray[i] = Convert.ToInt32(Console.ReadLine());

            }

 

            Console.Write("Before Sorting   : ");

            for(int k = 0; k < max; k++)

                Console.Write(numarray[k] + " ");

            Console.Write("\n");

 

            for(int i = 1; i < max; i++)

            {

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

                {

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

                    {

                        int temp = numarray[j];

                        numarray[j] = numarray[j + 1];

                        numarray[j + 1] = temp;

                    }

                }

                Console.Write("After iteration " + i.ToString() + ": ");

                for(int k = 0; k < max; k++)

                    Console.Write(numarray[k] +  " ");

                Console.Write("/*** " + (i + 1).ToString() + " biggest number(s) is(are) pushed to the end of the array ***/\n");

            }

 

            Console.Write("\n\nThe numbers in ascending orders are given below:\n\n");

            for(int i = 0; i < max; i++)

            {

                Console.Write("Sorted [" + (i + 1).ToString() + "] element: ");

                Console.Write(numarray[i]);

                Console.Write("\n");

            }

            return 0;

        }  

 

      static void Main(string[] args)

      {

            BubbleSort();

           

      }

    }

}

Output


 

Program for Ascending order of Numeric Values using BUBBLE SORT

 

Enter the total number of elements: 8

 

Enter [1] element: 80

Enter [2] element: 60

Enter [3] element: 40

Enter [4] element: 20

Enter [5] element: 10

Enter [6] element: 30

Enter [7] element: 50

Enter [8] element: 70

 

Before Sorting   : 80 60 40 20 10 30 50 70

After iteration 1: 60 40 20 10 30 50 70 80

After iteration 2: 40 20 10 30 50 60 70 80

After iteration 3: 20 10 30 40 50 60 70 80

After iteration 4: 10 20 30 40 50 60 70 80

After iteration 5: 10 20 30 40 50 60 70 80

After iteration 6: 10 20 30 40 50 60 70 80

After iteration 7: 10 20 30 40 50 60 70 80

 

The numbers in ascending orders are given below:

 

Sorted [1] element: 10

Sorted [2] element: 20

Sorted [3] element: 30

Sorted [4] element: 40

Sorted [5] element: 50

Sorted [6] element: 60

Sorted [7] element: 70

Sorted [8] element: 80