用于Java中的int数组链接列表的bubbleSort()

时间:2014-02-13 19:53:01

标签: java arrays

我在使用非常独特的作业分配的bubbleSort方法时遇到了问题。

我们应该使用我们选择的排序方法来排序,得到这个,int数组的链表。不是ArrayList而不仅仅是LinkedList。它的工作方式类似于链接列表,但每个节点包含一个容量为10个整数的数组。

我坚持使用排序方法。我选择bubbleSort只是因为它在最后一次作业中使用过,我觉得它最熟悉。任何有关更好的排序方法的提示都会被认为是有用的。

这是我的代码:

public void bubbleSort() {

        current = head;                     // Start at the head ArrayNode
        for (int i = 0; i < size; i++) {    // iterate through each ArrayNode
            currentArray = current.getArray();  // get the array in this ArrayNode

            int in, out;    
            for (out = size-1; out > 1; out--) {        // outer loop (backwards)
                for (in = 0; in < out; in++) {              // inner loop (forwards)
                    if (currentArray[in] > currentArray[in+1])  // out of order?
                    swap(in, in+1);                             // swap them!
                }
            }
            current.setArray(currentArray);
            current = current.getNext();
        }
    }// End bubbleSort() method

// A helper method for the bubble sort
private void swap(int one, int two) {
    int temp = currentArray[one];
    currentArray[one] = currentArray[two];
    currentArray[two] = temp;
} // End swap() method

这是我应该做的事情的图片示例。 Growable Linked List of Arrays

2 个答案:

答案 0 :(得分:1)

我找到了一个带有selectionsort的解决方案。有几个测试值,只需运行它来查看它。 如果需要,我可以提供进一步的信息。

import java.util.ArrayList;
import java.util.Random;

public class ArrayedListSort {

int listsize = 5;  // how many nodes
int maxValue = 99; // the highest value (0 to this)
int nodeSize = 3;  // size for every node

public static void main(String[] args) {
    // run non static
    new ArrayedListSort().runTest();
}

/**
 * Log function.
 */
public void log(Object s) {
    System.out.println(s);
}
public void logNoBR(Object s) {
    System.out.print(s);
}

/**
 * Output of list we have.
 */
public void logMyList(ArrayList<ListNode> listNode, String name) {
    log("=== LOG OUTPUT " + name + " ===");
    for ( int i=0; i < listNode.size(); i++) {
        logNoBR(" node <" + i + ">");
        logNoBR(" (");
        for (int j=0; j < listNode.get(i).getSize(); j++) {
            if ( j != (listNode.get(i).getSize()-1)) // if not last add ","
                logNoBR( listNode.get(i).getValueAt(j) + "," );
            else
                logNoBR( listNode.get(i).getValueAt(j) );
        }
        log(")");
    }
    log("=====================================\n");
}

public void runTest() {
    // create example List

    ArrayList<ListNode> myList = new ArrayList<ListNode>();

    // fill the nodes with random values
    for ( int i = 0; i < listsize; i++) {
        myList.add(new ListNode(nodeSize));
        for (int j=0; j < nodeSize; j++) {
            int randomValue = new Random().nextInt(maxValue);
            myList.get(i).addValue(randomValue);
        }
    }
    logMyList(myList, "myList unsorted"); // to see what we have

    // now lets sort it
    myList = sortListNode(myList);

    logMyList(myList, "myList sorted"); // what we have after sorting
}

/**
 *  Selectionsort 
 */
public ArrayList<ListNode> sortListNode(ArrayList<ListNode> myList) {
    ArrayList<ListNode> retList = new ArrayList<ListNode>();
    for ( int i = 0; i < listsize; i++) {
        retList.add(new ListNode(nodeSize));
    }
    int lastSmallest = myList.get(0).getValueAt(0);

    while ( !myList.isEmpty() ) {
        int lastJ=0, lastI=0;
        for ( int i = 0; i < myList.size(); i++) {
            for (int j=0; j < myList.get(i).getSize(); j++) {
                if ( myList.get(i).getValueAt(j) <= lastSmallest ) {
                    lastSmallest = myList.get(i).getValueAt(j);
                    lastJ = j;
                    lastI = i;
                    //log("Found smallest element at <"+i+","+j+"> (" + lastSmallest + ")");
                }
            }
        }
        myList.get(lastI).removeValue(lastJ);

        if ( myList.get(lastI).getSize() == 0 )
            myList.remove(lastI);

        // add value to new list
        for ( int i = 0; i < listsize; i++) {
            if ( retList.get(i).getSize() < retList.get(i).getMaxSize() ) {
                retList.get(i).addValue(lastSmallest);
                break;
            }
        }
        lastSmallest = Integer.MAX_VALUE;

    }
    return retList;
}

public class ListNode {
    private ArrayList<Integer>  values  = new ArrayList<Integer>();
    private  int                    maxSize;

    public ListNode(int maxSize) {
        this.maxSize = maxSize;
    }
    public ArrayList<Integer> getValues() {
        return values;
    }
    public int getMaxSize() {
        return maxSize;
    }
    public int getSize() {
        return values.size();
    }
    public int getValueAt(int position) {
        if ( position < values.size())
            return values.get(position);
        else
            throw new IndexOutOfBoundsException();
    }
    public void addValue(int value) {
        values.add(value);
    }
    public void removeValue(int position) {
        if ( position < values.size()) {
            values.remove(position);                    
        } else
            throw new IndexOutOfBoundsException();
    }
}

}

答案 1 :(得分:0)

我们走了。简单的解决方案包括提取每个数组节点的所有元素并将它们存储在一个大数组中,对该大数组进行排序(使用冒号排序,快速排序,Shell排序等),最后重建数组的链接列表。排序的值。我几乎肯定这不是你想要的。

如果您想对数字进行排序,我可以考虑以下算法:

正如其他人所评论的,您需要一个比较函数来确定节点 A 是否在节点 B 之前。以下算法使用第一个想法但是对于每对节点,例如, A->[3, 9, 7]B->[1, 6, 8]变为[1, 3, 6, 7, 8, 9],最后变为A->[1,3, 6]B->[7, 8, 9]。如果我们对每个可能的对应用这种重新排列,最终将得到一个排序的数组链表(尽管我没有证据)。

A = head;
while (A.hasNext()) {
    arrayA = A.getArray();
    B = A.getNext();
    while (B.hasNext()) {
        arrayB = B.getArray();

        // concatenate two arrays
        int[] C = new int[arrayA.size() + arrayB.size()];
        int i;
        for (i = 0; i < arrayA.size(); i++)
            C[i] = arrayA[i];
        for ( ; i < arrayA.size() + arrayB.size(); i++)
            C[i] = arrayB[i-arrayA.size()];

        // sort the new arrays using agains Bubble sort or any
        // other method, or Arrays.sort()
        Arrays.sort(C);

        // now return the sorted values to the two arrays
        for (i = 0; i < arrayA.size(); i++)
            arrayA[i] = C[i];
        for (i = 0; i < arrayB.size(); i++)
            arrayB[i] = C[i+arrayA.size()];
    }
}

这是一种伪代码,因为我没有使用Java中的链表,但我认为你明白了。

注意:我没有测试过代码,可能包含恐怖。