Selection sort using linked list

Here given code implementation process.

// C Program 
// Selection sort using linked list
#include <stdio.h>
#include <stdlib.h> //for malloc function

// Linked List LinkNode
struct LinkNode
{
    int data;
    struct LinkNode *next;
};

// Singly linked list 
struct SingleLL
{
    struct LinkNode *head;
    struct LinkNode *tail;
};
// Returns the new linked list
struct SingleLL *newLinkedList()
{
    // Create memory of head and tail Nodes
    struct SingleLL *sll = (struct SingleLL *) malloc(sizeof(struct SingleLL));
    if (sll == NULL)
    {
        printf("Memory overflow\n");
    }
    else
    {
        sll->head = NULL;
    }
    return sll;
}
// Returns a new Node of linked list
struct LinkNode *createLinkNode(int data)
{
    // Create dynamic node
    struct LinkNode *node = (struct LinkNode *) malloc(sizeof(struct LinkNode));
    if (node == NULL)
    {
        printf("Memory overflow\n");
    }
    else
    {
        // Set initial node value
        node->data = data;
        node->next = NULL;
    }
    return node;
}
// Add new Node at end of linked list 
void addNode(struct SingleLL *sll, int data)
{
    struct LinkNode *node = createLinkNode(data);
    if (sll->head == NULL)
    {
        sll->head = node;
    }
    else
    {
        struct LinkNode *temp = sll->head;
        //Find last node
        while (temp->next != NULL)
        {
            temp = temp->next;
        }
        // Append the node at last position
        temp->next = node;
    }
}
// Display linked list element
void display(struct SingleLL *sll)
{
    if (sll->head == NULL)
    {
        printf("\n Empty linked list\n");
        return;
    }
    struct LinkNode *temp = sll->head;
    // iterating linked list elements
    while (temp != NULL)
    {
        if (temp != sll->head)
        {
            printf(" →");
        }
        printf(" %d", temp->data);
        // Visit to next node
        temp = temp->next;
    }
    printf(" → NULL\n");
}

// Swap the linked list node value
void swapData(struct LinkNode *a, struct LinkNode *b)
{
    int temp = a->data;
    a->data = b->data;
    b->data = temp;
}

// Sort the linked list using selection sort
void selectionSort(struct SingleLL *sll)
{

    struct LinkNode*auxiliary = sll->head;
    struct LinkNode*temp = NULL;
    struct LinkNode*node = NULL;

    // Execute linked list node
    while(auxiliary != NULL)
    {
        node = auxiliary;
        temp = auxiliary->next;
        
        // Find the minimum node
        while(temp != NULL)
        {
            if(node->data > temp->data)
            {
                node = temp;
            }
            // Visit to next node
            temp = temp->next;
        }
        if(auxiliary->data > node->data)
        {
            // Transfer minimum value to initial position
            // Swap the node value
            swapData(auxiliary,node);
        }
        // Visit to next node
        auxiliary = auxiliary->next;
    }
}
int main()
{
    // Create a empty linked list
    struct SingleLL *sll = newLinkedList();
 
    //  Constructed linked list
    //  2 → 5 → 0 → 8 → 2 → 4 → 9 → 7 → 0 → 1 → NULL
    addNode(sll, 2);
    addNode(sll, 5);
    addNode(sll, 0);
    addNode(sll, 8);
    addNode(sll, 2);
    addNode(sll, 4);
    addNode(sll, 9);
    addNode(sll, 7);
    addNode(sll, 0);
    addNode(sll, 1);

    printf("\n Before sort \n");
    display(sll);
    // Sort linked list
    selectionSort(sll);
    //  0 → 0 → 1 → 2 → 2 → 4 → 5 → 7 → 8 → 9 → NULL
    printf("\n After sort \n");
    display(sll);
    return 0;
}

Output

 Before sort
 2 → 5 → 0 → 8 → 2 → 4 → 9 → 7 → 0 → 1 → NULL

 After sort
 0 → 0 → 1 → 2 → 2 → 4 → 5 → 7 → 8 → 9 → NULL
/*
  Java Program for
  Selection sort using linked list
*/

// Linked list node
class LinkNode
{
    public int data;
    public LinkNode next;
    public LinkNode(int data)
    {
        this.data = data;
        this.next = null;
    }
};
public class SingleLL
{
    public LinkNode head;

