-
使用IDEA开发一个Java项目 ArrayTest,创建一个类Mytools编写一个方法。可以完成对int数组冒泡排序的功能
public class Hello {
public static void main(String[] args){
int arr[] = {1,3,7,4,6};
MyTools tools = new MyTools();
tools.buddle(arr);
System.out.println("====排序后的结果是===");
for(int i = 0; i < arr.length;i++){
System.out.print(arr[i]+"\t");
}
}
}
class MyTools{
public void buddle(int arr[]){
int type = 0;
for(int i = 0; i < arr.length-1; i++){
for(int j = 0;j < arr.length-1-i;j++){
if(arr[j] > arr[j+1]){
type = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = type;
}
}
}
}
}
运行结果: