采用冒泡排序对如下数组排序String[] arr = { "Now","is","the","time","for","all","good","men","to","come","to","the","aid","of","their","country"};

来源:学生作业帮助网 编辑:作业帮 时间:2024/05/07 18:17:52
采用冒泡排序对如下数组排序String[] arr = {

采用冒泡排序对如下数组排序String[] arr = { "Now","is","the","time","for","all","good","men","to","come","to","the","aid","of","their","country"};
采用冒泡排序对如下数组排序
String[] arr = { "Now","is","the","time","for","all","good","men",
"to","come","to","the","aid","of","their","country"
};

采用冒泡排序对如下数组排序String[] arr = { "Now","is","the","time","for","all","good","men","to","come","to","the","aid","of","their","country"};
看楼主的写的代码猜测是用Java语言写的,我写了一个Java小程序,文件名是Sort.java
代码如下:
public class Sort {
public static void sort(String[] s) {
for(int i = 0; i < s.length - 1; i++) {
String temp;
for(int j = 0; j < s.length - i - 1; j++) {
if(s[j].compareTo(s[j + 1]) > 0) {
temp = s[j + 1];
s[j + 1] = s[j];
s[j] = temp;
}
}
}
}

public static void main(String[] args) {
String[] arr = { "Now", "is", "the", "time", "for", "all", "good", "men",
"to", "come", "to", "the", "aid", "of", "their", "country"
};
sort(arr);
for(int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
}
}