C# Program to sort an array using Insertion Sort

Program

using System; 
namespace Application {
	public class InsertionSort {
		public InsertionSort () {
		}
		public static void Main() {
			Console.WriteLine ("Enter the size of the array"); 
			int size = Convert.ToInt32(Console.ReadLine ()); 
			int [] arr = new int[size]; 
			Console.WriteLine("Enter the array elements"); 
			for (int i = 0; i < arr.Length; i++)
				arr[i] = Convert.ToInt32(Console.ReadLine()); 
			InsertionSort sort = new InsertionSort(); 
			sort.insertionSort(arr); 
		}
		void insertionSort(int[] arr) {
			int i, j, temp; 
			for (i = 0; i < arr.Length; i++) {
				temp = arr[i]; 
				j = i - 1; 
				while ((j >= 0) && (arr[j] > temp)) {
					arr[j + 1] = arr[j]; 
					j = j - 1; 
				}
				arr[j + 1] = temp; 
			}
			Console.WriteLine("Sorted array elements:"); 
			for (i = 0; i < arr.Length; i++)
				Console.Write(arr[i] + "\t"); 
			Console.WriteLine(); 
		}
	}
}

Output

$ mcs InsertionSort.cs
$ mono InsertionSort.exe
Enter the size of the array6
Enter the array elements
12
345
56
129
-98
0
Sorted array elements:
-98     0       12      56      129     345