Bubble Sort With Simple Explanation in C#

By | March 10, 2017

Do you want to learn about bubble sort in simple and lucid way? In this article I will discuss this sorting strategy with a C# programming example.

Become an Expert in Programming and Web Development.View all courses!

But to create a program for this sorting mechanism first of all we should understand what a bubble sort is.

Bubble Sort Description

First some theory. The bubble sort works in passes. The number of passes are one less then total number of elements.

Each pass starts with the last element in the array. The element is compared with the element just before itself. If the last element is less then the second last element, they change their location. But otherwise they remain unchanged.

With each pass the smallest element will be arranged at the starting of the array.

I will try to make things clear by a hand written image as I did not find any other less time consuming and more descriptive way other then this.

As seen in the above image I have an array of four elements i.e. 9,5,7,3.

Since there are four elements, it means there will be total three pass.

Pass 1 : In pass 1 the smallest element of the array will come and sit at first place. Each pass will start at last of the array. In this pass elements 7 and 3 are compared. Since 3 is smaller then 7, both of them will change their location.

Next 5 and 3 are compared. And Again 3 is less then 5, both will change their location.

Same with 3 and 9. With this process complete the smallest element 3 is bubbled to first place in array.

The array structure after pass 1 is {3,9,5,7} which is input for pass 2.

Pass 2: Now this pass is for second location. Again it starts from last location. 5 and 7 are compared. Since 5 is smaller then 7 and they are already arranged, no operation takes place.

9 and 5 are compared. They change their location. And hence element greater then 3 (i.e 5) comes and sit at place 2.

The array after pass 2 is {3,5 ,9, 7} which is input for pass 3.

Pass 3: This pass is for third location. 9 and 7 are compared and rearranged as explained previously.

and we get a fully sorted array after all the three passes.

Coding in C#

Once you understand the logic, it is not much difficult to code for bubble sort in C#.

Please find the below code to sort and array using the same logic explained above.

In the above code I have two loops. The outer loop is for the passes. The inner loop is for the traversing the array from end for each pass.

In the above code I am sorting the array { 9, 5, 1, 6, 7, 11, 2, 8, 3 }. And the output is shown in below figure.

Bubble sort in C#

Bubble sort in C#



I hope I have clearly explained the simplest sorting algorithm with a very simple and practical example in C#

  • Dmitry Bytchenko

    Side note: in the Real World applications we don’t use Bubble Sort since it’s too slow – O(n**2) complexity when O(n * log(n)) can be achieved. If you want to sort “intArray”, just put “Array.Sort(intArray);”. In case of custom order, just use the overloaded “Array.Sort” version,
    e.g. to sort in descending order: “Array.Sort(intArray, Comparer.Create((left, right) => -left.CompareTo(right)));”