    public SingleLL()
    {
        this.head = null;
    }
    //Add new Node at end of linked list 
    public void addNode(int data)
    {
        LinkNode node = new LinkNode(data);
        if (this.head == null)
        {
            this.head = node;
        }
        else
        {
            LinkNode temp = this.head;
            //Find last node
            while (temp.next != null)
            {
                temp = temp.next;
            }
            // Append the node at last position
            temp.next = node;
        }
    }
    // Display linked list element
    public void display()
    {
        if (this.head == null)
        {
            System.out.print("\n Empty linked list\n");
            return;
        }
        LinkNode temp = this.head;
        //iterating linked list elements
        while (temp != null)
        {
            if (temp != this.head)
            {
                System.out.print(" →");
            }
            System.out.print(" " + temp.data);
            // Visit to next node
            temp = temp.next;
        }
        System.out.print(" → NULL\n");
    }
    // Swap the linked list node value
    public void swapData(LinkNode a, LinkNode b)
    {
        int temp = a.data;
        a.data = b.data;
        b.data = temp;
    }
    // Sort the linked list using selection sort
    public void selectionSort()
    {
        LinkNode auxiliary = this.head;
        LinkNode temp = null;
        LinkNode node = null;
        // Execute linked list node
        while (auxiliary != null)
        {
            node = auxiliary;
            temp = auxiliary.next;
            // Find the minimum node
            while (temp != null)
            {
                if (node.data > temp.data)
                {
                    node = temp;
                }
                // Visit to next node
                temp = temp.next;
            }
            if (auxiliary.data > node.data)
            {
                // Transfer minimum value to initial position
                // Swap the node value
                swapData(auxiliary, node);
            }
            // Visit to next node
            auxiliary = auxiliary.next;
        }
    }
    public static void main(String[] args)
    {
       
        SingleLL sll = new SingleLL();

        //  Constructed linked list
        //  2 → 5 → 0 → 8 → 2 → 4 → 9 → 7 → 0 → 1 → NULL
        sll.addNode(2);
        sll.addNode(5);
        sll.addNode(0);
        sll.addNode(8);
        sll.addNode(2);
        sll.addNode(4);
        sll.addNode(9);
        sll.addNode(7);
        sll.addNode(0);
        sll.addNode(1);
        System.out.print("\n Before sort \n");
        sll.display();
        // Sort linked list
        sll.selectionSort();
        //  0 → 0 → 1 → 2 → 2 → 4 → 5 → 7 → 8 → 9 → NULL
        System.out.print("\n After sort \n");
        sll.display();
    }
}

Output

 Before sort
 2 → 5 → 0 → 8 → 2 → 4 → 9 → 7 → 0 → 1 → NULL

 After sort
 0 → 0 → 1 → 2 → 2 → 4 → 5 → 7 → 8 → 9 → NULL
// Include header file
#include <iostream>
using namespace std;

/*
  C++ Program for
  Selection sort using linked list
*/

