冒泡排序

来源:互联网 发布:二维平移变换矩阵 编辑:程序博客网 时间:2024/06/16 02:33
class Sort
{
public static void main(String[] args) 
{
int[] arr={5,1,6,4,2,8,9};
printArray(arr);

bubbleSort(arr);
printArray(arr);
}
public static void bubbleSort(int[] arr)
{
for (int x=0;x<arr.length-1 ;x++ )
{
for (int y=0;y<arr.length-x-1 ; y++)
{
if (arr[y]>arr[y+1])
{
int temp=arr[y];
arr[y]=arr[y+1];
arr[y+1]=temp;
}
}
}
}
public static void printArray(int[] arr)
{
System.out.print("[");
for (int x=0;x<arr.length ; x++)
{
if(x!=arr.length-1)
System.out.print(arr[x]+", ");
else
System.out.println(arr[x]+"]");
}
}
}
0 0
原创粉丝点击