Python Program For Recursive Selection Sort For Singly Linked List - Swapping Node Links - GeeksforGeeks

30 Mar, 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. 
Python
# Python implementation of recursive# selection sort for singly linked# list | Swapping node links # Linked List nodeclass Node:    def __init__(self, data):        self.data = data        self.next = None # Function to swap nodes 'currX' and# 'currY' in a linked list without# swapping datadef swapNodes(head_ref, currX,              currY, prevY):     # Make 'currY' as new head    head_ref = currY     # Adjust links    prevY.next = currX     # Swap next pointers    temp = currY.next    currY.next = currX.next    currX.next = temp    return head_ref # Function to sort the linked list using# recursive selection sort techniquedef recurSelectionSort(head):     # If there is only a single node    if (head.next == None)8:        return head     # 'min' - pointer to store the node    # having minimum data value    min = head     # 'beforeMin' - pointer to store node    # previous to 'min' node    beforeMin = None    ptr = head         # Traverse the list till the last node    while (ptr.next != None):             # if true, then update 'min' and        # 'beforeMin'        if (ptr.next.data < min.data) :                     min = ptr.next            beforeMin = ptr                     ptr = ptr.next         # if 'min' and 'head' are not same,    # swap the head node with the 'min' node    if (min != head):        head = swapNodes(head, head,                         min, beforeMin)     # Recursively sort the remaining list    head.next =         recurSelectionSort(head.next)     return head # Function to sort the given linked listdef sort(head_ref):     # If list is empty    if ((head_ref) == None):        return None     # Sort the list using recursive    # selection sort technique    head_ref = recurSelectionSort(head_ref)    return head_ref # Function to insert a node at the# beginning of the linked listdef push(head_ref, new_data):     # Allocate node    new_node = Node(0)     # Put in the data    new_node.data = new_data     # Link the old list to the    # new node    new_node.next = (head_ref)     # Move the head to point to the    # new node    (head_ref) = new_node    return head_ref # Function to print the linked listdef printList(head):     while (head != None):           print(head.data , end = " ")        head = head.next     # Driver codehead = None # Create linked list 10.12.8.4.6head = 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) # sort the linked listhead = sort(head) print("Linked list after sorting:")printList(head)# This code is contributed by Arnab Kundu
Output: 
Linked list before sorting:
10 12 8 4 6
Linked list after sorting:
4 6 8 10 12
Time Complexity: O(n2)
Auxiliary Space: O(n)
Please refer complete article on Recursive selection sort for singly linked list | Swapping node links for more details!
rohan07
Linked-List-Sorting
Linked List
Python Programs
Sorting
Linked List
Sorting
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Circular Linked List | Set 2 (Traversal)
Swap nodes in a linked list without swapping data
Circular Singly Linked List | Insertion
Given a linked list which is sorted, how will you insert in sorted way
Real-time application of Data Structures
Python program to convert a list to string
Defaultdict in Python
Python | Get dictionary keys as a list
Python | Split string into list of characters
Python | Convert a list to dictionary