Bubble Sort in C# Console

Submitted by: 
Language: 
Visitors have accessed this post 469 times.

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:

output

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

Engr. Lyndon Bermoy
IT Instructor/System Developer/Android Developer/Freelance Programmer

If you have some queries, feel free to contact the number or e-mail below.
Mobile: 09488225971
Landline: 826-9296
E-mail:[email protected]

Add and Follow me on Facebook: https://www.facebook.com/donzzsky

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


Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.