Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net 简单排序算法不起作用-索引问题_Vb.net_Sorting - Fatal编程技术网

Vb.net 简单排序算法不起作用-索引问题

Vb.net 简单排序算法不起作用-索引问题,vb.net,sorting,Vb.net,Sorting,我试图使用算法对数组进行排序,但我的索引有问题 Sub Main() Dim List() As Integer = {9, 8, 5, 6} Dim InnerPointer As Integer = 0 Dim CurrentValue As Integer For OutPointer = 1 To List.Length - 1 CurrentValue = List(OutPointer) While InnerP

我试图使用算法对数组进行排序,但我的索引有问题

    Sub Main()
    Dim List() As Integer = {9, 8, 5, 6}
    Dim InnerPointer As Integer = 0
    Dim CurrentValue As Integer
    For OutPointer = 1 To List.Length - 1
        CurrentValue = List(OutPointer)
        While InnerPointer > 0 And List(InnerPointer) > CurrentValue
            List(InnerPointer + 1) = List(InnerPointer)
            InnerPointer = InnerPointer - 1
        End While
        List(InnerPointer + 1) = CurrentValue
    Next
    For Each element In List
        Console.WriteLine(element)
    Next
    Console.ReadKey()
End Sub

我将InnerPointer定义为0,因为我的数组是基于0索引的,但这意味着我的While循环不会启动…但我确实想比较索引0和索引1,以确定哪个数字更大。我一辈子都无法计算出逻辑谬误在哪里,所以请将其更改为
>=0
。@jimcilhinney-这将导致索引超出范围,除非您的算法错误或算法实现错误。如果我们不知道算法是什么,我们又怎么知道呢?做你本来应该做的事。用文字写出算法,然后逐步完成代码,在每一步中,将代码与算法进行比较,以确保它正在做它应该做的事情。如果总是这样,那么算法就错了。