//对给定数组进行排序并打印。{5,1,6,4,2,8,9}
class ArrayTest2
{
public static void selectSort(int[] arr)
{
for (int x = 0;x<arr.length-1 ;x++ )
{
for (int y = x+1;y < arr.length ;y++ )
{
if (arr[x]>arr[y])
{
int temp = arr[x];
arr [x] =arr[y];
arr [y] = temp;
}
}
}
}
public static void main(String[] args)
{
int[] arr = {5,1,6,4,2,8,9};
printArray(arr);
selectSort(arr);
printArray(arr);
}
public static void printArray( int[] arr)
{
for (int x = 0;x<arr.length ;x++)
{
if (x == arr.length-1)
{
System.out.println(arr[x]);
}
else
System.out.print(arr[x]+";");
}
}
} |
|