如何修复我的数据收集程序?

时间:2014-01-25 21:48:03

标签: java

该程序旨在收集有关三种搜索算法(线性,二进制和随机)速度的数据。我已经广泛测试了所调用的每种搜索方法,它们都可以工作。我知道包含k的for循环中存在错误,但我不确定是什么或为什么。我得到了线性[0],二进制[0]和随机[0]的正确结果,但无法得到循环内数组中任何其他位置的答案(这是有道理的,因为我其他地方尚未发生)。在循环之外,我无法打印出任何数组的任何部分。

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);
        }
    }
    }
}

我正在使用的二进制搜索。用于测试目的。

/*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;
    }
}

1 个答案:

答案 0 :(得分:0)

如果这是Java,则要使用 的System.out.println(....) 不是你的StdOut ......