Introducing Radical.sh

Forget Code launches a powerful code generator for building API's

Bubble sort programming in C#

  1. using System;
  2.  
  3. namespace forgetCode
  4. {
  5. class Program
  6. {
  7. public static void Main()
  8. {
  9. int[] a = new int[100];
  10. Console.WriteLine("Number of elements in the array ?");
  11. string s = Console.ReadLine();
  12. int x = Int32.Parse(s);
  13. Console.WriteLine("-----------------------");
  14. Console.WriteLine(" Array elements ");
  15. Console.WriteLine("-----------------------");
  16. for (int j = 0; j < x; j++)
  17. {
  18. string s1 = Console.ReadLine();
  19. a[j] = Int32.Parse(s1);
  20. }
  21. int limit = x - 1;
  22. for (int pass = 0; pass < x - 1; pass++)
  23. {
  24. for (int j = 0; j < limit - pass; j++)
  25. {
  26. if (a[j] > a[j + 1])
  27. {
  28. int k = a[j];
  29. a[j] = a[j + 1];
  30. a[j + 1] = k;
  31. }
  32. }
  33. }
  34. Console.WriteLine("------------------------------------------------");
  35. Console.WriteLine("Sorted elements of an array are(bubble sort)");
  36. for (int j = 0; j < x; j++)
  37. Console.WriteLine(a[j]);
  38. }
  39. }
  40. }


Output:
Number of elements in the array ?
4
-----------------------
Array elements
-----------------------
23
12
45
34
------------------------------------------------
Sorted elements of an array are(bubble sort)
12
23
34
45

...