采用冒泡排序对如下数组排序
来源:学生作业帮 编辑:大师作文网作业帮 分类:综合作业 时间:2024/10/10 09:25:07
采用冒泡排序对如下数组排序
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]);
}
}
}
代码如下:
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]);
}
}
}
用冒泡排序法对偶数下标的数组元素进行升序排列用选择排序法对奇数下标的数组元素进行降序排列
VB作业 利用“冒泡法”对一下数据进行从小到大排序:7、3、9、12、20、8(数组)
C语言用冒泡排序法对数组中的8个整数(a[8]={6,8,5,4,6,9,3,2})进行从小到大排序
什么是冒泡排序法?
已知序列(25,16,32,18,20,36,12)请给出采用冒泡排序对该序列做升序排序时的每一趟结果
冒泡排序法是如何排序的?
编写程序,定义数组存放如下数据{1,4,5,3,2,0},并对该数组按照由小到大顺序排序,输出排序后的结果
对n个数排序:使用选择法和冒泡法
对N个元素进行排序,用冒泡法进行排序时,共需排几次?
数组排列的冒泡排序法,有谁能帮我找出错误啊
Java 用冒泡法,把数组按升序排序{37,47,23,-5,19,56}
java 数组字母排序