public Node get(int i) {
if (!isEmpty()) {
int j = 0;
Node element = head;
while (j++ < i) {
element = element.getNext();
if (element == null)
return null;
}
return element;
}
return null;
}
private void bubbleSort() {
for (int i = 0; i < size - 1; i++) {
boolean changed = false;
for (int j = 0; j < size - i - 1; j++) {
if (get(j + 1) != null) {
if (get(j).getValue() > get(j + 1).getValue()) {
//System.out.println("Swapping: " + get(j).getValue() + " : " + get(j + 1).getValue());
swap(get(j), get(j + 1));
changed = true;
}
}
}
if (!changed)
return;
}
}
public void swap(Node first, Node mid) {
if (first == head)
head = mid;
if (mid == tail) {
tail = first;
}
first.setNext(mid.getNext());
mid.setPrev(first.getPrev());
first.setPrev(mid);
mid.setNext(first);
}
I dont know what is missing…
List, value:
6
2
4
5
7
After sort:
2
6
7
Auxiliar outputs:
Swapping: 6 <> 2
Swapping: 6 <> 4
Swapping: 6 <> 5
Please, some on help-me, I don’t know sort a double linked list…..
public void swap(Node first, Node mid) {
if (first == head)
head = mid;
if (mid == tail) {
tail = first;
}
first.setNext(mid.getNext());
mid.setPrev(first.getPrev());
first.setPrev(mid);
mid.setNext(first);
}
See the code following : swap function.
Since this is appears to be a homework question I will not give you the full answer. I am fairly positive that the problem lies in your swap function. Consider drawing the nodes on paper and going through your swap function and seeing what happens to which nodes pointing where. Consider the previous and next nodes of the nodes you are swapping and that you have to update their previous/next as well! Unless of course your setNext and setPrev functions already do that.
Feel free to comment if you need additional help.
Edit 1:
Also, as a side note, rather than using indices (i and j) to keep track of which node you’re using, consider just using the node itself. And then when you call i++ or j++, call node = node.getNext() instead. This way you aren’t adding an additional factor of n to your runtime, and you can eliminate your swap function altogether.
Edit 2:
What I mean by drawing it out on paper is write each of the values out on paper, in order, horizontally. Then draw a circle around each one and an arrow pointing to the next circle and the previous circle in each case. Draw the arrows in pencil! Then update the arrows accordingly as you step through your swap function. You don’t even have to move/erase the circles, just erase the arrows and see where they point next.
Edit 3:
Okay so you’ve got two circles that you want to swap. These circles both have arrows going outwards, and their neighbors both have arrows going inwards. That’s 4 arrows per circle (2 in, 2 out, unless you’re dealing with the head/tail). Since you are moving 2 circles, that means you have to change a total of 8 arrows (unless dealing with head/tail) – 4 per circle. You’re probably only changing one arrow per setPrev/setNext. Which implies that these should usually be called 8 times in your swap function. You’re only calling them 4 times.
Edit 4:
You are updating the prev/next of the swapped nodes, but not of their neighbors. You’re going to want to do something like first.getNext().setPrev(second), etc. to update the references/prev/next/arrows of the neighbors.
Edit 5:
Keep in mind null checks though (you can’t call setPrev or setNext on a null, so make sure when you set the neighbor’s prev/next that the neighbor actually exists (because prev doesn’t exist for head and next doesn’t exist for tail)).
Edit 6:
I guess if you say you aren’t doing homework… 😉