Skip to main content

Bubble sort on linked list in vb.net

Bubble sort in linked list

Vb program for Bubble sort on linked list.

' Include namespace system
Imports System 
' Vb.net program for
' Bubble Sort For Linked List
Public Class Node
    Public data As Integer
    Public [next] As Node
    Public Sub New(ByVal data As Integer)
        Me.data = data
        Me.next = Nothing
    End Sub
End Class
public Class LinkedList
    Public head As Node
    ' Class constructors
    Public Sub New()
        Me.head = Nothing
    End Sub
    ' Add node at the beginning of linked list
    Public Sub insert(ByVal value As Integer)
        ' Create a new node
        Dim node As Node = New Node(value)
        ' Add node at front
        node.[next] = Me.head
        ' Make new head
        Me.head = node
    End Sub
    ' Display all elements
    Public Sub display()
        if (Me.head IsNot Nothing) Then
            Dim temp As Node = Me.head
            while (temp IsNot Nothing)
                ' Display node value
                Console.Write("  {0}", temp.data)
                ' Visit to next node
                temp = temp.[next]
            End While
        Else
            Console.WriteLine("Empty Linked list")
        End IF
    End Sub
    ' Perform bubble sort in single linked list
    Public Sub bubbleSort()
        if (Me.head IsNot Nothing) Then
            Dim current As Node = Nothing
            Dim status As Boolean = False
            Do
            ' Start with first node
            current = Me.head
            ' Reset working status
            status = False
            while (current IsNot Nothing AndAlso current.[next] IsNot Nothing)
                if (current.data > current.[next].data) Then
                    ' Swap node values
                    current.data = current.data + current.[next].data
                    current.[next].data = current.data - current.[next].data
                    current.data = current.data - current.[next].data
                    ' When node value change
                    status = True
                End If
                ' Visit to next node
                current = current.[next]
            End While
            Loop While (status)
        Else
            Console.WriteLine("Empty Linked list")
        End IF
    End Sub
    Public Shared Sub Main(ByVal args As String())
        Dim task As LinkedList = New LinkedList()
        ' Insert element of linked list
        task.insert(15)
        task.insert(5)
        task.insert(42)
        task.insert(9)
        task.insert(50)
        task.insert(7)
        Console.Write(" Before sort : ")
        ' Display all node
        task.display()
        task.bubbleSort()
        Console.Write( vbLf &" After sort  : ")
        task.display()
    End Sub
End Class

Output

 Before sort :   7  50  9  42  5  15
 After sort  :   5  7  9  15  42  50




Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment