Skip to main content

Bubble sort on linked list in ruby

Bubble sort in linked list

Ruby program for Bubble sort on linked list. Here more solutions.

# Ruby program for
# Bubble Sort For Linked List
class Node
    # Define the accessor and reader of class Node
    attr_reader :data,:next
    attr_accessor :data,:next
    def initialize( data)
        self.data = data
        self.next = nil
    end
end
class LinkedList
    # Define the accessor and reader of class LinkedList
    attr_reader :head
    attr_accessor :head
    # Class constructors
    def initialize()
        self.head = nil
    end
    # Add node at the beginning of linked list
    def insert( value)
        # Create a new node
        node = Node.new(value)
        # Add node at front
        node.next = self.head
        # Make new head
        self.head = node
    end
    # Display all elements
    def display()
        if (self.head != nil)
            temp = self.head
            while (temp != nil)
                # Display node value
                print("  ", temp.data)
                # Visit to next node
                temp = temp.next
            end
        else
            print("Empty Linked list\n")
        end
    end
    # Perform bubble sort in single linked list
    def bubbleSort()
        if (self.head != nil)
            current = nil
            status = false
            loop do
                # Start with first node    
                current = self.head
                # Reset working status
                status = false
                while (current != nil && current.next != nil)
                    if (current.data > current.next.data)
                        # 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
                    # Visit to next node
                    current = current.next
                end
                if(!(status))
                    break
                end
            end
        else
            print("Empty Linked list\n")
        end
    end
    def self.main()
        task = LinkedList.new()
        # Insert element of linked list
        task.insert(15)
        task.insert(5)
        task.insert(42)
        task.insert(9)
        task.insert(50)
        task.insert(7)
        print(" Before sort : ")
        # Display all node
        task.display()
        task.bubbleSort()
        print("\n After sort  : ")
        task.display()
    end
end
LinkedList.main()

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