Generic Insertion Sort - CSharp Data Structure Algorithm

CSharp examples for Data Structure Algorithm:Sort

Description

Generic Insertion Sort

Demo Code


using System.Security;
using System.Runtime.Versioning;
using System.Runtime.CompilerServices;

public class Main{
    private static void InsertionSort(T[] keys, int lo, int hi, IComparer<T> comparer)
    {//from www  .  j a  v a  2  s  .c om
      for (int index1 = lo; index1 < hi; ++index1)
      {
        int index2 = index1;
        T x;
        for (x = keys[index1 + 1]; index2 >= lo && comparer.Compare(x, keys[index2]) < 0; --index2)
          keys[index2 + 1] = keys[index2];
        keys[index2 + 1] = x;
      }
    }
}

Related Tutorials