Code golf: combining multiple sorted lists into a single sorted list

前端 未结 26 1947
余生分开走
余生分开走 2020-12-29 12:42

Implement an algorithm to merge an arbitrary number of sorted lists into one sorted list. The aim is to create the smallest working programme, in whatever language you like.

26条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-29 12:55

    VB is usually not the language of choice for code golf, but here goes anyway.

    The setup -

    
            Dim m1 As List(Of Integer) = New List(Of Integer)
            Dim m2 As List(Of Integer) = New List(Of Integer)
            Dim m3 As List(Of Integer) = New List(Of Integer)
            Dim m4 As List(Of Integer) = New List(Of Integer)
    
            m1.Add(1)
            m1.Add(2)
            m1.Add(3)
    
            m2.Add(4)
            m2.Add(5)
            m2.Add(6)
    
            m3.Add(7)
            m3.Add(8)
            m3.Add(9)
    
            Dim m5 As List(Of List(Of Integer)) = New List(Of List(Of Integer))
            m5.Add(m1)
            m5.Add(m2)
            m5.Add(m3)
    

    An attempt in VB.NET (without sort)

            While m5.Count > 0
                Dim idx As Integer = 0
                Dim min As Integer = Integer.MaxValue
                For k As Integer = 0 To m5.Count - 1
                    If m5(k)(0) < min Then min = m5(k)(0) : idx = k
                Next
                m4.Add(min) : m5(idx).RemoveAt(0)
                If m5(idx).Count = 0 Then m5.RemoveAt(idx)
            End While
    

    Another VB.NET attempt (with an allowed sort)

    
        Private Function Comp(ByVal l1 As List(Of Integer), ByVal l2 As List(Of Integer)) As Integer
            Return l1(0).CompareTo(l2(0))
        End Function
        .
        .
        .
        While m5.Count > 0
            m5.Sort(AddressOf Comp)
            m4.Add(m5(0)(0)) : m5(0).RemoveAt(0)
            If m5(0).Count = 0 Then m5.RemoveAt(0)
        End While
    

    The entire program -

            Dim rand As New Random
            Dim m1 As List(Of Integer) = New List(Of Integer)
            Dim m2 As List(Of Integer) = New List(Of Integer)
            Dim m3 As List(Of Integer) = New List(Of Integer)
            Dim m4 As List(Of Integer) = New List(Of Integer)
            Dim m5 As List(Of List(Of Integer)) = New List(Of List(Of Integer))
            m5.Add(m1)
            m5.Add(m2)
            m5.Add(m3)
    
            For Each d As List(Of Integer) In m5
                For i As Integer = 0 To 100000
                    d.Add(rand.Next())
                Next
                d.Sort()
            Next
    
            Dim sw As New Stopwatch
            sw.Start()
            While m5.Count > 0
                Dim idx As Integer = 0
                Dim min As Integer = Integer.MaxValue
                For k As Integer = 0 To m5.Count - 1
                    If m5(k)(0) < min Then min = m5(k)(0) : idx = k
                Next
                m4.Add(min) : m5(idx).RemoveAt(0)
                If m5(idx).Count = 0 Then m5.RemoveAt(idx)
            End While
            sw.Stop()
    
            'Dim sw As New Stopwatch
            'sw.Start()
            'While m5.Count > 0
            '    m5.Sort(AddressOf Comp)
            '    m4.Add(m5(0)(0)) : m5(0).RemoveAt(0)
            '    If m5(0).Count = 0 Then m5.RemoveAt(0)
            'End While
            'sw.Stop()
    
            Console.WriteLine(sw.Elapsed)
            Console.ReadLine()
    

提交回复
热议问题