Python Program For Recursive Selection Sort For Singly Linked List – Swapping Node Links
Last Updated :
20 Jan, 2023
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.
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)
|
Output
Linked list before sorting:
10 12 8 4 6
Linked list after sorting:
4 6 8 10 12
Time Complexity: O(n2), since we are traversing the linked list using a while loop and inside it there is a recursive function is operating at O(n) time thus the complexity turns out to be quadratic
Auxiliary Space: O(n), since we are using recursion all elements in the linked List takes up the entire stack space
Please refer complete article on Recursive selection sort for singly linked list | Swapping node links for more details!
Share your thoughts in the comments
Please Login to comment...