我有3个整数类型的一维数组,每个数组的长度相同(在500-2000万之间),我想通过第二个数组并行(即保持相对位置)对它们进行排序,然后是第三个数组,然后是第一个数组。有没有人有任何关于在vb.net中做这件事最有效的方法的想法?
如果有帮助,第一个数组只是初始位置的记录(完成各种计算后,这三个数组将按此顺序重新排序)。我需要对它们进行排序,以确定第二个和第三个数组的唯一组合的数量(组合由数组中的位置确定-在下面的示例中,组合是(0-1-4),(1-1-6)等)。一旦确定这一点,我将根据第一个数组重新排序。
我看过array.sort,但它只涵盖了2个并行数组。我对将值放在元组(或任何其他格式)中有点谨慎,因为在处理之前转换500-2000万条记录时,这将是一个很大的开销(我假设-也许不是)。
例如:
Record Number Array: {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
Link Array: {1, 1, 2, 1, 1, 2, 2, 2, 1, 2}
Line Number Array: {4, 6, 3, 5, 6, 7, 3, 2, 3, 4}
按照第二个、第三个、第一个数组进行排序/排序后,预期输出将为:
Record Number (1st Array): {8, 0, 3, 1, 4, 7, 2, 6, 9, 5}
Link Array (2nd Array): {1, 1, 1, 1, 1, 2, 2, 2, 2, 2}
Line Number Array (3rd Array): {3, 4, 5, 6, 6, 2, 3, 3, 4, 7}
Array.sort只允许你并行排序2个数组,我对LinQ中的可用选项感到有点困惑。
有没有人对解决这个问题的最佳方法有什么建议?
干杯,
发布于 2013-08-13 22:38:20
我想你可以得到你的链接阵列的备份。然后使用记录数组对链接数组进行排序,然后使用行号数组对未排序的链接数组进行排序。你能试一下吗?
发布于 2013-08-13 23:46:39
您没有说您是否希望在时间或空间方面高效,但这需要大约3秒将2000万条记录从数组传输到列表,然后大约30秒对它们进行排序,使用不到1 1GB的RAM。在我的电脑上。
Option Infer On
Module Module1
Class Grouped
Property RecNo As Integer
Property Link As Integer
Property LineNo As Integer
Public Overrides Function ToString() As String
Return String.Format("({0}, {1}, {2})", Me.RecNo, Me.Link, Me.LineNo)
End Function
End Class
Sub Main()
' First try with the test data to show the correct result is obtained
Dim recNos = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
Dim links = {1, 1, 2, 1, 1, 2, 2, 2, 1, 2}
Dim lineNos = {4, 6, 3, 5, 6, 7, 3, 2, 3, 4}
' transfer the arrays to a List
Dim xs As New List(Of Grouped)
xs.Capacity = recNos.Length
For i = 0 To recNos.Length() - 1
xs.Add(New Grouped With {.RecNo = recNos(i), .Link = links(i), .LineNo = lineNos(i)})
Next
' sort the data
Dim ys = xs.OrderBy(Function(x) x.Link).ThenBy(Function(x) x.LineNo).ToList()
Console.WriteLine(String.Join(", ", ys))
' Now try with twenty million records
Dim rand = New Random()
Dim nRecs As Integer = 20000000
recNos = Enumerable.Range(0, nRecs - 1).ToArray()
ReDim links(nRecs - 1)
ReDim lineNos(nRecs - 1)
For i = 0 To nRecs - 1
links(i) = rand.Next(0, 9)
lineNos(i) = rand.Next(1, 9)
Next
Dim sw As New Stopwatch
sw.Start()
xs.Clear()
xs.Capacity = nRecs
For i = 0 To recNos.Length() - 1
xs.Add(New Grouped With {.RecNo = recNos(i), .Link = links(i), .LineNo = lineNos(i)})
Next
sw.Stop()
Console.WriteLine(sw.ElapsedMilliseconds.ToString())
sw.Restart()
ys = xs.OrderBy(Function(x) x.Link).ThenBy(Function(x) x.LineNo).ToList()
sw.Stop()
Console.WriteLine(sw.ElapsedMilliseconds.ToString())
Console.ReadLine()
End Sub
End Modulehttps://stackoverflow.com/questions/18211877
复制相似问题