冒泡排序

来源:互联网 发布:戈仑石人升级数据 编辑:程序博客网 时间:2024/06/06 08:46

思路:

        第一步:将第一个数和第二个数进行比较,如果大,则交换;

        第二部:将第二个数和第三个数进行比较,如果大,则交换;

       第N步:帅选出最大的一个数,然后从剩下的数中按照上面的方法反复操作,得到需要的序列

   

代码:

 public class BubbleSort {    public int[] Sort(int[] array)    {        for(int i = 0; i < array.length; i++)        {            for(int j = 0; j < array.length - i - 1; j++)            {                if(array[j] > array[j + 1])                {                    int temp = array[j];                                            array[j] = array[j + 1];                                        array[j + 1] = temp;                }            }        }        return array;    }    public static void main(String[] args)    {        int[] array = new int[]{8,6,7,1,2,4,5,3,9,0};                BubbleSort bubbleSort = new BubbleSort();                int[] arrays = bubbleSort.Sort(array);                for(int i = 0; i < arrays.length; i++)        {            System.out.print(array[i] + " ");        }    }}
排序结果为:

                    0,1,2,3,4,5,6,7,8,9


1 0
原创粉丝点击