冒泡排序

来源:互联网 发布:网络推广吧 编辑:程序博客网 时间:2024/06/11 01:12
public class BubbleSort {    public static void Sort(int a[])    {        int len = a.length;        int temp = 0;        for(int i=0;i<len-1;i++)        {            for(int j=0;j<len-1-i;j++)            {                if(a[j]>a[j+1])                {                    temp=a[j];                    a[j]=a[j+1];                    a[j+1]=temp;                }            }        }    }    public static void print(int a[])    {        for(int x : a)            System.out.print(x + " ");        System.out.println();    }    public static void main(String[] args)    {        int a[] = {13, 24, 56, 8, 90, 6, 22};        BubbleSort.print(a);        BubbleSort.Sort(a);        BubbleSort.print(a);    }}

样例输出:
13 24 56 8 90 6 22
6 8 13 22 24 56 90

0 0
原创粉丝点击