Skip to main content

# Bubble Sort in C# Console

Today in C#, I will teach you how to create a program for bubble sorting using C# console. We all know that bubble sort is a sorting algorithm that is repeatedly searching through lists that need to be sorted, comparing each pair of items and swapping them if they are in the wrong order.

Now, let's start this tutorial!

1. Let's start with creating a Console Application in C# for this tutorial by following the following steps in Microsoft Visual Studio 2010: Go to File, click New Project, and choose Console Application.

2. Name your project as bubbleSort as your module.

Note: This is a console application so we cannot have visual controls for this tutorial.

3. Add the following code in your module.

Make a function named sorting. This will automatically sort the inputted elements that we will create in Sub Main.

1. static public void sorting(int[] x, int y)
2. {
3. int i = default(int);
4. int a = default(int);
5. int t = default(int);
6. for (i = 0; i <= y - 1; i++)
7. {
8. for (a = i + 1; a <= y - 1; a++)
9. {
10. if (x[i] > x[a])
11. {
12. t = x[i];
13. x[i] = x[a];
14. x[a] = t;
15. }
16. }
17. }
18. }

5. For entering number of elements, put this code below.

1. Console.WriteLine("Bubble Sorting");
2. Console.WriteLine();
3. int num = default(int);
4. int i = default(int);
5. Console.Write("Enter Number of Elements: ");
6. num = int.Parse(Console.ReadLine());
7. int[] arr = new int[num + 1];
8. Console.WriteLine();
9. for (i = 0; i <= num - 1; i++)
10. {
11. Console.Write("Enter Element(" + (i + 1) + "): ");
12. arr[i] = int.Parse(Console.ReadLine());
13. }

6. For printing the inputted elements above, put this code below.

1. Console.WriteLine();
2. Console.WriteLine("Inputted Elements");
3. Console.WriteLine();
4. for (i = 0; i <= num - 1; i++)
5. {
6. Console.WriteLine("Element in (" + i.ToString() + "): " + arr[i]);
7. }

7. Lastly, we will code for the sorting of elements (bubble sort), put this code below.

1. Console.WriteLine();
2. sorting(arr, num);
3. Console.WriteLine("Sorted Elements");
4. Console.WriteLine();
5. for (i = 0; i <= num - 1; i++)
6. {
7. Console.WriteLine("Element in (" + i.ToString() + "): " + arr[i]);
8. }
9. Console.ReadLine();
10. }

Full source code:

1. Console.WriteLine();
2. sorting(arr, num);
3. Console.WriteLine("Sorted Elements");
4. Console.WriteLine();
5. for (i = 0; i <= num - 1; i++)
6. {
7. Console.WriteLine("Element in (" + i.ToString() + "): " + arr[i]);
8. }
9. Console.ReadLine();
10. }

Output:

For more inquiries and need programmer for your thesis systems in any kind of programming languages, just contact my number below.

Engr. Lyndon R. Bermoy
IT Instructor/System Developer/Android Developer
Mobile: 09079373999
Telephone: 826-9296
E-mail:[email protected]

Visit and like my page on Facebook at: https://www.facebook.com/BermzISware

Subscribe at my YouTube Channel at: Ser Bermz

Visit my website at: www.campcodes.com

### Tags

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

## Add new comment

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.