Insertion Sort Ascending - CSharp Data Structure Algorithm

CSharp examples for Data Structure Algorithm:Sort

Description

Insertion Sort Ascending

Demo Code


using System.Collections.Generic;

public class Main{
        public static void InsertionSortAscending(this IList<sbyte> i_List)
        {//from  ww  w .  j a  va2s  . com
            int size = i_List.Count;
            for (int index = 1; index < size; ++index)
            {
                int readIndex = index - 1;
                sbyte examinedValue = i_List[index];
                //early break test for quicker processing of almost sorted arrays
                if (i_List[readIndex] > examinedValue)
                {
                    while ((readIndex >= 0) && (i_List[readIndex] > examinedValue))
                    {
                        i_List[readIndex + 1] = i_List[readIndex];
                        --readIndex;
                    }
                    i_List[readIndex + 1] = examinedValue;
                }
            }
        }
        public static void InsertionSortAscending(this IList<ushort> i_List)
        {
            int size = i_List.Count;
            for (int index = 1; index < size; ++index)
            {
                int readIndex = index - 1;
                ushort examinedValue = i_List[index];
                //early break test for quicker processing of almost sorted arrays
                if (i_List[readIndex] > examinedValue)
                {
                    while ((readIndex >= 0) && (i_List[readIndex] > examinedValue))
                    {
                        i_List[readIndex + 1] = i_List[readIndex];
                        --readIndex;
                    }
                    i_List[readIndex + 1] = examinedValue;
                }
            }
        }
        public static void InsertionSortAscending(this IList<uint> i_List)
        {
            int size = i_List.Count;
            for (int index = 1; index < size; ++index)
            {
                int readIndex = index - 1;
                uint examinedValue = i_List[index];
                //early break test for quicker processing of almost sorted arrays
                if (i_List[readIndex] > examinedValue)
                {
                    while ((readIndex >= 0) && (i_List[readIndex] > examinedValue))
                    {
                        i_List[readIndex + 1] = i_List[readIndex];
                        --readIndex;
                    }
                    i_List[readIndex + 1] = examinedValue;
                }
            }
        }
        public static void InsertionSortAscending(this IList<ulong> i_List)
        {
            int size = i_List.Count;
            for (int index = 1; index < size; ++index)
            {
                int readIndex = index - 1;
                ulong examinedValue = i_List[index];
                //early break test for quicker processing of almost sorted arrays
                if (i_List[readIndex] > examinedValue)
                {
                    while ((readIndex >= 0) && (i_List[readIndex] > examinedValue))
                    {
                        i_List[readIndex + 1] = i_List[readIndex];
                        --readIndex;
                    }
                    i_List[readIndex + 1] = examinedValue;
                }
            }
        }
        public static void InsertionSortAscending(this IList<byte> i_List)
        {
            int size = i_List.Count;
            for (int index = 1; index < size; ++index)
            {
                int readIndex = index - 1;
                byte examinedValue = i_List[index];
                //early break test for quicker processing of almost sorted arrays
                if (i_List[readIndex] > examinedValue)
                {
                    while ((readIndex >= 0) && (i_List[readIndex] > examinedValue))
                    {
                        i_List[readIndex + 1] = i_List[readIndex];
                        --readIndex;
                    }
                    i_List[readIndex + 1] = examinedValue;
                }
            }
        }
        public static void InsertionSortAscending(this IList<short> i_List)
        {
            int size = i_List.Count;
            for (int index = 1; index < size; ++index)
            {
                int readIndex = index - 1;
                short examinedValue = i_List[index];
                //early break test for quicker processing of almost sorted arrays
                if (i_List[readIndex] > examinedValue)
                {
                    while ((readIndex >= 0) && (i_List[readIndex] > examinedValue))
                    {
                        i_List[readIndex + 1] = i_List[readIndex];
                        --readIndex;
                    }
                    i_List[readIndex + 1] = examinedValue;
                }
            }
        }
        public static void InsertionSortAscending(this IList<double> i_List)
        {
            int size = i_List.Count;
            for (int index = 1; index < size; ++index)
            {
                int readIndex = index - 1;
                double examinedValue = i_List[index];
                //early break test for quicker processing of almost sorted arrays
                if (i_List[readIndex] > examinedValue)
                {
                    while ((readIndex >= 0) && (i_List[readIndex] > examinedValue))
                    {
                        i_List[readIndex + 1] = i_List[readIndex];
                        --readIndex;
                    }
                    i_List[readIndex + 1] = examinedValue;
                }
            }
        }
        public static void InsertionSortAscending(this IList<long> i_List)
        {
            int size = i_List.Count;
            for (int index = 1; index < size; ++index)
            {
                int readIndex = index - 1;
                long examinedValue = i_List[index];
                //early break test for quicker processing of almost sorted arrays
                if (i_List[readIndex] > examinedValue)
                {
                    while ((readIndex >= 0) && (i_List[readIndex] > examinedValue))
                    {
                        i_List[readIndex + 1] = i_List[readIndex];
                        --readIndex;
                    }
                    i_List[readIndex + 1] = examinedValue;
                }
            }
        }
        public static void InsertionSortAscending(this IList<float> i_List)
        {
            int size = i_List.Count;
            for (int index = 1; index < size; ++index)
            {
                int readIndex = index - 1;
                float examinedValue = i_List[index];
                //early break test for quicker processing of almost sorted arrays
                if (i_List[readIndex] > examinedValue)
                {
                    while ((readIndex >= 0) && (i_List[readIndex] > examinedValue))
                    {
                        i_List[readIndex + 1] = i_List[readIndex];
                        --readIndex;
                    }
                    i_List[readIndex + 1] = examinedValue;
                }
            }
        }
        #region Specialised primitive numerical type InsertionSort varients

        public static void InsertionSortAscending(this IList<int> i_List)
        {
            int size = i_List.Count;
            for (int index = 1; index < size; ++index)
            {
                int readIndex = index - 1;
                int examinedValue = i_List[index];
                //early break test for quicker processing of almost sorted arrays
                if (i_List[readIndex] > examinedValue)
                {
                    while ((readIndex >= 0) && (i_List[readIndex] > examinedValue))
                    {
                        i_List[readIndex + 1] = i_List[readIndex];
                        --readIndex;
                    }
                    i_List[readIndex + 1] = examinedValue;
                }
            }
        }
}

Related Tutorials