📄 bubblesort.java
字号:
public class BubbleSort{
public static void main(String args[]){
int array[] = {6,1,8,2,12,32, 19,53,30,21};
for(int i=0; i<array.length; i++) { // 原始顺序输出
System.out.print(array[i] + " , ");
}
BubbleSort b = new BubbleSort();
int[] result = b.bubble(array); // 调用排序函数
System.out.println();
for(int i=0; i<result.length; i++) {
System.out.print(result[i] + " , ");
} System.out.print("\n");
}
int[] bubble(int a[]) { // 冒泡排序
int temp,size = a.length;
for(int i=size-1; i>=1; i--) // 外层循环,控制"冒泡"的最终位置
{ for(int j=0; j < i; j++) // 内层循环,用于相临元素的比较
{ if(a[j] > a[j+1])
{
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
}
return a;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -