简体   繁体   中英

My SelectionSort method does not work. Why?

I am trying to use the selection sort algorithm in a version of the doubly linked list that I wrote myself. For this question we can assume that there are no errors elsewhere other than the code that I post (at least, none relevant to the question). I have done plenty of testing.

here is my method:

public void selectionSort(){

    ListItem front = head;
    ListItem current;
    T currentLowest;
    T potentialLowest;
    int lowestIndex = 0;
    for (int a = 0; a<count-1; a++){
        System.out.println("a: "+a);
        currentLowest = (T) front.content;
        front = front.next;
        current = front.next;
    for(int i = a+1; i<count; i++){
        System.out.println("i: "+i);
**(29)**    potentialLowest = (T) current.content;
        if (potentialLowest.compareTo(currentLowest)==-1)
        {
            currentLowest = (T) current.content;
            lowestIndex = i;
        }
        if(current.next == null)break;

        current = current.next;
    }
    System.out.println("swapped"+a+","+lowestIndex);
    swap(a, lowestIndex);
}

}

It is sorting a list of 100 integers. Here is the last bit of output before I receive a null pointer on line 29 (marked).

swapped95,97

a: 96 i: 97 i: 98

swapped96,97

a: 97 i: 98

swapped97,97

a: 98 i: 99 (null pointer)

I had this working earlier but it was horribly optimized. After making some changes, I'm stuck with this. Any ideas?

Thanks for your time.

Well you're trying to access the content of a null element. When you're on the last element, your "current" will null when you set it to next.

I think I'm a little too tired to provide a fix for it, but you should be able to compare your old (working) code to it and spot the fix.

I think the problem might arise in the first iteration of your sorting loop. Considering the first line in this function ( ListItem front = head ) points the front to the first element of the list, it seems that by calling: front = front.next; current = front.next; front = front.next; current = front.next; you actually 'skip' the element at index 1 in the list and start your comparison loop of the element at index 2.

For example, if your (unsorted) list looks like this:

[54, 11, 25, 34]

It will look like

[25, 11, 54, 34]

after the first iteration of your sorting algorithm. Since the next iteration will start at index 1, the element 11 will never be placed at index 0 even though it is the lowest element in the list.

It might be this inaccuracy which causes the null pointer problem at the end of the list. I would consider putting the statement front = front.next; after the inner for loop and before the swap(a, lowestIndex); statement. This will prevent the possible error in the first iteration and might solve your problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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