在min方法中定义好数组
public class BubbleSortTest
{
public static void main (String[] args)
{
int [] array = {};
}
}
定义静态方法
public class BubbleSortTest
{
public static void bubblesort(int [] array0)
{
}
public static void main (String[] args)
{
int [] array = {};
}
}
在静态方法中进行for循环
public class BubbleSortTest
{
public static void bubblesort(int [] array0)
{
for (int i = 0;i<array.length - 1;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;
}
}
}
}
public static void main (String[] args)
{
int [] array = {8,4,3,2,5}
}
}
为了更清楚的看清每次循环的过程,我特意加了如下代码:
public class BubbleSortTest
{
public static void bubblesort(int [] array0)
{
for (int i = 0;i<array.length - 1;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;
}
}
System.out.println("第" +(i + 1)+ "回排序" );
for(int m = 0 ;m<array.length; m++)
{
System.out.print(array[m] + " ");
}
System.out.println();
}
}
public static void main (String[] args)
{
int [] array = {8,4,3,2,5}
bubbleSort(array);
}
}