【问题标题】:How to order array in lexicographical order vb.net如何按字典顺序排列数组 vb.net
【发布时间】:2018-12-07 07:44:49
【问题描述】:

这对我来说有点复杂

    Dim test() As Byte = New Byte() {50, 40, 30, 10, 10}
    Dim answer() As UInteger = SortLexicoGraphicallyBigIntegerArray(test)

答案是每个 Rotation 从最低数组值到最高数组值排序。

Rotation 0 = 50, 40, 30, 10, 10
Rotation 1 = 10, 50, 40, 30, 10
Rotation 2 = 10, 10, 50, 40, 30
Rotation 3 = 30, 10, 10, 50, 40
Rotation 4 = 40, 30, 10, 10, 50

当我手动对上面的这个数组进行排序时,我应该得到

Rotation 2 = 10, 10, 50, 40, 30
Rotation 1 = 10, 50, 40, 30, 10
Rotation 3 = 30, 10, 10, 50, 40
Rotation 4 = 40, 30, 10, 10, 50
Rotation 0 = 50, 40, 30, 10, 10

所以答案应该是2, 1, 3, 4, 0

我陷入了无限循环,我无法将手指放在它上面

这是我的代码

Public Function GetRotation(Data As Byte(), rotation As UInteger) As Byte()
   'Rotation Left
    Dim rotationData As New List(Of Byte)

    Dim start As UInteger = Data.Length - rotation Mod Data.Length

    For i = 0 To Data.Length - 1
        rotationData.Add(Data((start + i) Mod (Data.Length)))
    Next

    Return rotationData.ToArray()
End Function

Public Function SortLexicoGraphicallyBigIntegerArray(data As Byte()) As UInteger()
    Dim OrderedRotations As New List(Of UInteger)
    Dim index As Integer = 0
    Dim rowSwapped As Boolean
    Dim data1 As Byte()
    Dim data2 As Byte()

    For rotation As Short = 0 To data.Length - 1
        OrderedRotations.Add(rotation)
    Next

    For rotation As Long = data.Length - 1 To 0 Step -1
        Do
            rowSwapped = False
            data1 = GetRotation(data, OrderedRotations(rotation))
            data2 = GetRotation(data, OrderedRotations((rotation + 1) Mod (data.Length)))
            Do
                If data1(index) > data2(index) Then
                    'Swaps a full row in a few copies.
                    Dim tmpFirst As UInteger = OrderedRotations(index)
                    OrderedRotations(index) = OrderedRotations(index + 1)
                    OrderedRotations(index + 1) = tmpFirst

                    data1 = GetRotation(data, OrderedRotations(rotation))
                    data2 = GetRotation(data, OrderedRotations((rotation + 1) Mod (data.Length)))
                    rowSwapped = True
                End If
                index += 1
            Loop While index < data.Length - 1
            index = 0

        Loop While rowSwapped <> False
    Next
    Return OrderedRotations.ToArray()
End Function

这是我尝试过的新尝试,但仍然无法成功

    Public Function SortLexicoGraphicallyBigIntegerArray(ByRef data As Byte()) As UInteger()
        Dim OrderedRotations As New List(Of UInteger)
        Dim index As Integer = 0
        Dim data1 As Byte()
        Dim data2 As Byte()

        Dim rotation As UInteger = 0
        Dim eachRotation As Integer = 0
        Dim TryAgain As Boolean = False

        For rotation = 0 To data.Length - 1
            data1 = GetRotation(data, rotation)
            OrderedRotations.Add(rotation)
            If OrderedRotations.Count > 1 Then