// Linked list node
class LinkNode
{
	public: int data;
	LinkNode *next;
	LinkNode(int data)
	{
		this->data = data;
		this->next = NULL;
	}
};;
class SingleLL
{
	public: LinkNode *head;
	SingleLL()
	{
		this->head = NULL;
	}
	//Add new Node at end of linked list
	void addNode(int data)
	{
		LinkNode *node = new LinkNode(data);
		if (this->head == NULL)
		{
			this->head = node;
		}
		else
		{
			LinkNode *temp = this->head;
			//Find last node
			while (temp->next != NULL)
			{
				temp = temp->next;
			}
			// Append the node at last position
			temp->next = node;
		}
	}
	// Display linked list element
	void display()
	{
		if (this->head == NULL)
		{
			cout << "\n Empty linked list\n";
			return;
		}
		LinkNode *temp = this->head;
		//iterating linked list elements
		while (temp != NULL)
		{
			if (temp != this->head)
			{
				cout << " →";
			}
			cout << " " << temp->data;
			// Visit to next node
			temp = temp->next;
		}
		cout << " → NULL\n";
	}
	// Swap the linked list node value
	void swapData(LinkNode *a, LinkNode *b)
	{
		int temp = a->data;
		a->data = b->data;
		b->data = temp;
	}
	// Sort the linked list using selection sort
	void selectionSort()
	{
		LinkNode *auxiliary = this->head;
		LinkNode *temp = NULL;
		LinkNode *node = NULL;
		// Execute linked list node
		while (auxiliary != NULL)
		{
			node = auxiliary;
			temp = auxiliary->next;
			// Find the minimum node
			while (temp != NULL)
			{
				if (node->data > temp->data)
				{
					node = temp;
				}
				// Visit to next node
				temp = temp->next;
			}
			if (auxiliary->data > node->data)
			{
				// Transfer minimum value to initial position
				// Swap the node value
				this->swapData(auxiliary, node);
			}
			// Visit to next node
			auxiliary = auxiliary->next;
		}
	}
};
int main()
{
	SingleLL sll = SingleLL();
	//  Constructed linked list
	//  2 → 5 → 0 → 8 → 2 → 4 → 9 → 7 → 0 → 1 → NULL
	sll.addNode(2);
	sll.addNode(5);
	sll.addNode(0);
	sll.addNode(8);
	sll.addNode(2);
	sll.addNode(4);
	sll.addNode(9);
	sll.addNode(7);
	sll.addNode(0);
	sll.addNode(1);
	cout << "\n Before sort \n";
	sll.display();
	// Sort linked list
	sll.selectionSort();
	//  0 → 0 → 1 → 2 → 2 → 4 → 5 → 7 → 8 → 9 → NULL
	cout << "\n After sort \n";
	sll.display();
	return 0;
}

Output

 Before sort
 2 → 5 → 0 → 8 → 2 → 4 → 9 → 7 → 0 → 1 → NULL

 After sort
 0 → 0 → 1 → 2 → 2 → 4 → 5 → 7 → 8 → 9 → NULL
// Include namespace system
using System;
/*
  C# Program for
  Selection sort using linked list
*/
// Linked list node
public class LinkNode
{
	public int data;
	public LinkNode next;
	public LinkNode(int data)
	{
		this.data = data;
		this.next = null;
	}
};
public class SingleLL
{
	public LinkNode head;
	public SingleLL()
	{
		this.head = null;
	}
	//Add new Node at end of linked list
	public void addNode(int data)
	{
		LinkNode node = new LinkNode(data);
		if (this.head == null)
		{
			this.head = node;
		}
		else
		{
			LinkNode temp = this.head;
			//Find last node
			while (temp.next != null)
			{
				temp = temp.next;
			}
			// Append the node at last position
			temp.next = node;
		}
	}
	// Display linked list element
	public void display()
	{
		if (this.head == null)
		{
			Console.Write("\n Empty linked list\n");
			return;
		}
		LinkNode temp = this.head;
		//iterating linked list elements
		while (temp != null)
		{
			if (temp != this.head)
			{
				Console.Write(" →");
			}
			Console.Write(" " + temp.data);
			// Visit to next node
			temp = temp.next;
		}
		Console.Write(" → NULL\n");
	}
	// Swap the linked list node value
	public void swapData(LinkNode a, LinkNode b)
	{
		int temp = a.data;
		a.data = b.data;
		b.data = temp;
	}
	// Sort the linked list using selection sort
	public void selectionSort()
	{
		LinkNode auxiliary = this.head;
		LinkNode temp = null;
		LinkNode node = null;
		// Execute linked list node
		while (auxiliary != null)
		{
			node = auxiliary;
			temp = auxiliary.next;
			// Find the minimum node
			while (temp != null)
			{
				if (node.data > temp.data)
				{
					node = temp;
				}
				// Visit to next node
				temp = temp.next;
			}
			if (auxiliary.data > node.data)
			{
				// Transfer minimum value to initial position
				// Swap the node value
				swapData(auxiliary, node);
			}
			// Visit to next node
			auxiliary = auxiliary.next;
		}
	}
	public static void Main(String[] args)
	{
		SingleLL sll = new SingleLL();
		//  Constructed linked list
		//  2 → 5 → 0 → 8 → 2 → 4 → 9 → 7 → 0 → 1 → NULL
		sll.addNode(2);
		sll.addNode(5);
		sll.addNode(0);
		sll.addNode(8);
		sll.addNode(2);
		sll.addNode(4);
		sll.addNode(9);
		sll.addNode(7);
		sll.addNode(0);
		sll.addNode(1);
		Console.Write("\n Before sort \n");
		sll.display();
		// Sort linked list
		sll.selectionSort();
		//  0 → 0 → 1 → 2 → 2 → 4 → 5 → 7 → 8 → 9 → NULL
		Console.Write("\n After sort \n");
		sll.display();
	}
}

