C# - Bubble Sort Algorithm

Introduction

In this article, we are going to discuss the bubble sort algorithm using C#. This is the first article of the C# - Sorting. 

Many Multinational companies were asking for such algorithms in technical interviews for beginners, intermediate, and experienced hence I thought it will help us to prepare too.

We will cover,

  1. What is Sorting?
  2. Type of Sorting Algorithm.
  3. Bubble Sort Algorithm.

Prerequisites 

  1. Basic knowledge of C# and sorting is required.
  2. Visual Studio or VS code

What is sorting?

The first question that comes to my mind is, what is Sorting?

We can say, sorting is the process to arrange elements in ascending or descending order.

Why do we want to arrange it?

Sorting helps us to search records quickly. e.g., suppose you have millions of unorder and unsorted records in the database and you want to search for a record from it .That is difficult and impacts the performance of the application right?

Sorted data give us a better understanding of data and help us to search quickly.

Types of sorting Algorithms

There are many sorting algorithms techniques available in the C#. We may choose the appropriate algorithm as per our needs. 

A few of the important algorithms are,

  1. Bubble sort
  2. Merge sort
  3. Quick Sort
  4. Selection
  5. Heap
  6. Shell Sort 
  7. Counting sort
  8. Radix sort
  9. Bucket sort
  10. Insertion sort etc

Let’s discuss the Bubble sort algorithm now,

Bubble sorting 

Let’s have a practical example to understand Bubble sort in c#.

Suppose we have an array with 10 integers numbers like below,

int[] numbersArr = { 8, 2, 5, 10, 9, 7, 6, 4, 1, 3 };

In the bubble sorting, we compare first and second numbers and if second number is less than first number then swap both the numbers position. Repeat this algorithm method till end to get sorted array.

I mean in the above array, will compare 8 and 2. 2 is less than 8 so our array would be ,

int[] numbersArr = { 2, 8, 5, 10, 9, 7, 6, 4, 1, 3 };

Now compare 8 and 5. 5 is less than 8. So array would be,

int[] numbersArr = { 2, 5, 8, 10, 9, 7, 6, 4, 1, 3 };

Again, compare 8 and 10. 10 is greater than 8 so skip this position. Now compare 10 and 9. 9 is less than 10 so swap the position for 9 and 10.

Array would be,

int[] numbersArr = { 2,5,8,9,10, 7, 6, 4, 1, 3 };

This process will repeat till all elements will be sorted.

Let’s write code for the same,

using System;
namespace SortingDemo {
    class Program {
        static void Main(string[] args) {
            int[] numbersArr = {
                8,
                2,
                5,
                10,
                9,
                7,
                6,
                4,
                1,
                3
            };
            //Bubble Sort Algirithm logic
            for (int i = 0; i < (numbersArr.Length - 1); i++) {
                for (int j = 0; j < (numbersArr.Length - (1 + i)); j++) {
                    var lowerNumber = 0;
                    if (numbersArr[j] > numbersArr[j + 1]) {
                        lowerNumber = numbersArr[j + 1];
                        numbersArr[j + 1] = numbersArr[j];
                        numbersArr[j] = lowerNumber;
                    }
                }
            }
            // Print Logic after Sort
            Console.WriteLine("Print Sorted Numbers :");
            for (int i = 0; i <= (numbersArr.Length - 1); i++) {
                Console.Write(numbersArr[i]);
                Console.Write("\n");
            }
            Console.ReadLine();
        }
    }
}

In the above code,

  1. We have two “for” loops, first time both loops start from 0 to 9 (numberArray.Lenth). I mean i =0 to 9 and j= 0 to 9. Second loop compares first and second number.
  2. If first number is less than the second number then swap the number position.
  3. After that Increase second loop counter means j =1 now. Now compare second and third number. Please note that we have sorted array while j=0;
  4. Once second loop is finished (j=9), you notice that large number would be at the last.
  5. Now first loop increases to 1 (i=1) hence second loop will run till 0 to (10 – (1+1)) means 0 to 8. Last number is largest hence we will not compare that again.
  6. Array will be sorted once both “for” loop complete execution.

Let's execute this program and see the output

Output

Let’s discuss the advantages and disadvantages of this algorithm,

Advantages

  1. Easy to learn and implement
  2. Additional temporary storage is not required.
  3. Very popular

Disadvantages

  1. Not suitable with huge number of elements.
  2. Less use in the real world as it deals with less elements.

I hope you enjoyed and learned something from this article. 


Similar Articles