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
- C Program for Bubble Sort on Linked List
- Bubble Sort for Linked List by Swapping nodes
- Sort the biotonic doubly linked list
- Merge Sort for Doubly Linked List
- Sort a k sorted doubly linked list
- Insertion Sort for Doubly Linked List
- Sort the biotonic doubly linked list | Set-2
- Difference between Singly linked list and Doubly linked list
- XOR Linked List – A Memory Efficient Doubly Linked List | Set 2
- XOR Linked List - A Memory Efficient Doubly Linked List | Set 1
- QuickSort on Doubly Linked List
- Reverse a Doubly Linked List
- Reverse a Doubly Linked List | Set-2
- Comparison among Bubble Sort, Selection Sort and Insertion Sort
- Reverse a Doubly linked list using recursion
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.