gpt4 book ai didi

java - 如何对数组进行升序排序?

转载 作者:行者123 更新时间:2023-11-30 06:30:03 25 4
gpt4 key购买 nike

我不明白为什么 r 数组不会按升序排序。我尝试过 Array.sort 并手动对数组进行排序。

    import java.lang.*;
import java.lang.Object;
import java.lang.Integer;
import java.util.Arrays;
import java.util.Calendar;
import java.util.GregorianCalendar;

public class Gaussian {
public static int seed;
public static final int n = 100;
public static void main(String argv[]) {
double r[] = new double[100];

// Initiate the seed from the current time
GregorianCalendar t = new GregorianCalendar();
int t1 = t.get(Calendar.SECOND);
int t2 = t.get(Calendar.MINUTE);
int t3 = t.get(Calendar.HOUR_OF_DAY);
int t4 = t.get(Calendar.DAY_OF_MONTH);
int t5 = t.get(Calendar.MONTH);
int t6 = t.get(Calendar.YEAR);
seed = t6 + 65*(t5+12*(t4+31*(t3+24*(t2+60*t1))));
if ((seed%2) == 0) seed = seed-1;

****************这是给我带来麻烦的部分********************

// Put the Gaussian random numbers in the array
for (int i=0; i<n-1; i+=1) {
r = rang();
for (int l=0; l<r.length-1; l++) {
if(r[l] < r[l+1]) {
double tempValue = r[l+1];
r[l+1] = r[l];
r[l] = tempValue;

}
}
System.out.println(r[0]);

********************在这些星星之间**************************** *****

    }
}

// Method to create two Gaussian random numbers from two
// uniform random numbers in [0,1].

public static double[] rang() {
double x[] = new double[1];
double r1, r2;
r1 = - Math.log(1-ranf());
r2 = 2*Math.PI*ranf();
r1 = Math.sqrt(2*r1);
x[0] = r1*Math.cos(r2);
return x;
}

// Method to generate a uniform random number in [0,1]
// following x(i+1)=a*x(i) mod c with a=pow(7,5) and
// c=pow(2,31)-1. Here the seed is a global variable.

public static double ranf() {
final int a = 16807, c = 2147483647, q = 127773,
r = 2836;
final double cd = c;
int h = seed/q;
int l = seed%q;
int t = a*l-r*h;
if (t > 0) seed = t;
else seed = c+t;
return seed/cd;
}

}

出于某种原因,它给了我这个输出:

-0.7286443240313888
0.9205595151394262
-0.1201523471771766
-0.2955395834645375
0.5562293071303744
0.5947383124976592
-0.5190410499731951
1.1878905341959594
-0.6530738641804281
1.92941716216534
-1.55458771926982
1.011542837179014
-1.2973072313970084
-0.5115664645635027
-0.4537839981939878
-0.43386113937789456
2.1877083571592637
-0.1869725174568339
1.0427194985616417
0.7491392218512473
-0.2837863829399006
0.09204148771478798
0.08980225475596745
-1.0595943397788652
0.2493101533697332
-1.3926086961785766
0.9722238128294852
0.4490619874581054
1.4379635505387074
1.4550206564181973
-0.9754513444753741
-1.6454765651087158
0.1683214049373476
0.9981636099004854
-0.7289169766110786
1.6612385375332162
0.19025688479326378
0.0830947016802825
0.4674778575126086
-0.9077431792737849
-0.5638299547034225
0.13229202082089384
1.2429372493642745
-0.006685432080368285
2.336192098747748
-0.5450098522726261
-1.6420372037670146
0.3400579125911062
0.48689741262698993
-0.5075527810259783
1.9679760629290328
-1.9114919760463223
0.5633783650935041
0.12871665512520616
-1.8826404473732248
0.16725744941405607
1.049647212107755
0.767071049830706
0.3366261688045942
-1.726395330988362
-0.15241706234915703
-0.17645549457761323
1.098469368528642
-0.3173352964219553
-2.6584067823396675
0.4136264577634812
-1.2506808927401905
2.0462718170224186
-2.380899123430688
-1.0340941198026203
-3.223035072470854
-0.1423807157151033
-0.048464495873010084
1.4690537691472332
0.9110766995396362
-0.040683539673625924
-0.3895836309957472
-0.4793889976948361
0.007621022168540105
0.4151797552606307
1.2734508381903344
0.6398148976757589
-2.0458807284022114
0.23937728902415573
0.09380205942857836
1.331532378407905
-0.09813530948364875
0.9515533393393638
-1.5924626733327882
-1.2504131049626441
0.3674623983411812
0.8204457493547238
0.2214473639135442
0.5573901544532469
1.6349106235864332
-1.4373482822115147
0.38216369985059967
-0.6869980429363977
0.30632157455967757

而不是按升序对数字进行排序。

最佳答案

数组未排序的原因是 r = rang();。最初,double r[] = new double[100];r 设置为长度为 100 的数组。但是,r 被重新分配给rang() 的结果,它是一个长度为 1 的数组。这会导致内部 for 循环永远不会运行。

我建议进行以下更改:

// I'm assuming that you want 100 Gaussian random numbers, not 99
for (int i = 0; i < r.length; i += 1) {
// you could also change rang() to return a
// double instead of an array of double
// because the array only contains 1 double
double[] randomGaussian = rang();
r[i] = randomGaussian[0];
}

// sort the array
Arrays.sort(r);

// print out the array
for(int i = 0; i < r.length; i++) {
System.out.print(r[i] + " ");
}

关于java - 如何对数组进行升序排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46353035/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com