redo:
                data1 = GetRotation(data, OrderedRotations(rotation))
                For eachRotation = OrderedRotations.Count - 1 To 0 Step -1
                    If OrderedRotations(eachRotation) = OrderedRotations(rotation) Then Continue For
                    data2 = GetRotation(data, OrderedRotations(eachRotation))

                    For index = 0 To data.Length - 1
                        If data1(index) = data2(index) Then
                            Continue For
                        ElseIf data1(index) < data2(index) Then
                            Exit For
                        ElseIf data1(index) > data2(index) Then
                            Dim tmpFirst As UInteger = OrderedRotations(rotation)
                            OrderedRotations(rotation) = OrderedRotations(eachRotation)
                            OrderedRotations(eachRotation) = tmpFirst
                            GoTo redo
                            Exit For
                        End If
                    Next
                Next
            End If
        Next

        Return OrderedRotations.ToArray()
    End Function

与我无法掌握的多层比较有关。

【问题讨论】:

    标签: vb.net lexicographic-ordering


    【解决方案1】:

    你可以做一个通用的二进制排序算法:-

    Dim flag As Boolean
    Dim tempvalue As dataarraytype
    Dim i As Integer
    
    Do
        flag = False
        For i = 0 to dataarray.length - 2
            If dataarray(i) > dataarray(i+1) Then   'Do the test you require
                'Swap values
                tempvalue = dataarray(i)
                dataarray(i) = dataarray(i+1)
                dataarray(i+1) = tempvalue
                flag = True
            End If
        Next
    Loop While flag
    

    在这里使用 FateOfLeap 的答案解决了完整的工作代码

    Public Function SortLexicoGraphicallyBigIntegerArray(ByRef data As Byte()) As UInteger()
        Dim OrderedRotations As New List(Of UInteger)
        Dim index As Integer = 0
        Dim data1 As Byte()
        Dim data2 As Byte()
    
        Dim rotation As UInteger = 0
        Dim eachRotation As Integer = 0
        Dim TryAgain As Boolean = False
    
        For rotation = 0 To data.Length - 1
            data1 = GetRotation(data, rotation)
            OrderedRotations.Add(rotation)
            If OrderedRotations.Count > 1 Then
                Dim flag As Boolean
                Do
                    flag = False
    
                    For eachRotation = OrderedRotations.Count - 1 To 0 Step -1
                        data1 = GetRotation(data, OrderedRotations(rotation))
                        If OrderedRotations(eachRotation) = OrderedRotations(rotation) Then Continue For
                        data2 = GetRotation(data, OrderedRotations(eachRotation))
    
                        For index = 0 To data.Length - 1
                            If data1(index) > data2(index) Then
                                Exit For
                            ElseIf data1(index) < data2(index) Then
                                Dim tmpFirst As UInteger = OrderedRotations(rotation)
                                OrderedRotations(rotation) = OrderedRotations(eachRotation)
                                OrderedRotations(eachRotation) = tmpFirst
                                flag = True
                            End If
                        Next
                    Next
                Loop While flag
            End If
        Next
    
        Return OrderedRotations.ToArray()
    End Function
    

    【讨论】:

    • 谢谢,但这似乎是从数组的开头搜索到同一数组的下一个元素我如何检查两个不同的数组?我的第二次尝试代码几乎可以工作。它只是在某处错过了一个完整的循环,并且没有用底部元素替换顶部元素,我无法将手指放在它上面。
    • 这是我当前的代码,适用于带有标志和循环的二进制排序代码。它现在似乎可以工作了,但是我必须将比较更改为&lt; 而不是&gt; 代码:pastebin.com/raw/YhRY8QLJ
    • 通常 > 用于升序, 上下文。为了编码清晰,您可以创建一个函数来与您需要的深度进行比较,返回一个真/假值......我希望这会有所帮助。
    • 是的,它完美无瑕,现在唯一的问题是它无法处理长数组,它开始在第 500 个数组处变慢,速度太慢以至于不实用。我正在考虑使用内存映射文件,但是也有 4 GB 的限制,这无济于事。例如,我不知道为什么我不能使用 40 GB。
    • 你能帮我解决我的新问题吗:stackoverflow.com/questions/53666811/…
    猜你喜欢
    • 1970-01-01
    • 2019-05-09
    • 2020-08-02
    • 2020-09-26
    • 1970-01-01
    • 2018-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多