我尝试使用冒泡排序来排序数组,但每次迭代后都缺少最后一个索引。
以下是我的Java代码,请帮忙。
for i=1:n
mystruct(i).z = mystruct(i).x + mystruct(i).y;
end
编译后程序的输出
package arraytest;
public class BubbleSort {
public static void main(String[] args) {
int [] bblSort = {30, 45, 8, 204, 165, 95, 28, 180, 110, 40};
for(int i=0; i<bblSort.length; i++){
System.out.print(" " +bblSort[i]);
}
System.out.println();
System.out.println(" ..........");
sort(bblSort);
}
public static void sort(int [] bblSort){
int temp=0;
for(int i=0; i<bblSort.length-1; i++){
for(int j=0; j<bblSort.length-1 -i; j++){
if(bblSort[j] > bblSort[j+1]){
temp = bblSort[j];
bblSort[j] = bblSort[j + 1];
bblSort[j+1] = temp;
}
System.out.print(" " +bblSort[j]);
}
System.out.println();
}
}
}
答案 0 :(得分:1)
您的实施没有任何问题。只需在排序后再次输出bblSort
的内容,您就会看到它如下所示:
[8, 28, 30, 40, 45, 95, 110, 165, 180, 204]