在数组的排序程序中,随机产牛20个整数, 对其按照升序进行排列,
对其按照升序进行排列,并对排序前
后的数组按照每行5个数的方式输出。 展开
packageHello;
publicclassBackup2{
publicstaticvoidmain(String[]arguments){
inti=(int)(Math.random()*12+1);
Stringstr="";
System.out.println(i);
Stringmonth[]=newString[]{"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
switch(i){
case1:str=month[0];break;
case2:str=month[1];break;
case3:str=month[2];break;
case4:str=month[3];break;
case5:str=month[4];break;
case6:str=month[5];break;
case7:str=month[6];break;
case8:str=month[7];break;
case9:str=month[8];break;
}
System.out.println(str);
}
}
扩展资料
利用Arrays带有的排序方法快速排序
import java.util.Arrays;
public class Test2{
public static void main(String[] args){
int[] a={5,4,2,4,9,1};
Arrays.sort(a); //进行排序
for(int i: a){
System.out.print(i);
}
}
}
#include <time.h>
#include <stdlib.h>
int
main()
{
int a[20], m;
srand((unsigned int)time(NULL));
for (int i = 0; i < 20; i ++)
a[i] = rand() % 100;
for (int j = 0; j < 20; j ++)
for (int k = 0; k < 19; k++)
if (a[k]> a[k + 1])
{
int tm = a[k];
a[k + 1] = a[k];
a[k] = a[k + 1];
}
for (m = 0; m < 20; m ++)
{
if (m != 0 && m % 5 == 0)
putchar('\n');
printf("%d ", a[m]);
}
return 0;
}