Output

 Before sort
 2 → 5 → 0 → 8 → 2 → 4 → 9 → 7 → 0 → 1 → NULL

 After sort
 0 → 0 → 1 → 2 → 2 → 4 → 5 → 7 → 8 → 9 → NULL
<?php
/*
  Php Program for
  Selection sort using linked list
*/
// Linked list node
class LinkNode
{
	public $data;
	public $next;

	function __construct($data)
	{
		$this->data = $data;
		$this->next = null;
	}
};
class SingleLL
{
	public $head;

	function __construct()
	{
		$this->head = null;
	}
	//Add new Node at end of linked list
	public	function addNode($data)
	{
		$node = new LinkNode($data);
		if ($this->head == null)
		{
			$this->head = $node;
		}
		else
		{
			$temp = $this->head;
			//Find last node
			while ($temp->next != null)
			{
				$temp = $temp->next;
			}
			// Append the node at last position
			$temp->next = $node;
		}
	}
	// Display linked list element
	public	function display()
	{
		if ($this->head == null)
		{
			echo "\n Empty linked list\n";
			return;
		}
		$temp = $this->head;
		//iterating linked list elements
		while ($temp != null)
		{
			if ($temp != $this->head)
			{
				echo " →";
			}
			echo " ". $temp->data;
			// Visit to next node
			$temp = $temp->next;
		}
		echo " → NULL\n";
	}
	// Swap the linked list node value
	public	function swapData($a, $b)
	{
		$temp = $a->data;
		$a->data = $b->data;
		$b->data = $temp;
	}
	// Sort the linked list using selection sort
	public	function selectionSort()
	{
		$auxiliary = $this->head;
		$temp = null;
		$node = null;
		// Execute linked list node
		while ($auxiliary != null)
		{
			$node = $auxiliary;
			$temp = $auxiliary->next;
			// Find the minimum node
			while ($temp != null)
			{
				if ($node->data > $temp->data)
				{
					$node = $temp;
				}
				// Visit to next node
				$temp = $temp->next;
			}
			if ($auxiliary->data > $node->data)
			{
				// Transfer minimum value to initial position
				// Swap the node value
				$this->swapData($auxiliary, $node);
			}
			// Visit to next node
			$auxiliary = $auxiliary->next;
		}
	}
}

function main()
{
	$sll = new SingleLL();
	//  Constructed linked list
	//  2 → 5 → 0 → 8 → 2 → 4 → 9 → 7 → 0 → 1 → NULL
	$sll->addNode(2);
	$sll->addNode(5);
	$sll->addNode(0);
	$sll->addNode(8);
	$sll->addNode(2);
	$sll->addNode(4);
	$sll->addNode(9);
	$sll->addNode(7);
	$sll->addNode(0);
	$sll->addNode(1);
	echo "\n Before sort \n";
	$sll->display();
	// Sort linked list
	$sll->selectionSort();
	//  0 → 0 → 1 → 2 → 2 → 4 → 5 → 7 → 8 → 9 → NULL
	echo "\n After sort \n";
	$sll->display();
}
main();

Output

 Before sort
 2 → 5 → 0 → 8 → 2 → 4 → 9 → 7 → 0 → 1 → NULL

 After sort
 0 → 0 → 1 → 2 → 2 → 4 → 5 → 7 → 8 → 9 → NULL
