C#

# Programmingempire

This article explains Bubble Sort in C#. Basically, Bubble Sort is the most simple sorting algorithm. In general, bubble sort works by scanning each element of a list. Further, it checks the order of adjacent elements in the list. If the algorithm finds that the adjacent elements are in wrong order, it swaps these elements. For instance, if bubble sort is sorting in ascending order, it will check whether the next element is smaller than the previous one. If it is so, it will swap the two elements. Otherwise, it leaves the elements as they are.

Similarly, in the case of sorting in descending order, the bubble sort ensures that the next element is smaller than the previous one. Otherwise, it swaps the two elements. The following example shows how bubble sort works.

### Example of Bubble Sort

Basically, the bubble sort algorithm takes as many passes as the number of elements in the list. For example, suppose the list is

[9, 3, 2, 1, 5, 8]. Therefore, it will take 6 passes.

Pass 1: [9, 3, 2, 1, 5, 8] -> [3, 9, 2, 1, 5, 8] -> [3, 2, 9, 1, 5, 8] -> [3, 2, 1, 9, 5, 8] -> [3, 2, 1, 5, 9, 8] -> [3, 2, 1, 5, 8, 9]

After taht, the second pass starts.

Pass 2: [3, 2, 1, 5, 8, 9] -> [2, 3, 1, 5, 8, 9] -> [2, 1, 3, 5, 8, 9] -> [2, 1, 3, 5, 8, 9] -> [2, 1, 3, 5, 8, 9] -> [2, 1, 3, 5, 8, 9]

Pass 3: [2, 1, 3, 5, 8, 9] -> [1, 2, 3, 5, 8, 9] -> [1, 2, 3, 5, 8, 9] -> [1, 2, 3, 5, 8, 9] -> [1, 2, 3, 5, 8, 9] -> [1, 2, 3, 5, 8, 9]

As can be seen in the example above, after pass 3, the list has been sorted. However, the bubble sort algorithm has no way to know about this information. Hence, it keeps on performing the remaining passes. Furthermore, in pass 2, after the second iterations, no swapping takes place. Since, the elements are in correct order. Similarly, in pass 3, swapping doesn’t occur after the first iteration. Still the remaining iterations are performed. Therefore, the bubble sort algorithms is not the most efficient sorting algorithm.

### Implementation of Bubble Sort in C#

The following code shows how to implement bubble sort in C#.

using System;

namespace BubbleSort
{
class Program
{
static void Main(string[] args)
{
int[] myarray = {4,1, 9, -13, 90, 56, 81, 34, -2, -15, 60, 88};
Console.WriteLine("Array before sorting...");
foreach (int x in myarray)
Console.Write(x + "  ");
Sort.BubbleSort(myarray);
Console.WriteLine("\nArray after sorting...");
foreach (int x in myarray)
Console.Write(x + "  ");
Console.WriteLine();
}
}
class Sort
{
public static void BubbleSort(int[] arr)
{
for(int i=0;i<arr.Length-1;i++)
{
for(int j=0;j<arr.Length-i-1;j++)
{
if (arr[j] > arr[j + 1])
swap(ref arr[j], ref arr[j + 1]);
}
}
}
static void swap(ref int m, ref int n)
{
int t = m;
m = n;
n = t;
}
}
}

Output

## Further Reading

Selection Sort in C#

Insertion Sort in C#

Bubble Sort in C#

How to Create Instance Variables and Class Variables in Python

Comparing Rows of Two Tables with ADO.NET

Example of Label and Textbox Control in ASP.NET

One Dimensional and Two Dimensuonal Indexers in C#

Private and Static Constructors in C#

Methods of Array Class

Anonymous Functions in C#

Programs to Find Armstrong Numbers in C#

Matrix Multiplication in C#

One Dimensional and Two Dimensional Indexers in C#

Static Class Example in C#

Rotating an Array in C#

Generic IList Interface and its Implementation in C#

Recursive Binary search in C#

C# Practice Questions

Creating Navigation Window Application Using WPF in C#

Find Intersection Using Arrays

An array of Objects and Object Initializer

Performing Set Operations in LINQ

Using Quantifiers in LINQ

Data Binding Using BulletedList Control

Grouping Queries in LINQ

Generic Binary Search in C#

Understanding the Quantifiers in LINQ

Join Operation using LINQ

Deferred Query Execution and Immediate Query Execution in LINQ

Examples of Query Operations using LINQ in C#

An array of Objects and Object Initializer

Language-Integrated Query (LINQ) in C#

How Data Binding Works in WPF

Examples of Connected and Disconnected Approach in ADO.NET

New Features in C# 9

IEnumerable and IEnumerator Interfaces

KeyValuePair and its Applications

C# Root Class – Object

Access Modifiers in C#

Learning Properties in C#

Learning All Class Members in C#

Examples of Extension Methods in C#

How to Setup a Connection with SQL Server Database in Visual Studio

Understanding the Concept of Nested Classes in C#

LINQ To SQL Examples

A Beginner’s Tutorial on WPF in C#

Explaining C# Records with Examples

Everything about Tuples in C# and When to Use?

Creating Jagged Arrays in C#

Linear Search and Binary search in C#

Learning Indexers in C#

Object Initializers in C#

Examples of Static Constructors in C#

When should We Use Private Constructors?

C# Basic Examples

IEqualityComparer Interface

C#

C#

C#