Sort the doubly linked list using insertion sort technique.
Initial doubly linked list
Doubly Linked List after applying insertion sort
Below is a simple insertion sort algorithm for doubly linked list.
1) Create an empty sorted (or result) doubly linked list.
2) Traverse the given doubly linked list, do following for every node.
……a) Insert current node in sorted way in sorted(or result) doubly linked list.
3) Change head of given linked list to head of sorted (or result) list.
The main step is (2.a) which has been covered in below post.
Sorted Insert for Doubly Linked List
Doubly Linked List Before Sorting 8 12 10 5 3 9 Doubly Linked List After Sorting 3 5 8 9 10 12
Time Complexity: O(n2)
This article is contributed by Ayush Jauhari. 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Doubly Linked List | Set 1 (Introduction and Insertion)
- Doubly Circular Linked List | Set 1 (Introduction and Insertion)
- Insertion at Specific Position in a Circular Doubly Linked List
- Sort the biotonic doubly linked list
- Sort a k sorted doubly linked list
- Merge Sort for Doubly Linked List
- Bubble Sort On 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
- Construct a Doubly linked linked list from 2D Matrix
- Doubly Linked List Tutorial
- Reverse a Doubly Linked List | Set-2
- QuickSort on Doubly Linked List
- Reverse a Doubly Linked List
- Reverse a Doubly linked list using recursion
- Delete all occurrences of a given key in a doubly linked list
- Memory efficient doubly linked list
- Doubly Circular Linked List | Set 2 (Deletion)