Skip to main content

Sorting a linked list in Java


I have written a bubble sort algorithm to sort a linked list. I am a Java beginner and trying to learn data structures. I am confused why my second element is not sorted properly.



EDIT




class SListNode {
Object item;
SListNode next;


SListNode(Object obj) {
item = obj;
next = null;
}


SListNode(Object obj, SListNode next) {
item = obj;
this.next = next;
}

}
public class SList {

private SListNode head;
private SListNode temp;

public void sortList() {
SListNode node = head,i,j;
head = node;
i = node;
j = node.next;
while(i.next != null) {
while(j.next != null) {
if((Integer)i.item < (Integer)j.item) {
temp = i.next;
i.next = j.next;
j.next = temp;
}
j = j.next;
}
i = i.next;
}
}
}



This is the output I am getting




List after construction: [ 3 6 9 4 12 15 ]
After sorting: [ 3 4 9 12 6 15 ]



Besides I know the worst case scenario of a bubble sort is O(n 2 ). Can I use mergesort on a linked list to have a better time complexity?



Thanks!


Source: Tips4allCCNA FINAL EXAM

Comments

  1. There are many sorting algorithms that work on linked lists and mergesort works excellently in this case. I wrote an earlier answer to a question about sorting linked lists that explores many classic sorting algorithms on linked lists, along with their time and space complexities. You can use insertion sort, selection sort, mergesort, and quicksort on linked lists. With a bit of fudging, you can also get heapsort working. My older answer has details on how to do this.

    With regards to your code, notice that in your inner loop you advance j forward until the next pointer becomes null. At this point, you never reset j to be anything else, so on each future iteration of the outer loop the inner loop never executes. You should probably set j = i.next at the start of each iteration. Moreover, you probably don't want to have the loop stop when j.next is null, but rather when j is null, since otherwise you skip the last element of the array.

    Additionally, the sorting algorithm you've written here is selection sort rather than bubble sort, because you're making many passes over the linked list looking for the smallest element that you haven't positioned yet. I don't know if this is a problem or not, but I wasn't sure if you were aware of this. That said, I think that's probably a good thing, since bubble sort is less efficient than selection sort in most cases (unless the list is already close to being sorted).

    Hope this helps!

    ReplyDelete
  2. Without directly answering, the way to investigate would be to System.out.println() your list after every swap and after each outer loop to see what is happening.

    ReplyDelete

Post a Comment

Popular posts from this blog

[韓日関係] 首相含む大幅な内閣改造の可能性…早ければ来月10日ごろ=韓国

div not scrolling properly with slimScroll plugin

I am using the slimScroll plugin for jQuery by Piotr Rochala Which is a great plugin for nice scrollbars on most browsers but I am stuck because I am using it for a chat box and whenever the user appends new text to the boxit does scroll using the .scrollTop() method however the plugin's scrollbar doesnt scroll with it and when the user wants to look though the chat history it will start scrolling from near the top. I have made a quick demo of my situation http://jsfiddle.net/DY9CT/2/ Does anyone know how to solve this problem?

Why does this javascript based printing cause Safari to refresh the page?

The page I am working on has a javascript function executed to print parts of the page. For some reason, printing in Safari, causes the window to somehow update. I say somehow, because it does not really refresh as in reload the page, but rather it starts the "rendering" of the page from start, i.e. scroll to top, flash animations start from 0, and so forth. The effect is reproduced by this fiddle: http://jsfiddle.net/fYmnB/ Clicking the print button and finishing or cancelling a print in Safari causes the screen to "go white" for a sec, which in my real website manifests itself as something "like" a reload. While running print button with, let's say, Firefox, just opens and closes the print dialogue without affecting the fiddle page in any way. Is there something with my way of calling the browsers print method that causes this, or how can it be explained - and preferably, avoided? P.S.: On my real site the same occurs with Chrome. In the ex