/*
  Node Js Program for
  Selection sort using linked list
*/
// Linked list node
class LinkNode
{
	constructor(data)
	{
		this.data = data;
		this.next = null;
	}
};
class SingleLL
{
	constructor()
	{
		this.head = null;
	}
	//Add new Node at end of linked list
	addNode(data)
	{
		var node = new LinkNode(data);
		if (this.head == null)
		{
			this.head = node;
		}
		else
		{
			var temp = this.head;
			//Find last node
			while (temp.next != null)
			{
				temp = temp.next;
			}
			// Append the node at last position
			temp.next = node;
		}
	}
	// Display linked list element
	display()
	{
		if (this.head == null)
		{
			process.stdout.write("\n Empty linked list\n");
			return;
		}
		var temp = this.head;
		//iterating linked list elements
		while (temp != null)
		{
			if (temp != this.head)
			{
				process.stdout.write(" →");
			}
			process.stdout.write(" " + temp.data);
			// Visit to next node
			temp = temp.next;
		}
		process.stdout.write(" → NULL\n");
	}
	// Swap the linked list node value
	swapData(a, b)
	{
		var temp = a.data;
		a.data = b.data;
		b.data = temp;
	}
	// Sort the linked list using selection sort
	selectionSort()
	{
		var auxiliary = this.head;
		var temp = null;
		var node = null;
		// Execute linked list node
		while (auxiliary != null)
		{
			node = auxiliary;
			temp = auxiliary.next;
			// Find the minimum node
			while (temp != null)
			{
				if (node.data > temp.data)
				{
					node = temp;
				}
				// Visit to next node
				temp = temp.next;
			}
			if (auxiliary.data > node.data)
			{
				// Transfer minimum value to initial position
				// Swap the node value
				this.swapData(auxiliary, node);
			}
			// Visit to next node
			auxiliary = auxiliary.next;
		}
	}
}

function main()
{
	var sll = new SingleLL();
	//  Constructed linked list
	//  2 → 5 → 0 → 8 → 2 → 4 → 9 → 7 → 0 → 1 → NULL
	sll.addNode(2);
	sll.addNode(5);
	sll.addNode(0);
	sll.addNode(8);
	sll.addNode(2);
	sll.addNode(4);
	sll.addNode(9);
	sll.addNode(7);
	sll.addNode(0);
	sll.addNode(1);
	process.stdout.write("\n Before sort \n");
	sll.display();
	// Sort linked list
	sll.selectionSort();
	//  0 → 0 → 1 → 2 → 2 → 4 → 5 → 7 → 8 → 9 → NULL
	process.stdout.write("\n After sort \n");
	sll.display();
}
main();

Output

 Before sort
 2 → 5 → 0 → 8 → 2 → 4 → 9 → 7 → 0 → 1 → NULL

 After sort
 0 → 0 → 1 → 2 → 2 → 4 → 5 → 7 → 8 → 9 → NULL
#   Python 3 Program for
#   Selection sort using linked list

#  Linked list node
class LinkNode :
	
	def __init__(self, data) :
		self.data = data
		self.next = None
	

class SingleLL :
	
	def __init__(self) :
		self.head = None
	
	# Add new Node at end of linked list 
	def addNode(self, data) :
		node = LinkNode(data)
		if (self.head == None) :
			self.head = node
		else :
			temp = self.head
			# Find last node
			while (temp.next != None) :
				temp = temp.next
			
			#  Append the node at last position
			temp.next = node
		
	
	#  Display linked list element
	def display(self) :
		if (self.head == None) :
			print("\n Empty linked list")
			return
		
		temp = self.head
		# iterating linked list elements
		while (temp != None) :
			if (temp != self.head) :
				print(" →", end = "")
			
			print("", temp.data, end = "")
			#  Visit to next node
			temp = temp.next
		
		print(" → NULL")
	
	#  Swap the linked list node value
	def swapData(self, a, b) :
		temp = a.data
		a.data = b.data
		b.data = temp
	
	#  Sort the linked list using selection sort
	def selectionSort(self) :
		auxiliary = self.head
		temp = None
		node = None
		#  Execute linked list node
		while (auxiliary != None) :
			node = auxiliary
			temp = auxiliary.next
			#  Find the minimum node
			while (temp != None) :
				if (node.data > temp.data) :
					node = temp
				
				#  Visit to next node
				temp = temp.next
			
			if (auxiliary.data > node.data) :
				#  Transfer minimum value to initial position
				#  Swap the node value
				self.swapData(auxiliary, node)
			
			#  Visit to next node
			auxiliary = auxiliary.next
		
	

def main() :
	sll = SingleLL()
	#   Constructed linked list
	#   2 → 5 → 0 → 8 → 2 → 4 → 9 → 7 → 0 → 1 → NULL
	sll.addNode(2)
	sll.addNode(5)
	sll.addNode(0)
	sll.addNode(8)
	sll.addNode(2)
	sll.addNode(4)
	sll.addNode(9)
	sll.addNode(7)
	sll.addNode(0)
	sll.addNode(1)
	print("\n Before sort ")
	sll.display()
	#  Sort linked list
	sll.selectionSort()
	#   0 → 0 → 1 → 2 → 2 → 4 → 5 → 7 → 8 → 9 → NULL
	print("\n After sort ")
	sll.display()

