Recursive selection sort for singly linked list | Swapping node links - cook the code

Saturday 27 January 2018

Recursive selection sort for singly linked list | Swapping node links


Recursive selection sort for singly linked list | Swapping node links


Given a singly linked list containing n nodes. The problem is to sort the list using recursive selection sort technique. The approach should be such that it involves swapping node links instead of swapping nodes data.
Examples:
Input : 10 -> 12 -> 8 -> 4 -> 6
Output : 4 -> 6 -> 8 -> 10 -> 12
In Selection Sort, we first find minimum element, swap it with the beginning node and recur for remaining list. Below is recursive implementation of these steps for 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    
void swapNodes(struct Node** head_ref, struct Node* currX,
               struct Node* currY, struct Node* prevY)
{
    // make 'currY' as new head
    *head_ref = currY;
 
    // adjust links
    prevY->next = currX;
 
    // Swap next pointers
    struct Node* temp = currY->next;
    currY->next = currX->next;
    currX->next = temp;
}
 
// function to sort the linked list using
// recursive selection sort technique
struct Node* recurSelectionSort(struct Node* head)
{
    // if there is only a single node
    if (head->next == NULL)
        return head;
 
    // 'min' - pointer to store the node having
    // minimum data value
    struct Node* min = head;
 
    // 'beforeMin' - pointer to store node previous
    // to 'min' node
    struct Node* beforeMin = NULL;
    struct Node* ptr;
 
    // traverse the list till the last node
    for (ptr = head; ptr->next != NULL; ptr = ptr->next) {
 
        // if true, then update 'min' and 'beforeMin'
        if (ptr->next->data < min->data) {
            min = ptr->next;
            beforeMin = ptr;
        }
    }
 
    // if 'min' and 'head' are not same,
    // swap the head node with the 'min' node
    if (min != head)
        swapNodes(&head, head, min, beforeMin);
 
    // recursively sort the remaining list
    head->next = recurSelectionSort(head->next);
 
    return head;
}
 
// function to sort the given linked list
void sort(struct Node** head_ref)
{
    // if list is empty
    if ((*head_ref) == NULL)
        return;
 
    // sort the list using recursive selection
    // sort technique
    *head_ref = recurSelectionSort(*head_ref);
}

No comments:

Post a Comment