Java 如何修复我的数据收集程序?
该程序旨在收集三种搜索算法(线性、二进制和随机)速度的数据。我已经对每个调用的搜索方法进行了广泛的测试,它们都很有效。我知道包含k的for循环中存在错误,但我不确定是什么或为什么。我得到了线性[0]、二进制[0]和随机[0]的正确结果,但无法得到循环中数组中任何其他位置的答案(这是有意义的,因为我知道其他位置尚未发生)。但在循环之外,我无法打印出任何数组的任何部分Java 如何修复我的数据收集程序?,java,Java,该程序旨在收集三种搜索算法(线性、二进制和随机)速度的数据。我已经对每个调用的搜索方法进行了广泛的测试,它们都很有效。我知道包含k的for循环中存在错误,但我不确定是什么或为什么。我得到了线性[0]、二进制[0]和随机[0]的正确结果,但无法得到循环中数组中任何其他位置的答案(这是有意义的,因为我知道其他位置尚未发生)。但在循环之外,我无法打印出任何数组的任何部分 import java.util.Random; /*class to generate testing data*/ public
import java.util.Random;
/*class to generate testing data*/
public class GenerateData{
public static void main(String[] args) {
/*creates and runs through each power of 2 array*/
for(int i=3; i < 5; i++) {
/*calculates a power of 2 starting at 8*/
int n = (int)Math.pow(2, i);
/*creates an array of a power of 2 starting at 8*/
int [] test = new int[n];
/*generates a starting testing point for the array*/
int start = 3;
/*fills the array with sorted testing data*/
for(int j=0; j < n; j++) {
test[j] = start;
start = start + 2;
}
/*creates an array to store linear testing times*/
long [] linear = new long[10];
/*creates an array to store binary testing times*/
long [] binary = new long[10];
/*creates an array to store random testing times*/
long [] random = new long[10];
/*runs through the search algorithms ten times*/
for(int k=0; k < 10; k++) {
/*generates a random number to test no larger than the largest
value in the array*/
int queryValue = (int)Math.floor(Math.random()*(start-1));
/*tests the array in each algorithm, keeping track of start and
end time*/
long linearStartTime = System.nanoTime();
int linearTest = LinearSearch.linearSearch(queryValue, test);
/*calculates the time for linear algorithm and adds it to
an array keeping track of linear algorithm run times*/
linear[k] = System.nanoTime() - linearStartTime;
long binaryStartTime = System.nanoTime();
int binaryTest = BinarySearch.binarySearch(queryValue, test);
/*calculates the time for binary algorithm and adds it to
an array keeping track of binary algorithm run times*/
binary[k] = System.nanoTime() - binaryStartTime;
long randomStartTime = System.nanoTime();
int randomTest = RandomSearch.randomSearch(queryValue, test);
/*calculates the time for random algorithm and adds it to
an array keeping track of random algorithm run times*/
random[k] = System.nanoTime() - randomStartTime;
}
/*placeholder initial values for mins, maxes, and avgs*/
long linMin = linear[1];
long binMin = binary[1];
long randMin = random[1];
long linMax = linear[1];
long binMax = binary[1];
long randMax = random[1];
long linAvg = 0;
long binAvg = 0;
long randAvg = 0;
/*cycles through the arrays calculating the min, max, and avg*/
for(int l=0; l < 9; l++) {
/*calculates the avg for each algorithm array*/
linAvg = linAvg + linear[l] / 2;
binAvg = binAvg + binary[l] / 2;
randAvg = randAvg + random[l] / 2;
/*calculates the min for each algorithm array*/
if(linear[l] < linMin) {
linMin = linear[l];
}
if(binary[l] < binMin) {
binMin = binary[l];
}
if(random[l] < randMin) {
randMin = random[l];
}
/*calculates the max for each algorithm array*/
if(linear[l] > linMax) {
linMax = linear[l];
}
if(binary[l] > binMax) {
binMax = linear[l];
}
if(random[l] > randMax) {
randMax = random[l];
}
/*prints the current power of 2, min, max, and avg
for each algorithm into a file*/
StdOut.println("linear" + "," + n + "," + linMin + "," + linMax + "," + linAvg);
StdOut.println("binary" + "," + n + "," + binMin + "," + binMax + "," + binAvg);
StdOut.println("random" + "," + n + "," + randMin + "," + randMax + "," + randAvg);
}
}
}
}
import java.util.Random;
/*类生成测试数据*/
公共类生成数据{
公共静态void main(字符串[]args){
/*创建并运行2个阵列的每个幂次*/
对于(int i=3;i<5;i++){
/*从8开始计算2的幂*/
int n=(int)Math.pow(2,i);
/*从8开始创建2次幂的数组*/
int[]测试=新的int[n];
/*为阵列生成起始测试点*/
int start=3;
/*用已排序的测试数据填充数组*/
对于(int j=0;jlinMax){
linMax=线性[l];
}
if(二进制[l]>binMax){
binMax=线性[l];
}
if(随机[l]>randMax){
randMax=随机[l];
}
/*打印2、最小值、最大值和平均值的当前功率
对于每个算法,将其放入一个文件中*/
println(“线性“+”、“+n+”、“+linMin+”、“+linMax+”、“+linAvg”);
println(“二进制“+”、“+n+”、“+binMin+”、“+binMax+”、“+binAvg”);
println(“随机“+”、“+n+”、“+randMin+”、“+randMax+”、“+randAvg”);
}
}
}
}
我正在使用的二进制搜索。用于测试目的
/*class containing binary search algorithm*/
public class BinarySearch {
/*conducts a binary search as specified by user*/
public static int binarySearch(int queryValue, int[] list) {
int length = list.length;
/*last point of list*/
int top = length-1;
/*first point of list*/
int bottom = 0;
/*starting midpoint of list*/
int mid = (int)Math.round((top + bottom)/2);
/*binary search*/
do {
if(queryValue > list[mid]) {
bottom = mid;
mid = (int)Math.ceil((top + bottom) / 2.0);
}
else {
top = mid;
mid = (int)Math.floor((top + bottom) / 2.0);
}
if(queryValue == list[mid]) {
return mid;
}
} while (mid < top || mid > bottom);
/*returns -1 if user value not found*/
return -1;
}
}
/*class containing linear search algorithm*/
public class LinearSearch {
public static int linearSearch(int queryValue, int[] list) {
int length = list.length;
/*conducts a linear search as specified by user*/
for(int i = 0; i < length; i++) {
if((int)queryValue == (int)list[i]) {
return i;
}
}
/*return -1 if user value not found*/
return -1;
}
}
import java.util.Random;
/*class containing random search algorithm*/
public class RandomSearch {
public static int randomSearch(int queryValue, int[] list) {
/*conducts a random search as specified by user*/
/*trys 10,000,000 random combinations searching
for user value*/
int length = list.length;
for(int i=0; i < 10000000; i++) {
/*generates a random number from 0 to length*/
int randomNum = (int)Math.floor(Math.random()*(length));
if((int)queryValue == (int)list[randomNum]) {
return randomNum;
}
}
/*returns -2 if user value not found*/
return -2;
}
}
/*包含二进制搜索算法的类*/
公共类二进制搜索{
/*按照用户的指定执行二进制搜索*/
公共静态int二进制搜索(int queryValue,int[]列表){
int length=list.length;
/*最后一点*/
int top=长度-1;
/*第一点*/
int-bottom=0;
/*列表的起始中点*/
整数中间=(整数)数学圆((顶部+底部)/2);
/*二进制搜索*/
做{
如果(查询值>列表[mid]){
底部=中间;
mid=(int)Math.ceil((顶部+底部)/2.0);
}
否则{
顶部=中部;
中间=(内部)数学层((顶部+底部)/2.0);
}
if(queryValue==list[mid]){
中途返回;
}
}而(中间<顶部| |中间>底部);
/*如果未找到用户值,则返回-1*/
返回-1;
}
}
我正在使用的线性搜索。用于测试目的
/*class containing binary search algorithm*/
public class BinarySearch {
/*conducts a binary search as specified by user*/
public static int binarySearch(int queryValue, int[] list) {
int length = list.length;
/*last point of list*/
int top = length-1;
/*first point of list*/
int bottom = 0;
/*starting midpoint of list*/
int mid = (int)Math.round((top + bottom)/2);
/*binary search*/
do {
if(queryValue > list[mid]) {
bottom = mid;
mid = (int)Math.ceil((top + bottom) / 2.0);
}
else {
top = mid;
mid = (int)Math.floor((top + bottom) / 2.0);
}
if(queryValue == list[mid]) {
return mid;
}
} while (mid < top || mid > bottom);
/*returns -1 if user value not found*/
return -1;
}
}
/*class containing linear search algorithm*/
public class LinearSearch {
public static int linearSearch(int queryValue, int[] list) {
int length = list.length;
/*conducts a linear search as specified by user*/
for(int i = 0; i < length; i++) {
if((int)queryValue == (int)list[i]) {
return i;
}
}
/*return -1 if user value not found*/
return -1;
}
}
import java.util.Random;
/*class containing random search algorithm*/
public class RandomSearch {
public static int randomSearch(int queryValue, int[] list) {
/*conducts a random search as specified by user*/
/*trys 10,000,000 random combinations searching
for user value*/
int length = list.length;
for(int i=0; i < 10000000; i++) {
/*generates a random number from 0 to length*/
int randomNum = (int)Math.floor(Math.random()*(length));
if((int)queryValue == (int)list[randomNum]) {
return randomNum;
}
}
/*returns -2 if user value not found*/
return -2;
}
}
/*包含线性搜索算法的类*/
公共类线性搜索{
公共静态int-linearSearch(int-queryValue,int[]列表){
int length=list.length;
/*按照用户的指定进行线性搜索*/
for(int i=0;i
我正在使用的随机搜索。用于测试目的
/*class containing binary search algorithm*/
public class BinarySearch {
/*conducts a binary search as specified by user*/
public static int binarySearch(int queryValue, int[] list) {
int length = list.length;
/*last point of list*/
int top = length-1;
/*first point of list*/
int bottom = 0;
/*starting midpoint of list*/
int mid = (int)Math.round((top + bottom)/2);
/*binary search*/
do {
if(queryValue > list[mid]) {
bottom = mid;
mid = (int)Math.ceil((top + bottom) / 2.0);
}
else {
top = mid;
mid = (int)Math.floor((top + bottom) / 2.0);
}
if(queryValue == list[mid]) {
return mid;
}
} while (mid < top || mid > bottom);
/*returns -1 if user value not found*/
return -1;
}
}
/*class containing linear search algorithm*/
public class LinearSearch {
public static int linearSearch(int queryValue, int[] list) {
int length = list.length;
/*conducts a linear search as specified by user*/
for(int i = 0; i < length; i++) {
if((int)queryValue == (int)list[i]) {
return i;
}
}
/*return -1 if user value not found*/
return -1;
}
}
import java.util.Random;
/*class containing random search algorithm*/
public class RandomSearch {
public static int randomSearch(int queryValue, int[] list) {
/*conducts a random search as specified by user*/
/*trys 10,000,000 random combinations searching
for user value*/
int length = list.length;
for(int i=0; i < 10000000; i++) {
/*generates a random number from 0 to length*/
int randomNum = (int)Math.floor(Math.random()*(length));
if((int)queryValue == (int)list[randomNum]) {
return randomNum;
}
}
/*returns -2 if user value not found*/
return -2;
}
}
import java.util.Random;
/*包含随机搜索算法的类*/
公共类随机搜索{