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:
Java
public class LinkedlistIS
{
node head;
node sorted;
class node
{
int val;
node next;
public node( int val)
{
this .val = val;
}
}
void push( int val)
{
node newnode = new node(val);
newnode.next = head;
head = newnode;
}
void insertionSort(node headref)
{
sorted = null ;
node current = headref;
while (current != null )
{
node next = current.next;
sortedInsert(current);
current = next;
}
head = sorted;
}
void sortedInsert(node newnode)
{
if (sorted == null ||
sorted.val >= newnode.val)
{
newnode.next = sorted;
sorted = newnode;
}
else
{
node current = sorted;
while (current.next != null &&
current.next.val < newnode.val)
{
current = current.next;
}
newnode.next = current.next;
current.next = newnode;
}
}
void printlist(node head)
{
while (head != null )
{
System.out.print(head.val + " " );
head = head.next;
}
}
public static void main(String[] args)
{
LinkedlistIS list = new LinkedlistIS();
list.push( 5 );
list.push( 20 );
list.push( 4 );
list.push( 3 );
list.push( 30 );
System.out.println(
"Linked List before Sorting.." );
list.printlist(list.head);
list.insertionSort(list.head);
System.out.println(
"LinkedList After sorting" );
list.printlist(list.head);
}
}
|
Javascript
class Node {
constructor(val) {
this .val = val;
this .next = null ;
}
}
class LinkedListIS {
constructor() {
this .head = null ;
this .sorted = null ;
}
push(val) {
const newNode = new Node(val);
newNode.next = this .head;
this .head = newNode;
}
insertionSort(headRef) {
this .sorted = null ;
let current = headRef;
while (current !== null ) {
const next = current.next;
this .sortedInsert(current);
current = next;
}
this .head = this .sorted;
}
sortedInsert(newNode) {
if ( this .sorted === null || this .sorted.val >= newNode.val) {
newNode.next = this .sorted;
this .sorted = newNode;
} else {
let current = this .sorted;
while (current.next !== null && current.next.val < newNode.val) {
current = current.next;
}
newNode.next = current.next;
current.next = newNode;
}
}
printList(head) {
while (head !== null ) {
console.log(head.val + " " );
head = head.next;
}
}
}
const list = new LinkedListIS();
list.push(5);
list.push(20);
list.push(4);
list.push(3);
list.push(30);
console.log( "Linked List before Sorting.." );
list.printList(list.head);
list.insertionSort(list.head);
console.log( "LinkedList After sorting" );
list.printList(list.head);
|
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.
Space Complexity: 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!