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: 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