100 万から 1000 万までの 1000 万の数値を含む配列を作成したいと考えています。ループを使用して配列にデータを入力しています (1 ずつ増加)。ここで、最初の配列を 10 回検索し (10 回検索するループを作成します)、2 番目の数値配列 (例: int arr2[] = {10, 20, .....}) を使用します。次に、各検索にかかった時間、平均と標準偏差、結果を表に出力します. "\t" を付けて出力する表は、これまでのところ次のとおりです:
public class LinearBinearySearch {
public static void main(String[] args) {
System.out.println("By linear search:\n" + check[k] + " found at index " + found +"\t");
System.out.println("Table below shows result:");
System.out.print("First run\tSecond run\tThird run\tFourth run\tFifth run\tSixth run\tSeventh run\tEight run\tNinth run\tTenth run\tAverage \tStandard deviation\n");
arrPoplte();
linSrch();
loopCheck();
}
static int i = 0;
static int k = 0;
static int[]Arr = new int[10000000];
static int[]check = {500, 10000, 100000, 1000000, 5000000, 7000000, 10000000};
public static void loopCheck(){
}
public static void arrPoplte(){
for(int i = 0; i < Arr.length; i ++){
Arr[i] = i + 1;
}
}
static int found = 0;
static long start;
static long end;
public static void linSrch(){
long sum = 0;
long sumSquare = 0;
for(int c = 0; c < 10 ; c++){
start = System.nanoTime();
while(Arr[i]<check.length){
if(Arr[i]==check[i])
System.out.print(Arr[i]);
end = System.nanoTime();
sum += end - start;
sumSquare += Math.pow(end - start, 2);
}
System.out.print((end - start) +"\t\t");
}
double average = (sum * 1D) / 10;
double variance = (sumSquare * 1D) / 10 - Math.pow(average, 2);
double std = Math.sqrt(variance);
System.out.print(average +"\t\t" + std + "\n");
しかし、1.コードが多すぎると思います.2.最初の配列の値を使用するために2番目の配列をループできません。
これは私の希望する出力です: 500 は、どのインデックスでも見つかりました。x ms y ms z ms av ms それが何であれ
コードを微調整して目的の出力を生成するにはどうすればよいですか。
このような長い質問を前もってお詫び申し上げます。誰かが私を助けてくれることを願っています ありがとう