给定一个整数数组a,请实现一个排序算法,将该数组从大到小排列并输出

2024-11-23 11:26:17
推荐回答(2个)
回答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] + " ");
}
}

回答2:

#include
# define N 100
void main ()
{
int i,j,t,n,a[N];
printf("请输入数组元素个数n:");
scanf("%d",&n);
printf("请输入%d个数:",n);
for(i=0;i scanf("%d",&a[i]);
for(i=0;i for(j=i+1;j if(a[i] {
t=a[i];
a[i]=a[j];
a[j]=t;
}
for(i=0;i printf("%-5d",a[i]);
}