class ArrayDemo{
public static void main(String[] args) {
int[] arr= {89,34,-270,17,3,100};
System.out.print("排序前数组:" );
printArray(arr);
bubbleSort(arr);
System.out.print("排序后数组:" );
printArray(arr);
}
public static void bubbleSort(int[] arr){
for(int x = 0; x < arr.length - 1; x++){
for(int y = 0; y < arr.length - 1 -x; 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] + "]" );
}
}
}
|
|