java中数组int[] num = new int[]{3,2,7,5,9,1};实现从大到小冒泡排序

求大神给答案代码,完整点……
2025-03-22 09:09:23
推荐回答(3个)
回答1:

刚才花了2分钟写了一下,可以满足你的要求,而且我写代码用了递归,应该算是比较精简了。你直接新建一个文件把代码拷过去就可以看到效果了。


public class SortTest {
public static void main(String[] args) {
//测试数组
int[] num  = new int[]{3,2,7,5,9,1,300};
System.out.print("排序前的位置:");
for (int i : num) {
System.out.print(i+"-");
}
System.out.println();
System.out.print("排序后的位置:");
sortMaxToMin(num);//调用排序方法
for (int i : num) {
System.out.print(i+">");
}
}
//将数组从大到小排序
public static void sortMaxToMin(int[] arrs){
for(int i=0;i if(arrs[i] int s = arrs[i];
arrs[i]=arrs[i+1];
arrs[i+1]=s;
sortMaxToMin(arrs);//换过位置以后 使用递归进行处理 换位置。直到当前位置满足
}
}
}
}

回答2:

楼主你好,下面是代码
public class test_1_8 {
public static void main(String[] args) {
test_1_8 sort = new test_1_8();
int[] arr = new int[]{3,2,7,5,9,1};
sort.sort(arr);
for(int i : arr){
System.out.print(i+",");
}
}
public void sort(int[] targetArr){//大到小的排序
int temp = 0;
for(int i = 0;i for(int j = i;j if(targetArr[i] temp = targetArr[i];
targetArr[i] = targetArr[j];
targetArr[j] = temp;
}
}
}
}
}

回答3:

public class BubbleSortTest{
public static void main(String[] args) {
int[] num = new int[]{3,2,7,5,9,1};
BubbleSort(num);
printarr(num);
}
public static void BubbleSort(int[] arr){
for(int x=0;x for(int y=0;y if(arr[y] int temp=arr[y];
arr[y]=arr[y+1];
arr[y+1]=temp;
}
}
}
}
public static void printarr(int[] arr){
for(int x=0;x System.out.print(arr[x]+" ");
}
}
}