Bubble Sort On Doubly Linked List

Sort the given doubly linked list using bubble sort.
Examples:

Input :  5  4  3  2  1
Output : 1  2  3  4  5

Input : 2  1  3  5  4
Output :1  2  3  4  5

Explanation

As we do in the bubble sort, here also we check elements of two adjacent node whether they are in ascending order or not, if not then we swap the element. We do this until every element get its original position.

In 1st pass the largest element get its original position and in 2nd pass 2nd largest element get its original position and in 3rd pass 3rd largest element get its original position and so on.
And finally whole list get sorted.

Note: If the list is already sorted then it will do only one pass.

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to sort a doubly linked list using
// bubble sort
#include <iostream>
using namespace std;
  
// structure of a node
struct Node {
    int data;
    Node* prev;
    Node* next;
};
  
/* Function to insert a node at the beginning of a linked list */
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;
}
   
/* Function to print nodes in a given linked list */
void printList(struct Node *start)
{
    struct Node *temp = start;
    cout << endl;
    while (temp!=NULL)
    {
        cout << temp->data << " ";
        temp = temp->next;
    }
}
   
/* Bubble sort the given linked list */
void bubbleSort(struct Node *start)
{
    int swapped, i;
    struct Node *ptr1;
    struct Node *lptr = NULL;
   
    /* Checking for empty list */
    if (start == NULL)
        return;
   
    do
    {
        swapped = 0;
        ptr1 = start;
   
        while (ptr1->next != lptr)
        {
            if (ptr1->data > ptr1->next->data)
            
                swap(ptr1->data, ptr1->next->data);
                swapped = 1;
            }
            ptr1 = ptr1->next;
        }
        lptr = ptr1;
    }
    while (swapped);
}
  
int main()
{
    int arr[] = {12, 56, 2, 11, 1, 90};
    int list_size, i;
   
    /* start with empty linked list */
    struct Node *start = NULL;
   
    /* Create linked list from the array arr[].
      Created linked list will be 1->11->2->56->12 */
    for (i = 0; i< 6; i++)
        insertAtTheBegin(&start, arr[i]);
   
    /* print list before sorting */
    printf("\n Linked list before sorting ");
    printList(start);
   
    /* sort the linked list */
    bubbleSort(start);
   
    /* print list after sorting */
    printf("\n Linked list after sorting ");
    printList(start);
     
    return 0;
}

chevron_right


Output:

Linked list before sorting 
90 1 11 2 56 12 
 Linked list after sorting 
1 2 11 12 56 90


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.