## 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,

- What is Sorting?
- Type of Sorting Algorithm.
- Bubble Sort Algorithm.

**Prerequisites **

- Basic knowledge of C# and sorting is required.
- 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,

- Bubble sort
- Merge sort
- Quick Sort
- Selection
- Heap
- Shell Sort
- Counting sort
- Radix sort
- Bucket sort
- 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,

- 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. - If first number is less than the second number then swap the number position.
- 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*; - Once second loop is finished (
*j=9*), you notice that large number would be at the last. - 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. - 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**

- Easy to learn and implement
- Additional temporary storage is not required.
- Very popular

**Disadvantages**

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

I hope you enjoyed and learned something from this article.