Insertion Sort for Singly Linked List
We have discussed Insertion Sort for arrays. In this article, we are going to discuss Insertion Sort for a 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.
Recommended: Please try your approach on {IDE} first, before moving on to the solution
The main step is (2.a) which has been covered in the below post.
Sorted Insert for Singly Linked List
Below is the implementation of the above algorithm
C
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node* next;
};
struct node* head = NULL;
struct node* sorted = NULL;
void push( int val)
{
struct node* newnode
= ( struct node*) malloc ( sizeof ( struct node));
newnode->data = val;
newnode->next = head;
head = newnode;
}
void sortedInsert( struct node* newnode)
{
if (sorted == NULL || sorted->data >= newnode->data) {
newnode->next = sorted;
sorted = newnode;
}
else {
struct node* current = sorted;
while (current->next != NULL
&& current->next->data < newnode->data) {
current = current->next;
}
newnode->next = current->next;
current->next = newnode;
}
}
void insertionsort()
{
struct node* current = head;
while (current != NULL) {
struct node* next = current->next;
sortedInsert(current);
current = next;
}
head = sorted;
}
void printlist( struct node* head)
{
while (head != NULL) {
printf ( "%d->" , head->data);
head = head->next;
}
printf ( "NULL" );
}
int main()
{
push(5);
push(20);
push(4);
push(3);
push(30);
printf ( "Linked List before sorting:\n" );
printlist(head);
printf ( "\n" );
insertionsort(head);
printf ( "Linked List after sorting:\n" );
printlist(head);
}
|
C++
#include <bits/stdc++.h>
using namespace std;
struct Node {
int val;
struct Node* next;
Node( int x)
{
val = x;
next = NULL;
}
};
class LinkedlistIS {
public :
Node* head;
Node* sorted;
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) {
cout << head->val << " " ;
head = head->next;
}
}
};
int main()
{
LinkedlistIS list;
list.head = NULL;
list.push(5);
list.push(20);
list.push(4);
list.push(3);
list.push(30);
cout << "Linked List before sorting" << endl;
list.printlist(list.head);
cout << endl;
list.insertionSort(list.head);
cout << "Linked List After sorting" << endl;
list.printlist(list.head);
}
|
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( "\nLinkedList After sorting" );
list.printlist(list.head);
}
}
|
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 ( "\nLinked List after sorting " )
printList(a)
|
C#
using System;
public class LinkedlistIS
{
public node head;
public node sorted;
public class node
{
public int val;
public 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 )
{
Console.Write(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);
Console.WriteLine( "Linked List before Sorting.." );
list.printlist(list.head);
list.insertionSort(list.head);
Console.WriteLine( "\nLinkedList After sorting" );
list.printlist(list.head);
}
}
|
Javascript
<script>
var head = null ;
var sorted = null ;
class node {
constructor(val) {
this .val = val;
this .next = null ;
}
}
function push(val) {
var newnode = new node(val);
newnode.next = head;
head = newnode;
}
function insertionSort( headref) {
var sorted = null ;
var current = headref;
while (current != null ) {
var next = current.next;
sortedInsert(current);
current = next;
}
head = sorted;
}
function sortedInsert( newnode) {
if (sorted == null || sorted.val >= newnode.val) {
newnode.next = sorted;
sorted = newnode;
} else {
var current = sorted;
while (current.next != null && current.next.val < newnode.val) {
current = current.next;
}
newnode.next = current.next;
current.next = newnode;
}
}
function printlist( head) {
while (head != null ) {
document.write(head.val + " " );
head = head.next;
}
}
push(5);
push(20);
push(4);
push(3);
push(30);
document.write( "Linked List before Sorting..<br/>" );
printlist(head);
insertionSort(head);
document.write( "<br/>LinkedList After sorting<br/>" );
printlist(sorted);
</script>
|
Output:
Linked List before sorting
30 3 4 20 5
Linked List after sorting
3 4 5 20 30
Time and space complexity analysis:
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.
Thus Time Complexity: O(n)*O(n)=O(n^2)
Space Complexity: No extra space is required depending on the size of the input. Thus Space complexity is constant- O(1).
Last Updated :
10 Jan, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...