Skip to main content

Bubble sort on linked list in c++

Bubble sort in linked list

C++ program for Bubble sort on linked list. Here more solutions.

// Include header file
#include <iostream>
#include <string>
#include <vector>

//  Stdc++11 program for
//  Bubble Sort For Linked List
class Node
{
    public:
    int data;
    Node* next;
    Node(int data)
    {
        this->data = data;
        this->next = nullptr;
    }
};
class LinkedList
{
    public:
    Node* head;
    //  Class constructors
    LinkedList()
    {
        this->head = nullptr;
    }
    //  Add node at the beginning of linked list
    void insert(int value)
    {
        //  Create a new node
        Node* node = new Node(value);
        //  Add node at front
        node->next = this->head;
        //  Make new head
        this->head = node;
    }
    //  Display all elements
    void display()
    {
        if (this->head != nullptr)
        {
            Node* temp = this->head;
            while (temp != nullptr)
            {
                //  Display node value
                std::cout << "  " + std::to_string(temp->data);
                //  Visit to next node
                temp = temp->next;
            }
        }
        else
        {
            std::cout << "Empty Linked list" << std::endl;
        }
    }
    //  Perform bubble sort in single linked list
    void bubbleSort()
    {
        if (head != nullptr)
        {
            Node* current = nullptr;
            bool status = false;
            do
            {
                //  Start with first node
                current = this->head;
                //  Reset working status
                status = false;
                while (current != nullptr && current->next != nullptr)
                {
                    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;
                    }
                    //  Visit to next node
                    current = current->next;
                }
            } while (status);
        }
        else
        {
            std::cout << "Empty Linked list" << std::endl;
        }
    }
};
int main(int argc, char **argv){
    LinkedList* task = 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);
    std::cout << " Before sort : ";
    //  Display all node
    task->display();
    task->bubbleSort();
    std::cout << "\n After sort  : ";
    task->display();
    return 0;
};

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