Bubble Sort Algorithm C#

Bubble Sort is one of the simplest Sort Algorithms to implement, at the same time it’s not the most efficient.

Overview

We try to compare ‘i’ the element and ‘i+1’ the element in the array of numbers to be sorted. If ‘i’ th element is greater than ‘i+1″, we swap the position of these two numbers in the array. We keep on going next in the array and complete the check and swapping unitil ‘n-1’ and ‘n’ the element. This complete round is called a pass. We try do up to n-1 passes (check and swap) until all the numbers are sorted. This is for sorting in ascending order.

Pseudocode

// Outer loop for n-1 passes

static int [] num = {numbers to be sorted}

For (int k=0;k<n-1);k++)

{

for (int i=0;i<n-k-1;i)

{

if (num[i] > num [i+1])

{

Do a swap

}

}

}

n – size of the array (num.length)

Implementation in C#

The complete code is C# is below.

using System;

namespace BubbleSort
{
    class Program
    {
         static int [] num = {7,6,5,4,3,2,1};
         static int swapcount = 0;
        static void Main(string[] args)
        {
           
            // Total number of passes for swap it is 0 to arr.length-1
           for (int k=0;k<num.Length-1;k++)
           {
               int passcount = k+1;
                Console.WriteLine("This is "+"pass number-----> "+passcount);
                PassAndSwap(num,k);
            }
          // PrintSortedArray(num);
            Console.WriteLine("Hello World!");
        }



        public static void PassAndSwap(int [] input, int pass)
        {
            // Total number of swaps = 0 to arr.length-swap_iteration-1
            for (int i=0; i<input.Length-pass-1;i++)
            {
                

                if (input[i] > input[i+1])
                {
                    swapcount = swapcount+1;
                Console.WriteLine("     This is "+ "swap number ---> "+ swapcount);
                    int temp;
                    temp = input[i];
                    input[i]=input[i+1];
                    input[i+1]=temp;
                   
                }
            }
            PrintSortedArray(num);

        }

       
           
        

        public static void PrintSortedArray(int [] Sorted)
        {
            
            for (int j = 0 ;j<Sorted.Length;j++)
            {
                Console.Write(Sorted[j]+",");
               
            }
             Console.WriteLine("\n");

        }
    }
}

The link for Github repo is https://github.com/sundarnarasiman/SortingAlgorithms/tree/main/BubbleSort

Execution and Output

When we execute the above code we’ll get this output below. For sorting 7 (n) numbers, we can clearly see that this is doing 6 (n-1 passes.

This is pass number—–> 1
This is swap number —> 1
This is swap number —> 2
This is swap number —> 3
This is swap number —> 4
This is swap number —> 5
This is swap number —> 6
6,5,4,3,2,1,7,

This is pass number—–> 2
This is swap number —> 7
This is swap number —> 8
This is swap number —> 9
This is swap number —> 10
This is swap number —> 11
5,4,3,2,1,6,7,

This is pass number—–> 3
This is swap number —> 12
This is swap number —> 13
This is swap number —> 14
This is swap number —> 15
4,3,2,1,5,6,7,

This is pass number—–> 4
This is swap number —> 16
This is swap number —> 17
This is swap number —> 18
3,2,1,4,5,6,7,

This is pass number—–> 5
This is swap number —> 19
This is swap number —> 20
2,1,3,4,5,6,7,

This is pass number—–> 6
This is swap number —> 21
1,2,3,4,5,6,7,

The number of swaps depends on how these numbers are arranged in the array.

Time Complexity

When it comes to time complexity, this is not the best performing sorting algorithm. The time complexity in worst case is O(n^2). We’ll see another sorting algorithm in another post.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.