Hi guys,
What's wrong with my code? I have a Link class and a Linked List class. The Link class contains one random number. The Linked List class creates a new list. The size of that list is up to the user of the program. With a methode calles insertMany(int howMany) the user can insert x-links in the list as many as you like. The follow code is my bubblesort.
But my question the code sometimes sorts the list very well talking about 50000 links. Sometimes it stucks in a loop or the sorting takes longer as usual. If i enter lets say 20000 it always works but i need to get to 100000 and is not working so what can i change and what is wrong with my code?
public void bubbleSort(){ Link current = first; int index = 0; while(current != null && index <= howMany){ if(current.next == null){ index++; current = first; } else if(current.random < current.next.random){ current = current.next; } else if(current.random > current.next.random){ swap(current, current.next); } } }
public void swap(Link c, Link cn){ long temp = c.getValue(); c.setValue(cn.getValue()); cn.setValue(temp); }