Sort the given doubly linked list using bubble sort.
Input : 5 4 3 2 1 Output : 1 2 3 4 5 Input : 2 1 3 5 4 Output :1 2 3 4 5
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.
Linked list before sorting 90 1 11 2 56 12 Linked list after sorting 1 2 11 12 56 90
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Comparison among Bubble Sort, Selection Sort and Insertion Sort
- C Program for Bubble Sort on Linked List
- Bubble Sort for Linked List by Swapping nodes
- XOR Linked List - A Memory Efficient Doubly Linked List | Set 1
- XOR Linked List – A Memory Efficient Doubly Linked List | Set 2
- Difference between Singly linked list and Doubly linked list
- Merge Sort for Doubly Linked List
- Insertion Sort for Doubly Linked List
- Sort the biotonic doubly linked list
- Sort a k sorted doubly linked list
- Sort the biotonic doubly linked list | Set-2
- Construct a Doubly linked linked list from 2D Matrix
- Bubble Sort
- Sorting Strings using Bubble Sort
- C Program for Bubble Sort
- C++ Program for Bubble Sort
- Java Program for Bubble Sort
- Python Program for Bubble Sort
- C++ Program for Recursive Bubble Sort
- Java Program for Recursive Bubble Sort
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.