# 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
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

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)
Initialize beforeMin = NULL

while ptr->next != NULL
if min->data > ptr->next->data
min = ptr->next
beforeMin = ptr
ptr = ptr->next

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 is modified accordingly for the implementation of this post.

```// C++ implementation of recursive selection sort
#include <bits/stdc++.h>
using namespace std;

struct Node {
int data;
struct Node* next;
};

// function to swap nodes 'currX' and 'currY' in a
// linked list without swapping data
void swapNodes(struct Node** head_ref, struct Node* currX,
struct Node* currY, struct Node* prevY)
{
// make 'currY' as new head

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
{
// if there is only a single node

// 'min' - pointer to store the node having
// minimum data value

// '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

// recursively sort the remaining list

}

// function to sort the given linked list
{
// if list is empty
return;

// sort the list using recursive selection
// sort technique
}

// function to insert a node at the
// beginning of the linked list
void push(struct Node** head_ref, int new_data)
{
// allocate node
struct Node* new_node =
(struct Node*)malloc(sizeof(struct Node));

// put in the data
new_node->data = new_data;

// link the old list to the new node

// move the head to point to the new node
}

// function to print the linked list
{
cout << head->data << " ";
}
}

// Driver program to test above
int main()
{

cout << "Linked list before sorting:n";

cout << "\nLinked list after sorting:n";

return 0;
}
```

Output:

```Linked list before sorting:
10 12 8 4 6
4 6 8 10 12
```

Time Complexity: O(n2)

This article is contributed by Ayush Jauhari. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

# GATE CS Corner    Company Wise Coding Practice

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.
2 Average Difficulty : 2/5.0
Based on 1 vote(s)