C# - Simple Sorting Algorithm (Sort an array of Integers)


A simple sorting algorithm in C#.

CODE
using System; class Program { public static void Main(string[] args) { int[] a = new int[] { 100, 1, 99, 45, 67, 34, 23, 2 }; Console.Write("Array before Sorting: "); for (int i = 0; i < a.Length; i++) { Console.Write(a[i] + " "); } for (int i = 0; i < a.Length-1; i++) { for(int j = i; j < a.Length; j++) { if(a[i] > a[j]) { int temp = a[i]; a[i] = a[j]; a[j] = temp; } } } Console.Write("\nArray after Sorting: "); for (int i = 0; i < a.Length; i++) { Console.Write(a[i] + " "); } Console.ReadKey(); } }

INPUT & OUTPUT
The below is the output of the program.

Comments