Java 气泡排序(单链表)错误
我正在尝试使用冒泡排序对单个链接列表进行排序。当我运行代码时,它会对int和double列表进行排序。奇怪的是,当它对字符串列表进行排序时,除了一个元素之外,它对所有元素都进行了排序。我不知道为什么会这样。这是我得到的输出 246394335507384532 2 7 32 35 38 39 43 45 46 50 2.0 7.0 32.0 35.0 38.0 39.0 43.0 45.0 46.0 50.0 2 32 35 38 39 43 45 46 50 7Java 气泡排序(单链表)错误,java,singly-linked-list,bubble-sort,Java,Singly Linked List,Bubble Sort,我正在尝试使用冒泡排序对单个链接列表进行排序。当我运行代码时,它会对int和double列表进行排序。奇怪的是,当它对字符串列表进行排序时,除了一个元素之外,它对所有元素都进行了排序。我不知道为什么会这样。这是我得到的输出 246394335507384532 2 7 32 35 38 39 43 45 46 50 2.0 7.0 32.0 35.0 38.0 39.0 43.0 45.0 46.0 50.0 2 32 35 38 39 43 45 46 50 7 import java.uti
import java.util.Random;
public class SLLBubbleSort{
public static void main(String[] args){
Random rand = new Random();
SLL<Integer> sll1 = new SLL<Integer>();
SLL<Double> sll2 = new SLL<Double>();
SLL<String> sll3 = new SLL<String>();
for(int i=0;i<10;i++){
int val = rand.nextInt(50)+1;
sll1.addToHead(val);
sll2.addToHead((double)val);
sll3.addToHead(Integer.toString(val));
}
sll1.printAll();
System.out.println("");
BubbleSort(sll1);
sll1.printAll();
System.out.println("");
BubbleSort(sll2);
sll2.printAll();
System.out.println("");
BubbleSort(sll3);
sll3.printAll();
}
public static <T extends Comparable<? super T>> void BubbleSort(SLL<T> list){
for(int i=0;i<list.getLength();i++){
SLLNode<T> current = list.head;
SLLNode<T> next = current.next;
for(int j=0;j<list.getLength()-1;j++){
if(current.info.compareTo(next.info)>0){
T temp = current.info;
current.info = next.info;
next.info = temp;
}
current = next;
next = next.next;
}
}
}
}
import java.util.Random;
公共类SLLBubbleSort{
公共静态void main(字符串[]args){
Random rand=新的Random();
SLL sll1=新的SLL();
SLL sll2=新的SLL();
SLL sll3=新的SLL();
对于(int i=0;i而言,没有任何错误。请注意字符串
“7”大于“50”。你看它们就像看数字一样,但它们不是。结果很好,不要认为它们是数字,而要认为它们是字符串,因为ascii值0首先出现,然后是1、2、3…7、8、9、A、B、C…X、Y、Z、A、B、C…X、Y、Z
请参阅此ASCII表将SSL类的代码放在末尾。您可能担心的重复项是关于“7”?它在字典上比所有其他元素都大,这就是为什么它位于末尾