import java.util.Arrays;
public class MaoPao {
<span style="white-space:pre"> </span>public static void main(String[] args) {
<span style="white-space:pre"> </span>int[] a = {5,4,123,5,67,8};
<span style="white-space:pre"> </span>BubbleSort(a);
<span style="white-space:pre"> </span>System.out.println(Arrays.toString(a));
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>static void BubbleSort(int a[]) {
<span style="white-space:pre"> </span>int temp = 0;
<span style="white-space:pre"> </span>for (int i = 0; i < a.length; i++) {
<span style="white-space:pre"> </span>for (int j = 0; j < a.length - i - 1; j++) {
<span style="white-space:pre"> </span>if (a[j] > a[j + 1]) { // 把这里改成大于,就是升序了
<span style="white-space:pre"> </span>temp = a[j];
<span style="white-space:pre"> </span>a[j] = a[j + 1];
<span style="white-space:pre"> </span>a[j + 1] = temp;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>}
}
【Java】冒泡排序
最新推荐文章于 2023-06-06 09:14:05 发布