各位大虾看看我这个冒泡排序程序(java)错在哪里??

tandyong 2008-03-29 09:05:14
package test1;

public class BubbleSort {
static String sortArray(int before[]){
String result="";
for(int i=0;i<before.length;i++){
result+=before[i]+" ";
}
return result;
}
static int[] bubbleSort(int before[]){
int t;
boolean b=true;
for(int i=before.length-1;i>0&&b==true;i++){
b=false;
for(int j=0;j<i;j++){
if(before[j]>before[j+1]){
t=before[j];
before[j]=before[j+1];
before[j+1]=t;
b=true;
}
}

}
return before;
}
public static void main(String args[]){
int a[]={12,43,23,56,8,22,65,87};
System.out.println("Before Sorting:"+sortArray(a));
a=bubbleSort(a);
System.out.println("After Sorting:"+sortArray(a));
}
}
运行报错:
Before Sorting:12 43 23 56 8 22 65 87
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 8
at test1.BubbleSort.bubbleSort(BubbleSort.java:17)
at test1.BubbleSort.main(BubbleSort.java:31)
...全文
105 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
love1907 2008-03-30
  • 打赏
  • 举报
回复
楼上
JOJU123 2008-03-30
  • 打赏
  • 举报
回复
冒泡还算简单````做快排时比较伤脑筋```堆排也是
  • 打赏
  • 举报
回复
自问自答??呵呵。
tandyong 2008-03-29
  • 打赏
  • 举报
回复
呵呵,知道错在哪里了哟,
for(int i=before.length-1;i>0&&b==true;i++)
应该是
for(int i=before.length-1;i>0&&b==true;i--)

62,623

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