简体   繁体   English

插入排序动态数组

[英]Insertion sort dynamic array

I'm trying to compile the below code: (normally get and set operation) please assist with reviewing the insertion sort function, all the rest compiling perfectly.我正在尝试编译以下代码:(通常是 get 和 set 操作)请协助检查插入排序功能,其余所有编译完美。 The return value comparing 2 cells only (and it's correct).仅比较 2 个单元格的返回值(它是正确的)。
any idea what can cause that?知道是什么原因造成的吗?

  private static void Main()
    {
        IArray<int?> arr = Read();
        Console.WriteLine(arr);
        SelectionSort(arr);
        Console.WriteLine(arr);
        if (arr.Length >= 2)
        {
            arr.Set(arr.Length - 2, null);
            Console.WriteLine(arr);
        }
        if (arr.Length >= 1)
        {
            arr.Set(arr.Length - 1, null);
            Console.WriteLine(arr);
        }
        SelectionSort(arr);
        Console.WriteLine(arr);
        Console.WriteLine("(" + arr.Get(100) + ")");
        Console.WriteLine("insertion sort");
        int n = arr.Length;
        Insertionsort(arr, n);
        Console.WriteLine(arr);
        //// throw exception
        //_ = new DynArr<int>();
    }

    private static IArray<int?> Read()
    {
        var arr = new DynArr<int?>();
        Console.Write("Enter size >> ");
        var size = int.Parse(Console.ReadLine().Trim());
        for (var i = 0; i < size; ++i)
        {
            Console.Write("Enter [" + i + "] >> ");
            arr.Set(i, int.Parse(Console.ReadLine().Trim()));
        }
        return arr;
    }

    }
    private static void Insertionsort(IArray<int?> arr, int n)
    {
        for (int i = 1; i < n; i++)
        {
            var j = i - 1;
            voidcompare(arr, i, j);
          

        }
    }
    private static void voidcompare(IArray<int?> arr, int i, int j)
    {
        var c = arr.Get(i);
        while (j >= 0 && arr.Get(j) > c)
        {
            arr.Set(j + 1, c);
        }
       
    }
}

} }

Your Sort alg.你的排序算法。 isn't corect.不正确。 See hier how it's working - Insertion Sort .看看它是如何工作的 - 插入排序 Bellow i provide you a possible implementation.下面我为您提供一个可能的实现。

public class Program
{
    static void Main()
    {
        var collection = new List<int?> { 25, 71, 43, -1, 15, 0, 38 };

        DoInsertionSort(collection, collection.Count);

        Console.WriteLine(string.Join(Environment.NewLine, collection));
    }

    static void DoInsertionSort(IList<int?> collection, int length)
    {
        if (collection is null || collection.Count == 0 || length <= 0 || length > collection.Count)
        {
            return;
        }

        var previous = default(int?);

        for (int index = 0; index < length; index++)
        {
            var current = collection.Get(index);

            if (current < previous)
            {
                SwapUpToStart(collection, current, index);
            }

            previous = collection.Get(index);
        }
    }

    private static void SwapUpToStart(IList<int?> collection, int? current, int currentIndex)
    {
        for (int index = currentIndex - 1; index >= 0; index--)
        {
            var previous = collection.Get(index);

            if (current >= previous)
            {
                break;
            }

            collection.Swap(index, current, previous);
        }
    }
}

public static class YourIArray_T_Implementation
{
    public static int? Get(this IList<int?> collection, int index)
    {
        return collection[index];
    }

    public static void Swap(this IList<int?> collection, int index, int? current, int? previous)
    {
        collection[index] = current;
        collection[index + 1] = previous;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM