中心思想:把一个数与没有排序的数进行逐个比较,直到最后一个数为止,然后把最值与首元素进行位置互换。
class HomeWork2 {
public static void main(String[] arg)
{
int[] scores = {6,19,7,28,1,9}; // 定义一个数组
for(int i = 0;i < scores.length; i++) // 外循环,循环的是每个数
{
for(int j = i + 1; j < scores.length; j++) // 第i个数与其他没有排序的数进行比较
{
if(scores[ j ] < scores [ i ]) // 条件判断
{
int temp = scores[ i ];
scores[ i ] = scores[ j ];
scores[ j ] = temp;
}
for(int value : scores) // 遍历数组
{
System.out.println(value);
}
}
}