Recursive selection sort for singly linked list | Swapping node links
Last Updated :
14 Jul, 2022
Given a singly linked list containing n nodes. The problem is to sort the list using the recursive selection sort technique. The approach should be such that it involves swapping node links instead of swapping node data.
Examples:
Input : 10 -> 12 -> 8 -> 4 -> 6
Output : 4 -> 6 -> 8 -> 10 -> 12
In Selection Sort, we first find the minimum element, swap it with the beginning node and recur for the remaining list. Below is the recursive implementation of these steps for the linked list.
recurSelectionSort(head)
if head->next == NULL
return head
Initialize min = head
Initialize beforeMin = NULL
Initialize ptr = head
while ptr->next != NULL
if min->data > ptr->next->data
min = ptr->next
beforeMin = ptr
ptr = ptr->next
if min != head
swapNodes(&head, head, min, beforeMin)
head->next = recurSelectionSort(head->next)
return head
swapNodes(head_ref, currX, currY, prevY)
head_ref = currY
prevY->next = currX
Initialize temp = currY->next
currY->next = currX->next
currX->next = temp
The swapNodes(head_ref, currX, currY, prevY) is based on the approach discussed here but it has been modified accordingly for the implementation of this post.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
struct Node* next;
};
void swapNodes( struct Node** head_ref, struct Node* currX,
struct Node* currY, struct Node* prevY)
{
*head_ref = currY;
prevY->next = currX;
struct Node* temp = currY->next;
currY->next = currX->next;
currX->next = temp;
}
struct Node* recurSelectionSort( struct Node* head)
{
if (head->next == NULL)
return head;
struct Node* min = head;
struct Node* beforeMin = NULL;
struct Node* ptr;
for (ptr = head; ptr->next != NULL; ptr = ptr->next) {
if (ptr->next->data < min->data) {
min = ptr->next;
beforeMin = ptr;
}
}
if (min != head)
swapNodes(&head, head, min, beforeMin);
head->next = recurSelectionSort(head->next);
return head;
}
void sort( struct Node** head_ref)
{
if ((*head_ref) == NULL)
return ;
*head_ref = recurSelectionSort(*head_ref);
}
void push( struct Node** head_ref, int new_data)
{
struct Node* new_node =
( struct Node*) malloc ( sizeof ( struct Node));
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
void printList( struct Node* head)
{
while (head != NULL) {
cout << head->data << " " ;
head = head->next;
}
}
int main()
{
struct Node* head = NULL;
push(&head, 6);
push(&head, 4);
push(&head, 8);
push(&head, 12);
push(&head, 10);
cout << "Linked list before sorting:n" ;
printList(head);
sort(&head);
cout << "\nLinked list after sorting:n" ;
printList(head);
return 0;
}
|
Java
class GFG
{
static class Node
{
int data;
Node next;
};
static Node swapNodes( Node head_ref, Node currX,
Node currY, Node prevY)
{
head_ref = currY;
prevY.next = currX;
Node temp = currY.next;
currY.next = currX.next;
currX.next = temp;
return head_ref;
}
static Node recurSelectionSort( Node head)
{
if (head.next == null )
return head;
Node min = head;
Node beforeMin = null ;
Node ptr;
for (ptr = head; ptr.next != null ; ptr = ptr.next)
{
if (ptr.next.data < min.data)
{
min = ptr.next;
beforeMin = ptr;
}
}
if (min != head)
head = swapNodes(head, head, min, beforeMin);
head.next = recurSelectionSort(head.next);
return head;
}
static Node sort( Node head_ref)
{
if ((head_ref) == null )
return null ;
head_ref = recurSelectionSort(head_ref);
return head_ref;
}
static Node push( Node head_ref, int new_data)
{
Node new_node = new Node();
new_node.data = new_data;
new_node.next = (head_ref);
(head_ref) = new_node;
return head_ref;
}
static void printList( Node head)
{
while (head != null )
{
System.out.print( head.data + " " );
head = head.next;
}
}
public static void main(String args[])
{
Node head = null ;
head = push(head, 6 );
head = push(head, 4 );
head = push(head, 8 );
head = push(head, 12 );
head = push(head, 10 );
System.out.println( "Linked list before sorting:" );
printList(head);
head = sort(head);
System.out.print( "\nLinked list after sorting:" );
printList(head);
}
}
|
Python
class Node:
def __init__( self , data):
self .data = data
self . next = None
def swapNodes(head_ref, currX, currY, prevY) :
head_ref = currY
prevY. next = currX
temp = currY. next
currY. next = currX. next
currX. next = temp
return head_ref
def recurSelectionSort( head) :
if (head. next = = None ) :
return head
min = head
beforeMin = None
ptr = head
while ( ptr. next ! = None ) :
if (ptr. next .data < min .data) :
min = ptr. next
beforeMin = ptr
ptr = ptr. next
if ( min ! = head) :
head = swapNodes(head, head, min , beforeMin)
head. next = recurSelectionSort(head. next )
return head
def sort( head_ref) :
if ((head_ref) = = None ) :
return None
head_ref = recurSelectionSort(head_ref)
return head_ref
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
def printList( head) :
while (head ! = None ) :
print ( head.data ,end = " " )
head = head. next
head = None
head = push(head, 6 )
head = push(head, 4 )
head = push(head, 8 )
head = push(head, 12 )
head = push(head, 10 )
print ( "Linked list before sorting:" )
printList(head)
head = sort(head)
print ( "\nLinked list after sorting:" )
printList(head)
|
C#
using System;
public class GFG
{
public class Node
{
public int data;
public Node next;
};
static Node swapNodes(Node head_ref, Node currX,
Node currY, Node prevY)
{
head_ref = currY;
prevY.next = currX;
Node temp = currY.next;
currY.next = currX.next;
currX.next = temp;
return head_ref;
}
static Node recurSelectionSort(Node head)
{
if (head.next == null )
return head;
Node min = head;
Node beforeMin = null ;
Node ptr;
for (ptr = head; ptr.next != null ;
ptr = ptr.next)
{
if (ptr.next.data < min.data)
{
min = ptr.next;
beforeMin = ptr;
}
}
if (min != head)
head = swapNodes(head, head, min, beforeMin);
head.next = recurSelectionSort(head.next);
return head;
}
static Node sort( Node head_ref)
{
if ((head_ref) == null )
return null ;
head_ref = recurSelectionSort(head_ref);
return head_ref;
}
static Node push(Node head_ref, int new_data)
{
Node new_node = new Node();
new_node.data = new_data;
new_node.next = (head_ref);
(head_ref) = new_node;
return head_ref;
}
static void printList( Node head)
{
while (head != null )
{
Console.Write(head.data + " " );
head = head.next;
}
}
public static void Main(String []args)
{
Node head = null ;
head = push(head, 6);
head = push(head, 4);
head = push(head, 8);
head = push(head, 12);
head = push(head, 10);
Console.WriteLine( "Linked list before sorting:" );
printList(head);
head = sort(head);
Console.Write( "\nLinked list after sorting:" );
printList(head);
}
}
|
Javascript
<script>
class Node {
constructor(val) {
this .data = val;
this .next = null ;
}
}
function swapNodes(head_ref, currX, currY, prevY) {
head_ref = currY;
prevY.next = currX;
var temp = currY.next;
currY.next = currX.next;
currX.next = temp;
return head_ref;
}
function recurSelectionSort(head) {
if (head.next == null )
return head;
var min = head;
var beforeMin = null ;
var ptr;
for (ptr = head; ptr.next != null ; ptr = ptr.next) {
if (ptr.next.data < min.data) {
min = ptr.next;
beforeMin = ptr;
}
}
if (min != head)
head = swapNodes(head, head, min, beforeMin);
head.next = recurSelectionSort(head.next);
return head;
}
function sort(head_ref) {
if ((head_ref) == null )
return null ;
head_ref = recurSelectionSort(head_ref);
return head_ref;
}
function push(head_ref , new_data) {
var new_node = new Node();
new_node.data = new_data;
new_node.next = (head_ref);
(head_ref) = new_node;
return head_ref;
}
function printList(head) {
while (head != null ) {
document.write(head.data + " " );
head = head.next;
}
}
var head = null ;
head = push(head, 6);
head = push(head, 4);
head = push(head, 8);
head = push(head, 12);
head = push(head, 10);
document.write( "Linked list before sorting:<br/>" );
printList(head);
head = sort(head);
document.write( "<br/>Linked list after sorting:<br/>" );
printList(head);
</script>
|
Output
Linked list before sorting:n10 12 8 4 6
Linked list after sorting:n4 6 8 10 12
Time Complexity: O(n2)
Auxiliary Space: O(n)
Share your thoughts in the comments
Please Login to comment...