Sorts an array of data using the insertion sort algorithm
using System;
public class InsertionSort {
public static void InsertNext(int i, int[] item) {
int current = item[i];
int j = 0;
while (current > item[j]) j++;
for (int k = i; k > j; k--)
item[k] = item[k-1];
item[j] = current;
}
public static void Sort(int[] item) {
for (int i = 1; i < item.Length; i++) {
InsertNext(i, item);
}
}
public static void Main() {
int[] item = new int[]{2,4,1,6,3,8,1,0,2,6,3,6};
Sort(item);
for(int i=0; i<item.Length;i++){
Console.WriteLine(item[i]);
}
}
}
Related examples in the same category