Insertion Sort - CSharp Data Structure Algorithm

CSharp examples for Data Structure Algorithm:Sort

Description

Insertion Sort

Demo Code


using System.Text;
using System.Linq;
using System.Collections.Generic;
using ComponentBind;
using System;//  www  . ja  v  a  2  s .  co m

public class Main{
        public static void InsertionSort<T>(this IList<T> list, Comparison<T> comparison)
      {
         int count = list.Count;
         for (int j = 1; j < count; j++)
         {
            T key = list[j];

            int i = j - 1;
            for (; i >= 0 && comparison(list[i], key) > 0; i--)
               list[i + 1] = list[i];
            if (i != j - 1)
               list[i + 1] = key;
         }
      }
}

Related Tutorials