插入排序整数从文件读取到链表java

时间:2017-02-19 19:44:44

标签: java file sorting integer insertion-sort

我需要将文件中的整数读入链表,使用插入排序对列表进行排序,然后报告我的机器使用java完成插入排序需要多长时间。目前,我的代码除了从文件中读取外,一切正常,它只读取第一个和最后一个数字。例如,如果我以相反的顺序从具有数字1到5000的文件中读取,则它将仅读取并排序5000和1。

如何将文件中的所有整数读入ListNodes?代码发布如下:

import java.io.File;
import java.io.FileNotFoundException;
import java.util.*;

class ListNode {
    int val;
    ListNode next;

    ListNode(int x) {
        val = x;
        next = null;
    }
}

public class InsertionLinkedList {

    public static ListNode insertionSortList(ListNode head) {

        long start = System.nanoTime();
        if (head == null || head.next == null)
            return head;

        ListNode newHead = new ListNode(head.val);
        ListNode pointer = head.next;

        // loop through each element in the list
        while (pointer != null) {
            // insert this element to the new list

            ListNode innerPointer = newHead;
            ListNode next = pointer.next;

            if (pointer.val <= newHead.val) {
                ListNode oldHead = newHead;
                newHead = pointer;
                newHead.next = oldHead;
            } else {
                while (innerPointer.next != null) {

                    if (pointer.val > innerPointer.val && pointer.val <= innerPointer.next.val) {
                        ListNode oldNext = innerPointer.next;
                        innerPointer.next = pointer;
                        pointer.next = oldNext;
                    }

                    innerPointer = innerPointer.next;
                }

                if (innerPointer.next == null && pointer.val > innerPointer.val) {
                    innerPointer.next = pointer;
                    pointer.next = null;
                }
            }

            // finally
            pointer = next;
        }
        long time = System.nanoTime() - start;
        System.out.printf("The time taken was %.1f ns%n", (double) time);
        return newHead;
    }

    public static void main(String[] args) throws FileNotFoundException {

        Scanner scanner = new Scanner(new File("random5k.txt"));
        ListNode insertion = new ListNode(scanner.nextInt());
        while(scanner.hasNextInt()){
            ListNode nextNode = new ListNode(scanner.nextInt());
            insertion.next = nextNode;
        }

        insertion = insertionSortList(insertion);
    }
}

1 个答案:

答案 0 :(得分:-1)

  

目前,我的代码做的一切正常,除了从中读取   文件,它只读取第一个和最后一个数字。例如,如果我   以相反的顺序读取编号为1到5000的文件   只读和排序5000和一。

您实际上是正确读取文件中的所有数字。只是当您尝试填充ListNode对象insertion时,您不会将每个节点的next指向实际的下一个节点。请参阅我的程序中的编辑内容。

public static void main(String[] args) throws FileNotFoundException {

Scanner scanner = new Scanner(new File("random5k.txt"));
ListNode insertion = new ListNode(scanner.nextInt());
ListNode intNodes = insertion;
while(scanner.hasNextInt()){
    ListNode nextNode = new ListNode(scanner.nextInt());
    insertion.next = nextNode;
    insertion = nextNode;
}
intNodes = insertionSortList(intNodes);
}