Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Java


class ListNode {
    int data;
    ListNode next;
    public ListNode(int data) {
        this.data = data;
    }
}

public class BidirectionalSelectionSort {
    public static ListNode bidirectionalSelectionSort(ListNode head) {
        if (head == null || head.next == null) {
            return head; // Already sorted or empty list
        }

        ListNode start = null;
        ListNode end = null;
        ListNode sortedTail = null;

        while (sortedTail != head) {
            ListNode minPrev = null;
            ListNode min = head;
            ListNode maxPrev = null;
            ListNode max = head;
            ListNode curr = head;

            while (curr.next != sortedTail) {
                if (curr.data > curr.next.data) {
                    if (curr.data > max.data) {
                        max = curr;
                        maxPrev = minPrev;
                    }
                    if (curr.next.data < min.data) {
                        min = curr.next;
                        minPrev = curr;
                    }
                }
                curr = curr.next;
            }

            if (min == max) {
                break;
            }

            if (minPrev != null) {
                minPrev.next = min.next;
            } else {
                head = min.next;
            }

            if (maxPrev != null) {
                maxPrev.next = max.next;
            } else {
                head = max.next;
            }

            if (sortedTail != null) {
                sortedTail.next = min;
            }

            min.next = max;
            max.next = null;
            sortedTail = max;
        }

        return head;
    }

    public static void printList(ListNode head) {
        while (head != null) {
            System.out.print(head.data + " ");
            head = head.next;
        }
        System.out.println();
    }

    public static void main(String[] args) {
        ListNode head = new ListNode(6);
        head.next = new ListNode(2);
        head.next.next = new ListNode(8);
        head.next.next.next = new ListNode(3);
        head.next.next.next.next = new ListNode(1);

        System.out.println("Original Linked List:");
        printList(head);

        head = bidirectionalSelectionSort(head);

        System.out.println("Sorted Linked List:");
        printList(head);
    }
}


What I have tried:

int data;
I tried to change to string type
Posted
Comments
Richard MacCutchan 11-Nov-23 12:07pm    
What is the error?
Member 15627495 12-Nov-23 8:49am    
you need to mention "private" for your "datas" class, then you can use 'this.'
Richard MacCutchan 12-Nov-23 10:58am    
That is not true; the this accessor has access to all properties and methods whatever their access level.

1 solution

You have not initialized the 'next' node in your constructor, you can set the 'next' to null. This way, when you create a new 'ListNode', the 'next' field is set to null by default -

Java
class ListNode {
    int data;
    ListNode next;

    public ListNode(int data) {
        this.data = data;
        this.next = null; 
        //Initialize the 'next' node...
    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900