Iterative selection sort for linked list
Last Updated :
11 Jan, 2023
Given a linked list, the task is to sort the linked list in ascending order by using selection sort.
Examples:
Input : 1->4->2->2->3
Output : 1->2->2->3->4
Input : 5->4->3->2
Output : 2->3->4->5
Selection Sort Algorithm: Iterate the given list N times where N is the number of elements in the list. In every iteration of selection sort, the minimum element (considering ascending order) from the unsorted subarray is picked and moved to the sorted subarray.
Example:
list = 64 25 12 22 11
// Find the minimum element in list(0...4)
// and place it at beginning
11 25 12 22 64
// Find the minimum element in list(1...4)
// and place it at beginning of list(1...4)
11 12 25 22 64
// Find the minimum element in list(2...4)
// and place it at beginning of list(2...4)
11 12 22 25 64
// Find the minimum element in list(3...4)
// and place it at beginning of list(3...4)
11 12 22 25 64
The swapping required can be done in two ways:
- By swapping the data parts of the nodes.
- By swapping the complete nodes.
Second implementation is generally used when elements of the list are some kind of records because in such a case data swapping becomes tedious and expensive due to the presence of a large number of data elements.
Implementation Method 1: Below is the implementation of selection sort function for sorting linked lists by swapping only data parts of a node.
C++
void selectionSort(node* head)
{
node* temp = head;
while (temp) {
node* min = temp;
node* r = temp->next;
while (r) {
if (min->data > r->data)
min = r;
r = r->next;
}
int x = temp->data;
temp->data = min->data;
min->data = x;
temp = temp->next;
}
}
|
Java
void selectionSort(node head)
{
node temp = head;
while (temp) {
node min = temp;
node r = temp.next;
while (r) {
if (min.data > r.data)
min = r;
r = r.next;
}
int x = temp.data;
temp.data = min.data;
min.data = x;
temp = temp.next;
}
}
|
C#
static void selectionSort(node head)
{
node temp = head;
while (temp) {
node min = temp;
node r = temp.next;
while (r) {
if (min.data > r.data)
min = r;
r = r.next;
}
int x = temp.data;
temp.data = min.data;
min.data = x;
temp = temp.next;
}
}
|
Javascript
<script>
function selectionSort(head)
{
var temp = head;
while (temp) {
var min = temp;
var r = temp.next;
while (r) {
if (min.data > r.data)
min = r;
r = r.next;
}
var x = temp.data;
temp.data = min.data;
min.data = x;
temp = temp.next;
}
}
</script>
|
Python3
def selectionSort(head):
temp = head
while (temp):
minn = temp
r = temp. next
while (r):
if (minn.data > r.data):
minn = r
r = r. next
x = temp.data
temp.data = minn.data
minn.data = x
temp = temp. next
|
Method 2: Data swapping is no doubt easier to implement and understand, but in some cases( as one mentioned above ), it isn’t desirable. While doing swapping of the next parts of two nodes, four cases are needed to be taken into consideration :
- Nodes are adjacent and the first node is the starting node.
- Nodes are adjacent and the first node isn’t the starting node.
- Nodes aren’t adjacent and the first node is the starting node.
- Nodes aren’t adjacent and the first node isn’t the starting node.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
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* selectionSort(Node* head)
{
Node *a, *b, *c, *d, *r;
a = b = head;
while (b->next) {
c = d = b->next;
while (d) {
if (b->data > d->data) {
if (b->next == d) {
if (b == head) {
b->next = d->next;
d->next = b;
r = b;
b = d;
d = r;
c = d;
head = b;
d = d->next;
}
else {
b->next = d->next;
d->next = b;
a->next = d;
r = b;
b = d;
d = r;
c = d;
d = d->next;
}
}
else {
if (b == head) {
r = b->next;
b->next = d->next;
d->next = r;
c->next = b;
r = b;
b = d;
d = r;
c = d;
d = d->next;
head = b;
}
else {
r = b->next;
b->next = d->next;
d->next = r;
c->next = b;
a->next = d;
r = b;
b = d;
d = r;
c = d;
d = d->next;
}
}
}
else {
c = d;
d = d->next;
}
}
a = b;
b = b->next;
}
return head;
}
void printList(Node* head)
{
while (head) {
cout << head->data << " " ;
head = head->next;
}
}
int main()
{
Node* head = newNode(5);
head->next = newNode(4);
head->next->next = newNode(3);
head = selectionSort(head);
printList(head);
return 0;
}
|
Java
class GFG {
static class Node {
int data;
Node next;
};
static Node newNode( int val)
{
Node temp = new Node();
temp.data = val;
temp.next = null ;
return temp;
}
static Node selectionSort(Node head)
{
Node a, b, c, d, r;
a = b = head;
while (b.next != null ) {
c = d = b.next;
while (d != null ) {
if (b.data > d.data) {
if (b.next == d) {
if (b == head) {
b.next = d.next;
d.next = b;
r = b;
b = d;
d = r;
c = d;
head = b;
d = d.next;
}
else {
b.next = d.next;
d.next = b;
a.next = d;
r = b;
b = d;
d = r;
c = d;
d = d.next;
}
}
else {
if (b == head) {
r = b.next;
b.next = d.next;
d.next = r;
c.next = b;
r = b;
b = d;
d = r;
c = d;
d = d.next;
head = b;
}
else {
r = b.next;
b.next = d.next;
d.next = r;
c.next = b;
a.next = d;
r = b;
b = d;
d = r;
c = d;
d = d.next;
}
}
}
else {
c = d;
d = d.next;
}
}
a = b;
b = b.next;
}
return head;
}
static void printList(Node head)
{
while (head != null ) {
System.out.print(head.data + " " );
head = head.next;
}
}
public static void main(String args[])
{
Node head = newNode( 5 );
head.next = newNode( 4 );
head.next.next = newNode( 3 );
head = selectionSort(head);
printList(head);
}
}
|
Python3
class Node:
def __init__( self , val):
self .data = val
self . next = None
def selectionSort(head):
a = b = head
while b. next :
c = d = b. next
while d:
if b.data > d.data:
if b. next = = d:
if b = = head:
b. next = d. next
d. next = b
b, d = d, b
c = d
head = b
d = d. next
else :
b. next = d. next
d. next = b
a. next = d
b, d = d, b
c = d
d = d. next
else :
if b = = head:
r = b. next
b. next = d. next
d. next = r
c. next = b
b, d = d, b
c = d
d = d. next
head = b
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
return head
def printList(head):
while head:
print (head.data, end = " " )
head = head. next
if __name__ = = "__main__" :
head = Node( 5 )
head. next = Node( 4 )
head. next . next = Node( 3 )
head = selectionSort(head)
printList(head)
|
C#
using System;
class GFG {
public class Node {
public int data;
public Node next;
};
static Node newNode( int val)
{
Node temp = new Node();
temp.data = val;
temp.next = null ;
return temp;
}
static Node selectionSort(Node head)
{
Node a, b, c, d, r;
a = b = head;
while (b.next != null ) {
c = d = b.next;
while (d != null ) {
if (b.data > d.data) {
if (b.next == d) {
if (b == head) {
b.next = d.next;
d.next = b;
r = b;
b = d;
d = r;
c = d;
head = b;
d = d.next;
}
else {
b.next = d.next;
d.next = b;
a.next = d;
r = b;
b = d;
d = r;
c = d;
d = d.next;
}
}
else {
if (b == head) {
r = b.next;
b.next = d.next;
d.next = r;
c.next = b;
r = b;
b = d;
d = r;
c = d;
d = d.next;
head = b;
}
else {
r = b.next;
b.next = d.next;
d.next = r;
c.next = b;
a.next = d;
r = b;
b = d;
d = r;
c = d;
d = d.next;
}
}
}
else {
c = d;
d = d.next;
}
}
a = b;
b = b.next;
}
return head;
}
static void printList(Node head)
{
while (head != null ) {
Console.Write(head.data + " " );
head = head.next;
}
}
public static void Main(String[] arg)
{
Node head = newNode(5);
head.next = newNode(4);
head.next.next = newNode(3);
head = selectionSort(head);
printList(head);
}
}
|
Javascript
<script>
class Node {
constructor() {
this .data = 0;
this .next = null ;
}
}
function newNode( val)
{
var temp = new Node();
temp.data = val;
temp.next = null ;
return temp;
}
function selectionSort( head)
{
var a, b, c, d, r;
a = b = head;
while (b.next != null ) {
c = d = b.next;
while (d != null ) {
if (b.data > d.data) {
if (b.next == d) {
if (b == head) {
b.next = d.next;
d.next = b;
r = b;
b = d;
d = r;
c = d;
head = b;
d = d.next;
}
else {
b.next = d.next;
d.next = b;
a.next = d;
r = b;
b = d;
d = r;
c = d;
d = d.next;
}
}
else {
if (b == head) {
r = b.next;
b.next = d.next;
d.next = r;
c.next = b;
r = b;
b = d;
d = r;
c = d;
d = d.next;
head = b;
}
else {
r = b.next;
b.next = d.next;
d.next = r;
c.next = b;
a.next = d;
r = b;
b = d;
d = r;
c = d;
d = d.next;
}
}
}
else {
c = d;
d = d.next;
}
}
a = b;
b = b.next;
}
return head;
}
function printList( head)
{
while (head != null ) {
document.write(head.data + " " );
head = head.next;
}
}
var head = newNode(5);
head.next = newNode(4);
head.next.next = newNode(3);
head = selectionSort(head);
printList(head);
</script>
|
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...