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