日期:2014-05-17 浏览次数:20899 次
public class Test2
{
public static void main(String[] args)
{
int[] data = { 24, 35, 12, 27, 18, 26 };
for (int i = 1; i < data.length; i++)
{
for (int j = i; j > 0; j--)
{
if (data[j] < data[j - 1])
{
int temp = data[j];
data[j] = data[j - 1];
data[j - 1] = temp;
}
}
for (int m = 0; m < data.length; m++)
{
System.out.print(data[m] + " ");
}
System.out.println();
}
System.out.println("------------------------------");
int[] data1 = { 24, 35, 12, 27, 18, 26 };
int temp = 0;
for (int i = 1; i < data1.length; i++)
{
int j = i - 1;
temp = data1[i];
for (; j >= 0 && temp < data1[j]; j--)
{
data1[j + 1] = data1[j]; // 将大于temp的值整体后移一个单位
}
data1[j + 1] = temp;
for (int m = 0; m <data1.length; m++)
{
System.out.print(data1[m]+" ");
}
System.out.println();
}
}
}
24 35 12 27 18 26
12 24 35 27 18 26
12 24 27 35 18 26
12 18 24 27 35 26
12 18 24 26 27 35
------------------------------
24 35 12 27 18 26
12 24 35 27 18 26
12 24 27 35 18 26
12 18 24 27 35 26
12 18 24 26 27 35