  Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email  Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

# Iterative Selection Sort For Linked List In this blog, we tackle a coding task that involves iterative selection sort for linked list. A linked list is unidirectional i.e. we can only traverse the linked list in one direction. A selection sort is an effective sorting algorithm that is used in comparison operations. Let’s understand a brief explanation of the selection sort for linked list.

### Problem Statement

In this problem we are given the head of a linked list denoting the whole linked list, we have to apply the Iterative selection sort for linked list. Suppose we have a linked list represented as – Then the sorted linked list is given as – Selection sort on the Linked list works in a similar way as it does on arrays but here instead of accessing the elements directly, we will be dealing with the links. Most of the Linked List problems are based on the manipulation of the links.

### Approach & Algorithm of Iterative Selection Sort For Linked List

The idea is to Iterate over the given Linked list N times where N is the number of elements in the Linked list. In every iteration of the algorithm, the minimum element (considering ascending order) from the unsorted subarray is picked and moved to the sorted subarray of the Linked list.
Now we have to perform a similar swapping operation, which is done in the Linked list in 2 ways –

• By swapping the data parts of the nodes.
• By swapping the complete nodes.

We will be using here the second way to swap the nodes by manipulating the links. While we do the swapping of the link parts of two nodes, four cases arise:

• When nodes are adjacent and the first node is the head node.
• Nodes can be adjacent and the first node isn’t the head node.
• Nodes might not be adjacent and the first node is the head node.
• Nodes might not be adjacent and the first node isn’t the head node.

### Iterative Selection Sort For Linked List

```#include
using namespace std;
struct Node {
int data;
Node* next;
};
Node* newNode(int val)
{
Node* temp = new Node;
temp->data = val;
temp->next = NULL;
return temp;
}
{
Node *x, *y, *z, *w, *s;
while (y->next) {
z = w = y->next;
while (w) {
if (y->data > w->data) {
if (y->next == w) {
y->next = w->next;
w->next = y;
s = y;
y = w;
w = s;
z = w;
w = w->next;
}
else {
y->next = w->next;
w->next = y;
x->next = w;
s = y;
y = w;
w = s;
z = w;
w = w->next;
}
}
else {
s = y->next;
y->next = w->next;
w->next = s;
z->next = y;
s = y;
y = w;
w = s;
z = w;
w = w->next;
}
else {
s = y->next;
y->next = w->next;
w->next = s;
z->next = y;
x->next = w;
s = y;
y = w;
w = s;
z = w;
w = w->next;
}
}
}
else {
z = w;
w = w->next;
}
}

x = y;
y = y->next;
}

}
{
}
}
int main()
{
return 0;
}
```
```#include
#include

struct Node {
int data;
struct Node* next;
};
struct Node* newNode(int val)
{
struct Node* temp = (struct Node*)malloc(sizeof(struct Node));
temp->data = val;
temp->next = NULL;
return temp;
}
{
struct Node *x, *y, *z, *w, *s;
while (y->next) {
z = w = y->next;
while (w) {
if (y->data > w->data) {
if (y->next == w) {
y->next = w->next;
w->next = y;
s = y;
y = w;
w = s;
z = w;
w = w->next;
}
else {
y->next = w->next;
w->next = y;
x->next = w;
s = y;
y = w;
w = s;
z = w;
w = w->next;
}
}
else {
s = y->next;
y->next = w->next;
w->next = s;
z->next = y;
s = y;
y = w;
w = s;
z = w;
w = w->next;
}
else {
s = y->next;
y->next = w->next;
w->next = s;
z->next = y;
x->next = w;
s = y;
y = w;
w = s;
z = w;
w = w->next;
}
}
}
else {
z = w;
w = w->next;
}
}
x = y;
y = y->next;
}

}
{
}
}
int main()
{
return 0;
}

```
```
class IterativeSort
{
static class Node
{
int data;
Node next;
};
static Node newNode(int val)
{
Node temp = new Node();

temp.data = val;

temp.next = null;

return temp;
}

/* Function to sort a linked list using selection
sort algorithm by swapping the next pointers */
{
Node a, b, c, d, r;
// While b is not the last node
while (b.next != null)
{
c = d = b.next;
// While d is pointing to a valid node
while (d != null)
{
if (b.data > d.data)
{
// If d appears immediately after b
if (b.next == d)
{
{
b.next = d.next;
d.next = b;

// Swap b and d pointers
r = b;
b = d;
d = r;

c = d;

d = d.next;
}
// Case 2: b is not the head of the linked list
else
{
b.next = d.next;
d.next = b;
a.next = d;

// Swap b and d pointers
r = b;
b = d;
d = r;
c = d;

d = d.next;
}
}
/* If b and d have some non-zero number of nodes in between them */
else
{
{
// Swap b.next and d.next
r = b.next;
b.next = d.next;
d.next = r;
c.next = b;

// Swap b and d pointers
r = b;
b = d;
d = r;

c = d;

// as it is already in order
d = d.next;

}
// Case 4: b is not the head of the linked list
else
{
// Swap b.next and d.next
r = b.next;
b.next = d.next;
d.next = r;
c.next = b;
a.next = d;

// Swap b and d pointers
r = b;
b = d;
d = r;

c = d;

// as it is already in order
d = d.next;
}
}
}
else
{
c = d;
d = d.next;
}
}
a = b;
b = b.next;
}
}
{
}
}

// Driver Code
public static void main(String args[])
{

}
}

```
```class Node:

def __init__(self, val):
self.data = val
self.next = None

while b.next:

c = d = b.next

while d:

if b.data > d.data:

if b.next == d:

b.next = d.next
d.next = b
b, d = d, b
c = d
d = d.next

else:

b.next = d.next
d.next = b
a.next = d
b, d = d, b
c = d
d = d.next

else:

r = b.next
b.next = d.next
d.next = r
c.next = b
b, d = d, b
c = d
d = d.next

else:

r = b.next
b.next = d.next
d.next = r
c.next = b
a.next = d
b, d = d, b
c = d
d = d.next

else:

c = d
d = d.next

a = b
b = b.next

if __name__ == "__main__":

```

#### Output:

2->3->4->5

Time Complexity for selection sort for linked list : O(N2), where N is the number of nodes present in the Linked List.

## FAQs regarding selection sort for linked list

1. What is meant by selection sort?
2. Selection sort is an effective sorting algorithm that is based on comparison operations. Selection sort adds one element in every iteration and selects the smallest element and moves it to the beginning by swapping with the front element.

3. What is the time and space complexity of the selection sort?
4. The time complexity of the selection sort algorithm is O(N2) and the space complexity of O(1).

5. Which sorting is best for the linked list?
6. Merge sort is preferred for sorting a linked list.

7. Can we implement selection sort for linked list recursively?
8. Yes, we can implement selection sort for linked list recursively.