Introducing Radical.sh

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

Selection 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. int min, pass, i;
  11. Console.WriteLine("Number of elements in the array ?");
  12. string s = Console.ReadLine();
  13. int x = Int32.Parse(s);
  14. Console.WriteLine("-----------------------");
  15. Console.WriteLine(" array elements ");
  16. Console.WriteLine("-----------------------");
  17. for (int j = 0; j < x; j++)
  18. {
  19. string s1 = Console.ReadLine();
  20. a[j] = Int32.Parse(s1);
  21. }
  22. for (pass = 0; pass < x - 1; pass++)
  23. {
  24. min = pass;
  25. for (i = pass + 1; i < x; i++)
  26. {
  27. if (a[min] > a[i])
  28. min = i;
  29. }
  30. if (min != pass)
  31. {
  32. int k = a[pass];
  33. a[pass] = a[min];
  34. a[min] = k;
  35. }
  36. }
  37. Console.WriteLine("--------------------------------------------");
  38. Console.WriteLine("Sorted elements of an array are(selection sort)");
  39. for (int j = 0; j < x; j++)
  40. Console.WriteLine(a[j]);
  41. }
  42. }
  43. }




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


..