We have discussed Insertion Sort for arrays. In this article same for linked list is discussed.
Below is simple insertion sort algorithm for linked list.
1) Create an empty sorted (or result) list 2) Traverse the given list, do following for every node. ......a) Insert current node in sorted way in sorted or result 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 Singly Linked List
Below is implementation of above algorithm
Linked List before sorting 30 3 4 20 5 Linked List after sorting 3 4 5 20 30
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
- Rearrange a Linked List in Zig-Zag fashion
- Rearrange a given linked list in-place.
- Sort a linked list that is sorted alternating ascending and descending orders?
- Select a Random Node from a Singly Linked List
- Merge Sort for Doubly Linked List
- Point to next higher value node in a linked list with an arbitrary pointer
- Clone a linked list with next and random pointer | Set 2
- Can we reverse a linked list in less than O(n)?
- C Program for Bubble Sort on Linked List
- QuickSort on Singly Linked List
- Linked List | Set 2 (Inserting a node)
- Merge Sort for Linked Lists
- Given a linked list which is sorted, how will you insert in sorted way
- Clone a linked list with next and random pointer | Set 1
- Reverse a linked list