Java排序算法之冒泡排序

package com.BubbleSort.hql;

public class BubbleSort
{
    public static void main(String[] args)
    {
        int[] arr = new int[]
        { 12, 32, 16, 35, 46, 98, 5 };
        BubbleSort(arr);
        for (int i = 0; i < arr.length; i++)
        {
            System.out.print(arr[i]+" ");
        }
    }

    public static void BubbleSort(int[] arr)
    {
        int temp;
        for (int i = 0; i < arr.length - 1; i++)
        {
            //-i:每次循环结束之后,少进行一次排序,因为最大的那个已经查找出来,
            //-1:防止下标越界
            for (int j = 0; j < arr.length - i - 1; j++)
            {
                if (arr[j] > arr[j + 1])
                {
                    temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                }
            }
        }
    }
}
posted @ 2014-07-19 11:05  loneliness__白色  阅读(96)  评论(0)    收藏  举报