class BubbsortDemo1
{
public static void main(String[] args)
{
System.out.println("冒泡前");
int [] arr={12,3,24,42,15,52,56,6};
printArray(arr);
System.out.println("冒泡后");
bubbSort(arr);
printArray(arr);
}
public static void bubbSort(int []arr )
{
for (int x=0;x<arr.length-1;x++)
{
for(int y=0;y<arr.length-1-x;y++)
if(arr[y]>arr[y+1])
{
int tem=arr[y];
arr[y]=arr[y+1];
arr[y+1]=tem;
}
}
}
public static void printArray(int []arr)
{
System.out.print("[");
for(int x=0;x<arr.length;x++)
{
if(x==arr.length-1)
{
System.out.println(arr[x]+"]");
}
else
System.out.print(arr[x]+",");
}
}
}
|