if __name__ == "__main__": main()

Output

 Before sort
 2 → 5 → 0 → 8 → 2 → 4 → 9 → 7 → 0 → 1 → NULL

 After sort
 0 → 0 → 1 → 2 → 2 → 4 → 5 → 7 → 8 → 9 → NULL
#   Ruby Program for
#   Selection sort using linked list

#  Linked list node
class LinkNode  
	# Define the accessor and reader of class LinkNode  
	attr_reader :data, :next
	attr_accessor :data, :next
 
	
	def initialize(data) 
		self.data = data
		self.next = nil
	end

end

class SingleLL  
	# Define the accessor and reader of class SingleLL  
	attr_reader :head
	attr_accessor :head
 
	
	def initialize() 
		self.head = nil
	end

	# Add new Node at end of linked list 
	def addNode(data) 
		node = LinkNode.new(data)
		if (self.head == nil) 
			self.head = node
		else 
			temp = self.head
			# Find last node
			while (temp.next != nil) 
				temp = temp.next
			end

			#  Append the node at last position
			temp.next = node
		end

	end

	#  Display linked list element
	def display() 
		if (self.head == nil) 
			print("\n Empty linked list\n")
			return
		end

		temp = self.head
		# iterating linked list elements
		while (temp != nil) 
			if (temp != self.head) 
				print(" →")
			end

			print(" ", temp.data)
			#  Visit to next node
			temp = temp.next
		end

		print(" → NULL\n")
	end

	#  Swap the linked list node value
	def swapData(a, b) 
		temp = a.data
		a.data = b.data
		b.data = temp
	end

	#  Sort the linked list using selection sort
	def selectionSort() 
		auxiliary = self.head
		temp = nil
		node = nil
		#  Execute linked list node
		while (auxiliary != nil) 
			node = auxiliary
			temp = auxiliary.next
			#  Find the minimum node
			while (temp != nil) 
				if (node.data > temp.data) 
					node = temp
				end

				#  Visit to next node
				temp = temp.next
			end

			if (auxiliary.data > node.data) 
				#  Transfer minimum value to initial position
				#  Swap the node value
				self.swapData(auxiliary, node)
			end

			#  Visit to next node
			auxiliary = auxiliary.next
		end

	end

end

def main() 
	sll = SingleLL.new()
	#   Constructed linked list
	#   2 → 5 → 0 → 8 → 2 → 4 → 9 → 7 → 0 → 1 → NULL
	sll.addNode(2)
	sll.addNode(5)
	sll.addNode(0)
	sll.addNode(8)
	sll.addNode(2)
	sll.addNode(4)
	sll.addNode(9)
	sll.addNode(7)
	sll.addNode(0)
	sll.addNode(1)
	print("\n Before sort \n")
	sll.display()
	#  Sort linked list
	sll.selectionSort()
	#   0 → 0 → 1 → 2 → 2 → 4 → 5 → 7 → 8 → 9 → NULL
	print("\n After sort \n")
	sll.display()
end

main()

Output

 Before sort 
 2 → 5 → 0 → 8 → 2 → 4 → 9 → 7 → 0 → 1 → NULL

 After sort 
 0 → 0 → 1 → 2 → 2 → 4 → 5 → 7 → 8 → 9 → NULL
/*
  Scala Program for
  Selection sort using linked list
*/
// Linked list node
class LinkNode(var data: Int , var next: LinkNode)
{
	def this(data: Int)
	{
		this(data, null);
	}
};
class SingleLL(var head: LinkNode)
{
	def this()
	{
		this(null);
	}
	//Add new Node at end of linked list
	def addNode(data: Int): Unit = {
		var node: LinkNode = new LinkNode(data);
		if (this.head == null)
		{
			this.head = node;
		}
		else
		{
			var temp: LinkNode = this.head;
			//Find last node
			while (temp.next != null)
			{
				temp = temp.next;
			}
			// Append the node at last position
			temp.next = node;
		}
	}
	// Display linked list element
	def display(): Unit = {
		if (this.head == null)
		{
			print("\n Empty linked list\n");
			return;
		}
		var temp: LinkNode = this.head;
		//iterating linked list elements
		while (temp != null)
		{
			if (temp != this.head)
			{
				print(" →");
			}
			print(" " + temp.data);
			// Visit to next node
			temp = temp.next;
		}
		print(" → NULL\n");
	}
	// Swap the linked list node value
	def swapData(a: LinkNode, b: LinkNode): Unit = {
		var temp: Int = a.data;
		a.data = b.data;
		b.data = temp;
	}
	// Sort the linked list using selection sort
	def selectionSort(): Unit = {
		var auxiliary: LinkNode = this.head;
		var temp: LinkNode = null;
		var node: LinkNode = null;
		// Execute linked list node
		while (auxiliary != null)
		{
			node = auxiliary;
			temp = auxiliary.next;
			// Find the minimum node
			while (temp != null)
			{
				if (node.data > temp.data)
				{
					node = temp;
				}
				// Visit to next node
				temp = temp.next;
			}
			if (auxiliary.data > node.data)
			{
				// Transfer minimum value to initial position
				// Swap the node value
				this.swapData(auxiliary, node);
			}
			// Visit to next node
			auxiliary = auxiliary.next;
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var sll: SingleLL = new SingleLL();
		//  Constructed linked list
		//  2 → 5 → 0 → 8 → 2 → 4 → 9 → 7 → 0 → 1 → NULL
		sll.addNode(2);
		sll.addNode(5);
		sll.addNode(0);
		sll.addNode(8);
		sll.addNode(2);
		sll.addNode(4);
		sll.addNode(9);
		sll.addNode(7);
		sll.addNode(0);
		sll.addNode(1);
		print("\n Before sort \n");
		sll.display();
		// Sort linked list
		sll.selectionSort();
		//  0 → 0 → 1 → 2 → 2 → 4 → 5 → 7 → 8 → 9 → NULL
		print("\n After sort \n");
		sll.display();
	}
}

Output

 Before sort
 2 → 5 → 0 → 8 → 2 → 4 → 9 → 7 → 0 → 1 → NULL

 After sort
 0 → 0 → 1 → 2 → 2 → 4 → 5 → 7 → 8 → 9 → NULL
/*
  Swift 4 Program for
  Selection sort using linked list
*/
// Linked list node
class LinkNode
{
	var data: Int;
	var next: LinkNode? ;
	init(_ data: Int)
	{
		self.data = data;
		self.next = nil;
	}
};
class SingleLL
{
	var head: LinkNode? ;
	init()
	{
		self.head = nil;
	}
	//Add new Node at end of linked list
	func addNode(_ data: Int)
	{
		let node: LinkNode? = LinkNode(data);
		if (self.head == nil)
		{
			self.head = node;
		}
		else
		{
			var temp: LinkNode? = self.head;
			//Find last node
			while (temp!.next  != nil)
			{
				temp = temp!.next;
			}
			// Append the node at last position
			temp!.next = node;
		}
	}
	// Display linked list element
	func display()
	{
		if (self.head == nil)
		{
			print("\n Empty linked list");
			return;
		}
		var temp: LinkNode? = self.head;
		//iterating linked list elements
		while (temp  != nil)
		{
			if (!(temp === self.head))
			{
				print(" →", terminator: "");
			}
			print("", temp!.data, terminator: "");
			// Visit to next node
			temp = temp!.next;
		}
		print(" → NULL");
	}
	// Swap the linked list node value
	func swapData(_ a: LinkNode? , _ b : LinkNode? )
	{
		let temp: Int = a!.data;
		a!.data = b!.data;
		b!.data = temp;
	}
	// Sort the linked list using selection sort
	func selectionSort()
	{
		var auxiliary: LinkNode? = self.head;
		var temp: LinkNode? = nil;
		var node: LinkNode? = nil;
		// Execute linked list node
		while (auxiliary  != nil)
		{
			node = auxiliary;
			temp = auxiliary!.next;
			// Find the minimum node
			while (temp  != nil)
			{
				if (node!.data > temp!.data)
				{
					node = temp;
				}
				// Visit to next node
				temp = temp!.next;
			}
			if (auxiliary!.data > node!.data)
			{
				// Transfer minimum value to initial position
				// Swap the node value
				self.swapData(auxiliary, node);
			}
			// Visit to next node
			auxiliary = auxiliary!.next;
		}
	}
}
func main()
{
	let sll: SingleLL = SingleLL();
	//  Constructed linked list
	//  2 → 5 → 0 → 8 → 2 → 4 → 9 → 7 → 0 → 1 → NULL
	sll.addNode(2);
	sll.addNode(5);
	sll.addNode(0);
	sll.addNode(8);
	sll.addNode(2);
	sll.addNode(4);
	sll.addNode(9);
	sll.addNode(7);
	sll.addNode(0);
	sll.addNode(1);
	print("\n Before sort ");
	sll.display();
	// Sort linked list
	sll.selectionSort();
	//  0 → 0 → 1 → 2 → 2 → 4 → 5 → 7 → 8 → 9 → NULL
	print("\n After sort ");
	sll.display();
}
main();

Output

