java编程,对给定一整型数组,用冒泡排序算法按从大到小的顺序整序后输出

要经得起考验的(能编译运行的) 打酱油的就算了哈
2025-01-02 08:48:11
推荐回答(1个)
回答1:

public class Test
{
public static void main(String args[])
{
int[] arr = {4, 1, 2, 6, 3, 8, 9, 10, 7, 5};

for (int i = 0; i < 10; i++)
System.out.print(arr[i] + " ");
System.out.println();

for (int i = 0; i < 10 - 1; i++)
for (int j = 0; j < 10 - 1 - i; j++)
{
if (arr[j] > arr[j + 1])
{
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
for (int i = 0; i < 10; i++)
System.out.print(arr[i] + " ");
}
}