如何在vb.net中按字典顺序排列数组
这对我来说有点复杂如何在vb.net中按字典顺序排列数组,vb.net,lexicographic-ordering,Vb.net,Lexicographic Ordering,这对我来说有点复杂 Dim test() As Byte = New Byte() {50, 40, 30, 10, 10} Dim answer() As UInteger = SortLexicoGraphicallyBigIntegerArray(test) 答案是从最低数组值到最高数组值排序的每个旋转 Rotation 0 = 50, 40, 30, 10, 10 Rotation 1 = 10, 50, 40, 30, 10 Rotation 2 = 10, 10,
Dim test() As Byte = New Byte() {50, 40, 30, 10, 10}
Dim answer() As UInteger = SortLexicoGraphicallyBigIntegerArray(test)
答案是从最低数组值到最高数组值排序的每个旋转
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
公共函数GetRotation(数据为Byte(),旋转为UInteger)为Byte()
'向左旋转
Dim旋转数据作为新列表(字节)
尺寸起始为UInteger=Data.Length-旋转模式Data.Length
对于i=0到Data.Length-1
rotationData.Add(数据((start+i)Mod(Data.Length)))
下一个
返回rotationData.ToArray()
端函数
公共函数SortlexicographicalyBiginteGerray(数据为Byte())为UInteger()
Dim OrderedRotations作为新列表(UInteger的)
作为整数的Dim索引=0
Dim行交换为布尔值
Dim data1作为字节()
Dim data2作为字节()
对于旋转,短=0到数据。长度-1
OrderedRotations.Add(旋转)
下一个
对于长=数据的旋转。长度-1到0步骤-1
做
行交换=假
data1=GetRotation(数据、OrderedRotations(旋转))
data2=GetRotation(数据,OrderedRotations((rotation+1)Mod(data.Length)))
做
如果data1(索引)>data2(索引),则
'将整行交换为几份副本。
Dim tmpFirst As UInteger=有序旋转(索引)
有序旋转(索引)=有序旋转(索引+1)
有序旋转(索引+1)=tmpFirst
data1=GetRotation(数据、OrderedRotations(旋转))
data2=GetRotation(数据,OrderedRotations((rotation+1)Mod(data.Length)))
行交换=真
如果结束
指数+=1
循环,而索引
这是一个新的尝试,我试过了,但仍然无法成功
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
公共函数SortlexicographicallyBiginegerarray(ByRef数据作为Byte())作为UInteger()
Dim OrderedRotations作为新列表(UInteger的)
作为整数的Dim索引=0
Dim data1作为字节()
Dim data2作为字节()
尺寸旋转为UInteger=0
将每个旋转调整为整数=0
Dim TryAgain作为布尔值=False
对于旋转=0到数据。长度-1
data1=GetRotation(数据,旋转)
OrderedRotations.Add(旋转)
如果OrderedRotations.Count>1,则
重做:
data1=GetRotation(数据、OrderedRotations(旋转))
For eachRotation=OrderedRotations.计数-1到0步骤-1
如果OrderedRotations(eachRotation)=OrderedRotations(rotation),则继续
data2=GetRotation(数据,有序旋转(每个旋转))
对于索引=0到数据。长度-1
如果数据1(索引)=数据2(索引),则
继续
ElseIf data1(索引)data2(索引)然后
Dim tmpFirst As UInteger=有序旋转(旋转)
有序旋转(旋转)=有序旋转(每次旋转)
有序旋转(每次旋转)=tmpFirst
转到重做
退出
如果结束
下一个
下一个
如果结束
下一个
Return OrderedRotations.ToArray()
端函数
与多层比较有关,我无法理解。您可以使用通用的二进制排序算法:-
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
使用Fateoflip的答案解决了这个问题,这里是完整的工作代码
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
公共函数SortlexicographicallyBiginegerarray(ByRef数据作为Byte())作为UInteger()
Dim OrderedRotations作为新列表(UInteger的)
作为整数的Dim索引=0
Dim data1作为字节()
Dim data2作为字节()
尺寸旋转为UInteger=0
将每个旋转调整为整数=0
Dim TryAgain作为布尔值=False
对于旋转=0到数据。长度-1
data1=GetRotation(数据,旋转)
OrderedRotations.Add(旋转)
如果OrderedRotations.Count>1,则
将标志变暗为布尔值
做
flag=False
For eachRotation=OrderedRotations.计数-1到0步骤-1
data1=GetRotation(数据、OrderedRotations(旋转))
如果OrderedRotations(eachRotation)=OrderedRotations(rotation),则继续
data2=GetRotation(数据,有序旋转(每个旋转))
对于索引=0到数据。长度-1
如果data1(索引)>data2(索引),则
退出
ElseIf data1(索引)
谢谢,但这似乎是从数组的开头搜索到同一数组的下一个元素。如何在两个不同的数组之间进行检查?我的第二次尝试代码几乎可以工作。。它只是遗漏了某个地方的一个完整循环,没有用底部元素替换顶部元素,我无法将手指放在上面。这是我当前的代码,适用于您的二进制排序代码