Blog

It's a Wonderful Life

用java实现冒泡排序

2017-04-27 11:37 Posted in Learn with Java , algorithm

package com.leo.util;

public final class BubbleSort
{
    private BubbleSort()
    {
        throw new AssertionError();
    }
    
    public static int[] sort_int(int[] arr)
    {
        int len = arr.length;
        boolean done = false;
        for (int i = 0; i < len && ! done; i++)
        {
            done = true;
            for (int j = 0; j < len - 1; j++)
            {
                if (arr[j] > arr[j + 1])
                {
                    int temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                    done = false;
                }
            }
        }
        
        return arr;
    }
}