Bubble Sort On Doubly Linked List

Sorting is one of the famous techniques used to rearrange the array or the list of elements. In this article, we will learn how to sort a doubly linked list using bubble sort.

Sorting a linked list can be troublesome sometimes, but there are multiple algorithms to do so easily. A bubble sort on doubly linked list might not be the most efficient one for sorting a doubly linked List but it does give us one of the simple solutions to the problem.

Bubble sort on doubly linked list is similar to a single list just we also have to update the previous pointer.
Suppose we have a doubly-linked list as 3 4 5 2 1
Then after bubble sorting, we will have 1 2 3 4 5

Approach on how to sort a doubly linked list

The idea is similar to what we do in arrays, we keep on checking the adjacent elements and swap the elements if the first element is greater than the next element. We keep on doing this till all the elements are arranged in ascending order for doing bubble sort on doubly linked list.

Algorithm for bubble sort on doubly linked list

  • We use a do-while loop and a swap variable to denote if we want to swap the elements or not
  • Traverse the doubly Linked List and if for a current element if the next element is greater than the current element then swap the two nodes by changing their pointers and mark swap = 1
  • We keep doing this until we get all the elements in the list in sorted order.
  • Finally, we return the head of the doubly linked list for doing bubble sort on doubly linked list.

Code Implementation

Bubble Sort On Doubly Linked List

#include 
using namespace std;
struct Node {
    int data;
    Node* prev;
    Node* next;
};
void insertAtTheBegin(struct Node **start_ref, int data)
{
    struct Node *ptr1 = new Node;
    ptr1->data = data;
    ptr1->next = *start_ref;
    if (*start_ref != NULL)
    (*start_ref)->prev = ptr1;
    *start_ref = ptr1;
}
void display(struct Node *start)
{
    struct Node *temp = start;
    cout << endl;
    while (temp!=NULL)
    {
        cout << temp->data << " ";
        temp = temp->next;
    }
}
void bubbleSort(struct Node *start)
{
    int swapp, i;
    struct Node *ptr1;
    struct Node *lptr = NULL;
    if (start == NULL)
        return;

    do
    {
        swapp = 0;
        ptr1 = start;

        while (ptr1->next != lptr)
        {
            if (ptr1->data > ptr1->next->data)
            {
                swap(ptr1->data, ptr1->next->data);
                swapp = 1;
            }
            ptr1 = ptr1->next;
        }
        lptr = ptr1;
    }
    while (swapp);
}

int main()
{
    int arr[] = {3, 4, 5, 2, 1};
    int list_size, i;
    list_size = sizeof(arr)/sizeof(arr[0]);
    struct Node *start = NULL;
    for (i = 0; i< list_size; i++)
        insertAtTheBegin(&start, arr[i]);
    bubbleSort(start);
    display(start); 
    return 0;
}
class PrepBytes
{
static class Node
{
    int data;
    Node prev;
    Node next;
};
static Node pushatbegin( Node start_ref, int data)
{
    Node ptr1 = new Node();
    ptr1.data = data;
    ptr1.next = start_ref;
    if (start_ref != null)
    (start_ref).prev = ptr1;
    start_ref = ptr1;
    return start_ref;
}
static void display( Node start)
{
    Node temp = start;
    System.out.println();
    while (temp != null)
    {
        System.out.print( temp.data + " ");
        temp = temp.next;
    }
}
static Node bubbleSort( Node start)
{
    int swapp, i;
    Node ptr1;
    Node lptr = null;
    if (start == null)
        return null;

    do
    {
        swapp = 0;
        ptr1 = start;

        while (ptr1.next != lptr)
        {
            if (ptr1.data > ptr1.next.data)
            {
                int t = ptr1.data;
                ptr1.data = ptr1.next.data;
                ptr1.next.data = t;
                swapp = 1;
            }
            ptr1 = ptr1.next;
        }
        lptr = ptr1;
    }
    while (swapp != 0);
        return start;
}
public static void main(String args[])
{
    int arr[] = {3, 4, 5 ,2 ,1};
    int i;
    Node start = null;
    for (i = 0; i < 5; i++)
        start=pushatbegin(start, arr[i]);
    start = bubbleSort(start);
    display(start);
}
}

Output: 1 2 3 4 5

Space Complexity: O(1), no extra space is used.

Conclusion:

In this article, we tried to explain the bubble sort on a doubly linked list by modifying the node pointer and walkthrough the approach and algorithm along with the code implementation in various languages.

Related Articles
Doubly circular linked list in data structure Application of doubly linked list
Applications of linked list Singly linked list vs doubly linked list
Advantages and disadvantages of linked list Doubly linked list all operations in C
Binary search in linked list Bubble sort linked list
Deletion in doubly linked list Delete the middle node of a linked list
Polynomial addition using linked list Find max value and min value in linked list
Insert a node at a specific position in a linked list Swap nodes in linked list
Add two numbers represented by linked lists Find starting point of loop in linked list
Merge sort linked list Delete a node from linked list at a given position
Remove duplicates from unsorted linked list Reverse a linked list in Python

FAQs on how to sort a doubly linked list

  1. What is bubble sort?
    Bubble sort is a sorting technique that works frequently by swapping the adjacent elements if they are not in the correct order.

  2. What is a doubly linked list?
    A doubly linked list is a data structure which contains nodes with points to the previous node as well to the next node.

  3. What is the space and time complexity of bubble sort?
    The time complexity for bubble sort is O(N^2) and space complexity is O(1)

Leave a Reply

Your email address will not be published. Required fields are marked *