/**
* 产生25个的int型随机数,并将其进行从小到大排序。
* @author Administrator
*
*/import java.util.Random;public class RandInt {
public static void main(String args[]) {
int []rand=new int[25];
Random random=new Random();
for(int i=0;i<25;i++) {
rand[i]=random.nextInt();
} for(int i=1;i<25;i++) {
int temp=0;
if(rand[i-1]>rand[i]) {
temp=rand[i-1];
rand[i-1]=rand[i];
rand[i]=temp;
}
} int count=0;
for(int i=0;i<25;i++) {
System.out.print(rand[i]+" ");
count++;
if(count%5==0) {
System.out.println();
}
}
}
}
/*输出:
-2040654481 -437858510 -529086138 -624846354 -376378649
50943645 1676630516 -1789925509 -1073837152 1496118081
989467329 -1387791988 1440818431 1870852528 1970944376
38106442 -809158290 1224153672 1924102338 487500879
-1157748135 1581939605 1121898668 100239280 2028083333 */
不知道是算法问题,还是什么问题,这是最简单的冒泡排序法啊!可是为什么就是输出结果就是错的。是不是我忘记了冒泡排序了。打死也不敢相信自己是冒泡法错了。自己也不想去想。害怕啊!还请各位大叔大姐们帮个忙。谢谢.
public static void main(String args[]) {
int temp;
int []rand=new int[25];
Random random=new Random();
for(int i=0;i<25;i++) {
rand[i]=(int)(10*random.nextDouble());
;
} for(int i=1;i<25;i++)
for(int j=i;j<25;j++){
if(rand[j-1]>rand[j]){
temp=rand[j-1];
rand[j-1]=rand[i];
rand[j]=temp;
}
} int count=0;
for(int i=0;i<25;i++) {
System.out.print(rand[i]+" ");
count++;
if(count%5==0) {
System.out.println();
}
}
}
}
这个行不行?
import java.util.Random;public class RandInt {
public static void main(String args[]) {
int[] rand = new int[25];
Random random = new Random();
for (int i = 0; i < 25; i++) {
rand[i] = random.nextInt();
} //冒泡排序
int temp = 0;
for (int i = 0; i < rand.length; i++) {
for (int j = i; j < rand.length; j++) {
if (rand[i] > rand[j]) {
temp = rand[i];
rand[i] = rand[j];
rand[j] = temp;
}
}
} int count = 0;
for (int i = 0; i < 25; i++) {
System.out.print(rand[i] + " ");
count++;
if (count % 5 == 0) {
System.out.println();
}
}
}
}
int temp=0;
if(rand[i-1]>rand[i]) {
temp=rand[i-1];
rand[i-1]=rand[i];
rand[i]=temp;
}
}
要多一层循环:
for(int i=1;i<25;i++) {
for(int j=i+1;j<25;j++{
int temp=0;
if(rand[j]>rand[i]) {
temp=rand[j];
rand[j]=rand[i];
rand[i]=temp;
}
}
}
for(int i=0;i<25;i++) {
rand[i]=random.nextInt();
} for(int i=1;i<25;i++) {
int temp=0;
if(rand[i-1]>rand[i]) {
temp=rand[i-1];
rand[i-1]=rand[i];
rand[i]=temp;
}
}
每次把最大的放大最后一位,你只是排了一个数