引用了12616383的冒泡排序,我对其进行了改进~呵呵,下面是原文章链接!!
[url]http://www.iteye.com/topic/552814[/url]
[url]http://www.iteye.com/topic/552814[/url]
class TSort{
private long[] a;
public TSort(long[] array){
this.a = array;
}
public static void main(String[] args) {
long[] array = new long[]{5,3,3,7,9,2,0};
TSort b = new TSort(array);
b.bubbleSort();
System.out.println(b);
}
public void bubbleSort(){
int out,in;
int step = 0;
System.out.println("bubble---");
for(out = a.length -1 ; out > 0; out --){
for(in = 0; in < out; in++){
if(a[in+1] < a[in]){
long temp = a[in];
a[in] = a[in+1];
a[in+1] = temp;
}
step++; //冒泡的比较的步骤
System.out.println("step:"+step);
for(int i=0;i<a.length;i++){
System.out.print(a[i]); //输出每步比较后的数组
}
System.out.println();
}
}
}
//重写toString方法
public String toString(){
StringBuilder b = new StringBuilder();
for(long l : a){
b.append(l);
b.append(" ");
}
return b.toString();
}
}