Skip to main content

Bubble sort on linked list in

Bubble sort in linked list

Vb program for Bubble sort on linked list.

' Include namespace system
Imports System 
' 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) = data = 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}",
                ' Visit to next node
                temp = temp.[next]
            End While
            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
            ' Start with first node
            current = Me.head
            ' Reset working status
            status = False
            while (current IsNot Nothing AndAlso current.[next] IsNot Nothing)
                if ( > current.[next].data) Then
                    ' Swap node values
           = + current.[next].data
                    current.[next].data = - current.[next].data
           = - current.[next].data
                    ' When node value change
                    status = True
                End If
                ' Visit to next node
                current = current.[next]
            End While
            Loop While (status)
            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
        Console.Write(" Before sort : ")
        ' Display all node
        Console.Write( vbLf &" After sort  : ")
    End Sub
End Class


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


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