We have discussed Insertion Sort for arrays. In this article we are going to discuss Insertion Sort for linked list.
Below is a simple insertion sort algorithm for a 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 the post Sorted Insert for Singly Linked List Below is implementation of above algorithm:
Python
class Node:
def __init__( self , data):
self .data = data
self . next = None
def insertionSort(head_ref):
sorted = None
current = head_ref
while (current ! = None ):
next = current. next
sorted = sortedInsert( sorted ,
current)
current = next
head_ref = sorted
return head_ref
def sortedInsert(head_ref, new_node):
current = None
if (head_ref = = None or
(head_ref).data > = new_node.data):
new_node. next = head_ref
head_ref = new_node
else :
current = head_ref
while (current. next ! = None and
current. next .data < new_node.data):
current = current. next
new_node. next = current. next
current. next = new_node
return head_ref
def printList(head):
temp = head
while (temp ! = None ):
print ( temp.data, end = " " )
temp = temp. next
def push( head_ref, new_data):
new_node = Node( 0 )
new_node.data = new_data
new_node. next = (head_ref)
(head_ref) = new_node
return head_ref
a = None
a = push(a, 5 )
a = push(a, 20 )
a = push(a, 4 )
a = push(a, 3 )
a = push(a, 30 )
print ( "Linked List before sorting " )
printList(a)
a = insertionSort(a)
print ( "Linked List after sorting " )
printList(a)
|
Java
class Node {
int data;
Node next;
public Node( int data)
{
this .data = data;
this .next = null ;
}
}
public class InsertionSortLinkedList {
public static Node insertionSort(Node head)
{
Node sorted = null ;
Node current = head;
while (current != null ) {
Node next = current.next;
sorted = sortedInsert(sorted, current);
current = next;
}
return sorted;
}
public static Node sortedInsert(Node head,
Node new_node)
{
Node current = null ;
if (head == null || head.data >= new_node.data) {
new_node.next = head;
head = new_node;
}
else {
current = head;
while (current.next != null
&& current.next.data < new_node.data) {
current = current.next;
}
new_node.next = current.next;
current.next = new_node;
}
return head;
}
public static void printList(Node head)
{
Node temp = head;
while (temp != null ) {
System.out.print(temp.data + " " );
temp = temp.next;
}
}
public static Node push(Node head, int new_data)
{
Node new_node = new Node(new_data);
new_node.next = head;
head = new_node;
return head;
}
public static void main(String[] args)
{
Node a = null ;
a = push(a, 5 );
a = push(a, 20 );
a = push(a, 4 );
a = push(a, 3 );
a = push(a, 30 );
System.out.println( "Linked List before sorting " );
printList(a);
a = insertionSort(a);
System.out.println( "\nLinked List after sorting " );
printList(a);
}
}
|
Output:
Linked List before sorting
30 3 4 20 5
Linked List after sorting
3 4 5 20 30
Time Complexity: O(n2), in the worst case, we might have to traverse all nodes of the sorted list for inserting a node, and there are “n” such nodes.
Auxiliary Space : O(1), no extra space is required depending on the size of the input, thus it is constant.
Please refer complete article on Insertion Sort for Singly Linked List for more details!
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!