簡體   English   中英

使用插入排序,雙鏈表對字符串數組索引進行排序

[英]Sorting String Array Indicies with Insertion Sort, Doubly Linked List

我正在做一個作業,在該作業中,我必須對數組進行排序並將索引存儲在已構建的雙向鏈接列表ADT中。

Example:
{"A","B","C","A"} -> {0,3,1,2}

我當前的方法是將索引放入ADT中,然后在對數組進行排序時對ADT進行排序。 據我所知,每次循環執行時,我都會在數組和雙向鏈表上執行完全相同的操作,但是我的輸出不匹配。

public static void main(String[] args ) {
    List A = new List();
    String[] inputArray = {"A","C","B","A"};
    int i,j;
    String key;
    //load indices into doubly-linked list
    for (int x = 0; x < inputArray.length; x++ ) {
        A.append(x);
    }
    //begin insertion sort
    for (j = 1; j < inputArray.length; j++) {
        System.out.println(A);
        System.out.println(Arrays.toString(inputArray));
        key = inputArray[j];
        i = j - 1;
        while (i >= 0) {
            if (key.compareTo(inputArray[i]) > 0) {
                break;
            }
            inputArray[i+1] = inputArray[i];
            //moveTo moves the cursor to <int>
            A.moveTo(i);
            //make sure we aren't trying to insert before first node
            if (i > 0) { A.insertBefore(i+1); }
            else { A.prepend(i+1); }
            //remove node at cursor
            A.delete();
            i--;
        }
        inputArray[i+1] = key;
        A.moveTo(i+1);
        if (i > 0) { A.insertBefore(i+1); }
        else { A.prepend(i+1); }
        A.delete();
    }
    System.out.println(A);
    System.out.println(Arrays.toString(inputArray));
}

上面的代碼提供以下輸出:

跑:
0 1 2 3
[A,C,B,A]
1 0 2 3
[A,C,B,A]
1 1 2 3
[A,B,C,A]
0 2 3 3
[A,A,B,C]
建立成功(總時間:0秒)

編輯:List.java

public class List {

    private class Node{
        //Fields
        int data;
        Node next, previous;
        //Constructor
        Node(int data) {
            this.data = data;
            next = null;
            previous = null;
        }
        public String toString() { return String.valueOf(data); }
    }

    //Fields
    private Node frontNode, backNode, cursorNode;
    private int totalSize, cursorPosition;

    //Constructor
    List() {
        frontNode = backNode = cursorNode = null;
        totalSize = 0;
        cursorPosition = -1;
    }
    //moveTo(int i): If 0<=i<=length()-1, moves the cursor to the element 
    // at index i, otherwise the cursor becomes undefined.
    void moveTo(int i) {
        if (i == 0) {
            cursorPosition = i;
            cursorNode = frontNode;
        }
        else if (i == length() - 1) {
            cursorPosition = i;
            cursorNode = backNode;
        }
        else if (i > 0 && i < length() - 1 ) {
            cursorNode = frontNode;
            cursorPosition = i;
            for (int x=0; x < i; x++) {
                cursorNode = cursorNode.next;
            }
        }
        else {
            cursorPosition = -1; 
        }
    }

//prepend(int data): Inserts new element before front element in this List.
    void prepend(int data) {
        Node node = new Node(data);
        if ( this.length() == 0 ) {
            frontNode = backNode = node;
        }
        else {
            frontNode.previous = node;
            node.next = frontNode;
            frontNode = node;
        }
        totalSize++;
        //cursorPosition might change?
    }
    //insertBefore(int data): Inserts new element before cursor element in this
    // List. Pre: length()>0, getIndex()>=0
    void insertBefore(int data) {
        Node node = new Node(data);
        if ( this.length() > 0 && this.getIndex() >= 0 ) {
            node.previous = cursorNode.previous;
            node.next = cursorNode;
            cursorNode.previous.next = node;
            cursorNode.previous = node;
            totalSize++;
        }
        else if ( this.length() <= 0 )
        {
           throw new RuntimeException
                    ("Error: insertBefore called on empty list");
        }
        else {
            throw new RuntimeException
                    ("Error: insertBefore called without cursor set");
        }
    }

好,謝謝。 我不確切知道打算要執行insertBefore()和prepend()(我可能已經猜到了,但是編程不應該是猜測應該做什么方法;看文檔會更好)。

由於這是一項任務,因此我不會給您答案。 但是線索是這樣的:在第一次通過循環之后,A的轉儲會為您提供與開始時相同的索引,但是會重新排列。 我認為這就是每次循環迭代后的方式。 但這在第二次通過循環之后是不正確的(1出現兩次,而0消失了)。 想一想。 當你打電話A.insertBefore()來重新排列的元素,什么樣的數據應該告訴的insertBefore()來插入,是什么?你居然插入?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM