public class bubble {
public static void main(String[] args) {
int i,j,temp;
int[] arr = {38, 24, 15, 62, 49};
System.out.println("Primitive array:");
for (i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
System.out.println("\nThe length of array:");
System.out.println(arr.length);
//外层控制比较的轮数,内层循环用于控制每轮比较的次数
for (i = 0; i < arr.length-1; i++) {
for(j = 0; j <arr.length-1-i ; j++) {
if (arr[j] < arr[j+1]) {
temp=arr[j+1];
arr[j+1]=arr[j];
arr[j]=temp;
}
}
}
System.out.println("The array after bubble sorting:");
for (i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
}
}
冒泡排序法 数组 降序
最新推荐文章于 2024-10-31 15:56:11 发布