Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

selection sort linked list java

// Java implementation of the approach
class GFG {
 
    // Linked List Node
    static class Node {
        int data;
        Node next;
    };
 
    // Utility function to create a
    // new Linked List Node
    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
    static Node selectionSort(Node head)
    {
        Node a, b, c, d, r;
 
        a = b = head;
 
        // 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) {
 
                        // Case 1: b is the head of the linked list
                        if (b == head) {
 
                            // Move d before b
                            b.next = d.next;
                            d.next = b;
 
                            // Swap b and d pointers
                            r = b;
                            b = d;
                            d = r;
 
                            c = d;
 
                            // Update the head
                            head = b;
 
                            // Skip to the next element
                            // as it is already in order
                            d = d.next;
                        }
 
                        // Case 2: b is not the head of the linked list
                        else {
 
                            // Move d before b
                            b.next = d.next;
                            d.next = b;
                            a.next = d;
 
                            // Swap b and d pointers
                            r = b;
                            b = d;
                            d = r;
 
                            c = d;
 
                            // Skip to the next element
                            // as it is already in order
                            d = d.next;
                        }
                    }
 
                    // If b and d have some non-zero
                    // number of nodes in between them
                    else {
 
                        // Case 3: b is the head of the linked list
                        if (b == head) {
 
                            // 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;
 
                            // Skip to the next element
                            // as it is already in order
                            d = d.next;
 
                            // Update the head
                            head = b;
                        }
 
                        // 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;
 
                            // Skip to the next element
                            // as it is already in order
                            d = d.next;
                        }
                    }
                }
                else {
 
                    // Update c and skip to the next element
                    // as it is already in order
                    c = d;
                    d = d.next;
                }
            }
 
            a = b;
            b = b.next;
        }
 
        return head;
    }
 
    // Function to print the list
    static void printList(Node head)
    {
        while (head != null) {
            System.out.print(head.data + " ");
            head = head.next;
        }
    }
 
    // Driver Code
    public static void main(String args[])
    {
        Node head = newNode(5);
        head.next = newNode(4);
        head.next.next = newNode(3);
 
        head = selectionSort(head);
 
        printList(head);
    }
}
 
// This code is contributed by Arnab Kundu
Comment

PREVIOUS NEXT
Code Example
Java :: spring xml configuration 
Java :: java implement interface 
Java :: Java LogManager 
Java :: android get sdk version 
Java :: android convert date to local timezone 
Java :: Example of Creating a Java Stack 
Java :: java check if string ends with 
Java :: how to highlight text in android studio 
Java :: R8: java.lang.OutOfMemoryError: GC overhead limit exceeded react-native 
Java :: how to create char type with jpa 
Java :: how to put a string in gradle file and acce to it android studio 
Java :: fragment view in XML android 
Java :: pop back stack fragment android 
Java :: array reverse in java 
Java :: hasNext for scanner class java 
Java :: java stream inner join two lists 
Java :: object creation in java 
Java :: android studio Toast usage 
Java :: Java system load from resources 
Java :: how to read a table from text file in java 
Java :: calendar check if month is 30 days 
Java :: how to make a button disapear on click in javafx 
Java :: Sample HashMap 
Java :: array de meses java 
Java :: change brightness of image in java 
Java :: transparent border of jtextfield in java 
Java :: java heap sort heapify geeks for geeks 
Java :: how to make window in java 
Java :: for each loop in java 
Java :: how to create a node in java 
ADD CONTENT
Topic
Content
Source link
Name
8+7 =