JAVA冒泡排序

来源:互联网 发布:淘宝客api开发 编辑:程序博客网 时间:2024/06/05 06:38

package com.opensource.sort;

import java.util.Random;

/**
 *
 * @author Cache John
 * @email
550595698@qq.com
 *
 */
public class BubbleSort
{
    /**
     * 冒泡排序
     * @param sort
     */
    private static void buddleSort(final int[] sort)
    {
        for (int i = 0; i < sort.length; i++)
        {
            for (int j = i + 1; j < sort.length - 1; j++)
            {
                if (sort[i] > sort[j])
                {
                    int temp = sort[j];
                    sort[j] = sort[i];
                    sort[i] = temp;
                }
            }
        }
    }
   
    /**
     * @param args
     */
    public static void main(String[] args)
    {
        // 生成随即整数的数组
        Random random = new Random();
        int[] sort = new int[5];
        for (int i = 0; i < 5; i++)
        {
            sort[i] = random.nextInt(100);
        }
       
        // 生成随即数组的数据
        System.out.println("排序前的数组为:");
        for (int i : sort)
        {
            System.out.println(i + "");
        }
       
        // 排序之后的顺序
        buddleSort(sort);
        System.out.println();
        System.out.println("排序后的数组为:");
        for (int i : sort)
        {
            System.out.println(i + "");
        }
       
    }
   
}

原创粉丝点击