数组——冒泡排序2

冒泡排序规则:元素两两比较,大的往后排,一次比较结束,最大值出现在了最大索引处。

package sort;

import java.util.Arrays;

public class BubbleSort {
    public static void main(String[] args) {
        int[] ary={101,99,89,108,77,32,44};
        System.out.println(Arrays.toString(ary));
        for (int i = 0; i < ary.length-1; i++) {
            if(ary[i]>ary[i+1])
            {
                int tmp=ary[i];
                ary[i]=ary[i+1];
                ary[i+1]=tmp;
            }
        }
        System.out.println("一趟冒泡排序后");
        System.out.println(Arrays.toString(ary));
        System.out.println("正常的冒泡排序");
        for(int i=0;iary[j+1])
                {
                    int tmp=ary[j];
                    ary[j]=ary[j+1];
                    ary[j+1]=tmp;
                }
            }
        }
        System.out.println(Arrays.toString(ary));
    }

}

运行结果

[101, 99, 89, 108, 77, 32, 44]
一趟冒泡排序后
[99, 89, 101, 77, 32, 44, 108]
正常的冒泡排序
[32, 44, 77, 89, 99, 101, 108]

你可能感兴趣的:(数组——冒泡排序2)