 Before sort
 2 → 5 → 0 → 8 → 2 → 4 → 9 → 7 → 0 → 1 → NULL

 After sort
 0 → 0 → 1 → 2 → 2 → 4 → 5 → 7 → 8 → 9 → NULL
/*
  Kotlin Program for
  Selection sort using linked list
*/
// Linked list node
class LinkNode
{
	var data: Int;
	var next: LinkNode ? ;
	constructor(data: Int)
	{
		this.data = data;
		this.next = null;
	}
};
class SingleLL
{
	var head: LinkNode ? ;
	constructor()
	{
		this.head = null;
	}
	//Add new Node at end of linked list
	fun addNode(data: Int): Unit
	{
		var node: LinkNode ? = LinkNode(data);
		if (this.head == null)
		{
			this.head = node;
		}
		else
		{
			var temp: LinkNode ? = this.head;
			//Find last node
			while (temp?.next != null)
			{
				temp = temp.next;
			}
			// Append the node at last position
			temp?.next = node;
		}
	}
	// Display linked list element
	fun display(): Unit
	{
		if (this.head == null)
		{
			print("\n Empty linked list\n");
			return;
		}
		var temp: LinkNode ? = this.head;
		//iterating linked list elements
		while (temp != null)
		{
			if (temp != this.head)
			{
				print(" →");
			}
			print(" " + temp.data);
			// Visit to next node
			temp = temp.next;
		}
		print(" → NULL\n");
	}
	// Swap the linked list node value
	fun swapData(a: LinkNode, b : LinkNode): Unit
	{
		var temp: Int = a.data;
		a.data = b.data;
		b.data = temp;
	}
	// Sort the linked list using selection sort
	fun selectionSort(): Unit
	{
		var auxiliary: LinkNode ? = this.head;
		var temp: LinkNode ? ;
		var node: LinkNode ? ;
		// Execute linked list node
		while (auxiliary != null)
		{
			node = auxiliary;
			temp = auxiliary.next;
			// Find the minimum node
			while (temp != null)
			{
				if (node!!.data > temp.data)
				{
					node = temp;
				}
				// Visit to next node
				temp = temp.next;
			}
			if (auxiliary.data > node!!.data)
			{
				// Transfer minimum value to initial position
				// Swap the node value
				this.swapData(auxiliary, node);
			}
			// Visit to next node
			auxiliary = auxiliary.next;
		}
	}
}
fun main(args: Array < String > ): Unit
{
	var sll: SingleLL = SingleLL();
	//  Constructed linked list
	//  2 → 5 → 0 → 8 → 2 → 4 → 9 → 7 → 0 → 1 → NULL
	sll.addNode(2);
	sll.addNode(5);
	sll.addNode(0);
	sll.addNode(8);
	sll.addNode(2);
	sll.addNode(4);
	sll.addNode(9);
	sll.addNode(7);
	sll.addNode(0);
	sll.addNode(1);
	print("\n Before sort \n");
	sll.display();
	// Sort linked list
	sll.selectionSort();
	//  0 → 0 → 1 → 2 → 2 → 4 → 5 → 7 → 8 → 9 → NULL
	print("\n After sort \n");
	sll.display();
}

Output

 Before sort
 2 → 5 → 0 → 8 → 2 → 4 → 9 → 7 → 0 → 1 → NULL

 After sort
 0 → 0 → 1 → 2 → 2 → 4 → 5 → 7 → 8 → 9 → NULL


Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved