简体   繁体   English

选择排序双链表Java

[英]Selection sort Doubly linked list java

I need to Sort Linked list using Selection sort. 我需要使用选择排序对链接列表进行排序。 But I can not use Collections. 但是我不能使用收藏集。 I have a troubles with finding smallest elements and creating a new version of sorted list. 我很难找到最小的元素并创建新版本的排序列表。 Thanks. 谢谢。

    public class LinkedList {
        public Node first;
        public Node last;

        public LinkedList() {
            first = null;
            last = null;
        }

        public boolean isEmpty() {
            return first == null;
        }

        public void addFirst(Student student) {
            Node newNode = new Node(student);
            if (isEmpty())
                last = newNode;
            else
                first.previous = newNode;
            newNode.next = first;
            first = newNode;
        }

        public void addLast(Student student) {
            Node newNode = new Node(student);
            if (isEmpty())
                first = newNode;
            else
                last.next = newNode;
            newNode.previous = last;
            last = newNode;
        }

        public void display() {
            Node current = last;
            while (current != null) {
                System.out.print(current.student.name + "\b");
                System.out.print(current.student.surname + "\b");
                System.out.println(current.student.educationType);
                current = current.previous;
            }
        }

Because of non-working findSmallest method Sort method doesn't work correctly. 由于findSmallest方法无法正常工作, findSmallest Sort方法无法正常工作。 I try to implelent sorting by creating a new list where I put Nodes in sorted way. 我试图通过创建一个新列表来实现排序,在该列表中我将Nodes进行排序。 And it also doesn't go out of "While loop" 而且它也不会超出“ While loop”

        public void Sort() {
            LinkedList list = new LinkedList();
            Node toStart = last;
            while (toStart!=null){
                list.addLast(findSmallest(toStart).student);
                toStart = toStart.previous;
            }


        }

It sends biggest element added and if I manually assign'last' to'smallest' it would work. 它发送添加的最大元素,如果我手动将“ last”分配给“ smallest”,它将起作用。

        public Node findSmallest(Node toStartFrom) {
            Node current = toStartFrom;
            Node smallest = toStartFrom; //if i put here `last` it will work correctly
            while(current != null) {
                if (smallest.student.name.compareToIgnoreCase(current.student.name) > 0) smallest = current;
                current = current.previous;
            }
            return smallest;
        }

    }

  public class Node {
        public Student student;

        public Node next;
        public Node previous;

        public Node(Student student) {
            this.student = student;
        }
    }


    public class Student {
        public String name;
        public String surname;
        public String educationType;

        static public Student createStudent() {
         ....
            return student;
        }
    }

It might be helpful to not have a doubly linked list because then you have less links that you need to maintain. 没有双向链接列表可能会有所帮助,因为这样一来,您需要维护的链接就会减少。 Also you might be having trouble with the findSmallest() method because you are initially setting you current and smallest to the same node so when the if(smallest.student.name.compareToIgnoreCase(current.student.name) > 0) statement is executed you are comparing the name of the student to the same name of the student. 另外,您可能在findSmallest()方法上遇到麻烦,因为您最初将当前和最小设置为同一节点,因此当执行if(smallest.student.name.compareToIgnoreCase(current.student.name)> 0)语句时您正在将学生的姓名与学生的姓名进行比较。 For example if the node that smallest is set to has a student name of John well current is set to the same node so the student name of current is also John. 例如,如果将最小的节点设置为具有John的学生名,那么current当前被设置为同一节点,因此current的学生名也是John。 Not an issue if they are different students with the same name but in your code they are the same student and both current and smallest point to the same node. 如果他们是同名的不同学生,但在您的代码中他们是同一学生,并且当前和最小指向同一节点,这不是问题。 Effectively this if statement is always going to be false and you will never execute the code to move current along the list. 实际上,此if语句始终会为false,并且您将永远不会执行代码以使电流沿列表移动。 That is also why when you set smallest = last the method does work at least some of the time. 这就是为什么当您设置minimum = last时,该方法至少在某些时候有效。

As the above said, try something like 如上所述,尝试类似

smallest = startnode
next =startnode.next
while(next != null)
compare next with smallest, and assign accordingly
next = next.next

Shouldn't be too hard to turn this into code 将其转换为代码应该不太难

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM