public class Test {
public void test() {
int test[] = {49,38,65,97,76,13,27,49,78,34,12,64,5,4,62,99,98,54,101,56,17,18,23,34,15,35,25,53,51};
//int test[] = {5,4,3,2,1};
Bubble bubble = new Bubble();
bubble.sort(test);
for( int i=0;i<test.length;i++ ) {
System.out.format("%d ", test[i]);
}
}
public class Bubble {
public void sort(int[] a) {
/*1、JAVA没有指针,无法在函数内交换两个值
* 2、冒泡排序的思想就是循环比较相邻的元素并交换,直到最后一次循环发现没有交换为止
* 3、冒泡排序算法简单,效率较低*/
int i=0;
int j=0;
int cnt=0;
do {
cnt = 0;
for(i=0,j=1;i<a.length-1;i++,j++) {
if( a[i] > a[j] ) {
int tmp = a[i];
a[i] = a[j];
a[j] = tmp;
cnt++;
}
}
}while(cnt!=0);
}
}
}
【代码积累-3】bubble sort
最新推荐文章于 2021-05-20 10:46:24 发布