二进制搜索字符串

时间:2017-11-10 20:44:42

标签: java

我对Java没有多少经验,现在我正努力让我的代码正常工作。如果有人能帮助我让我的代码正常工作,我真的很感激。

我基本上使用文本输入文件来查找某些键,我创建了一个带有这些名称的数组:

Azevedo,Ana"," Silva,Rui"," Boussebough,Imane"," Terracina,Giorgio,"," Lefebvre ,Peter"," Houghten,Sher"," Revesz,Peter

请注意Lefebvre,Peter不在我正在使用的文本输入文件中

以下是我对代码的主要问题:

1)它给出了奇怪的结果: 二进制搜索,目标:Lefebvre,Peter指数:1030比较数:11

当输入文件中没有Lefebvre时,Peter

2)插值搜索也提供了非常高的比较计数,从50到139

我不认为以下country16是正确的,但无法找到其他方法。

好的,希望有人可以帮助我,这是我的计划:

binarySearch(sortedArray, toFind);
in public static int interpolationSearch(String[] sortedArray, String toFind)

}

1 个答案:

答案 0 :(得分:0)

您的quickSort()计划似乎存在一些问题。

使用Arrays.sort()方法的输出检出标准quickSort()方法的输出。一旦我使用标准排序,二进制搜索和插值搜索的索引匹配。

由于我不知道输入文件中存在哪些键,所以我使用相同的数组作为输入(但没有排序)。

更新:我不确定dist()方法的实现是否与数据分布一致,但一般来说,插值搜索的最差时间复杂度是O(n),这意味着您可以对n个数据点进行n次比较。这种糟糕的性能可能是由于数据分布不均匀或数据点之间距离计算的实现不正确或不良造成的。

问题:当keysArr, I have fixed the method binarySearch(String [] sortedArray,String target)中不存在该元素时,二进制搜索方法存在一个问题,即它没有返回-1 ,int start,int end)`。修复后,找不到以下键并具有index = -1

  • Terracina,Giorgio,(注意:搜索结尾处还有一个逗号)
  • Lefebvre,Peter
  • Houghten,Sher(注意:Houghten,Sheridan存在但不是Houghten,Sher)
import java.io.BufferedReader;
import java.io.FileReader;
import java.math.BigDecimal;
import java.util.*;

public class findstrings {

    static List<String> keys = new ArrayList<String>();
    static int binarySearchComparisionCount = 0;
    static int interpolationSearchComparisionCount = 0;

    public static void main(String args[]) {
        try {
            keys = readFile("C:\\workspace\\OCA\\datadfb.txt");
            String[] keysArr = keys.toArray(new String[keys.size()]);

            doQuickSort(keysArr, 0, keys.size() - 1);

            String arr[] = { "Azevedo, Ana", "Silva, Rui", "Boussebough, Imane", "Terracina, Giorgio,",
                    "Lefebvre, Peter", "Houghten, Sher", "Revesz, Peter" };

            System.out.println();

            System.out.printf("Total Elements : %d\n\n", keysArr.length);

            int binaryComplexity = (int) (Math.log(keysArr.length) / Math.log(2));
            int interpolationComplexity = (int) (Math.log(Math.log(keysArr.length) / Math.log(2)) / Math.log(2));
            for (String str : arr) {
                System.out.printf(
                        "Binary Search,        Target: %-20s Index: %-6d Comparision count: %-5d Complexity logn      : %d\n",
                        str, binarySearch(keysArr, str), binarySearchComparisionCount, binaryComplexity);
                System.out.printf(
                        "Interpolation Search, Target: %-20s Index: %-6d Comparision count: %-5d Complexity log(logn) : %d\n",
                        str, interpolationSearch(keysArr, str), interpolationSearchComparisionCount,
                        interpolationComplexity);
                System.out.println();
                binarySearchComparisionCount = 0;
                interpolationSearchComparisionCount = 0;
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static List<String> readFile(String filename) throws Exception {
        String line = null;
        List<String> records = new ArrayList<String>();

        // wrap a BufferedReader around FileReader
        BufferedReader bufferedReader = new BufferedReader(new FileReader(filename));

        // use the readLine method of the BufferedReader to read one line at a time.
        // the readLine method returns null when there is nothing else to read.
        while ((line = bufferedReader.readLine()) != null) {
            records.add(line.trim());
        }

        // close the BufferedReader when we're done
        bufferedReader.close();
        return records;
    }

    private static int binarySearch(String[] sortedArray, String target) {
        return binarySearch(sortedArray, target, 0, sortedArray.length - 1);
    }

    private static int binarySearch(String[] sortedArray, String target, int start, int end) {
        if (start <= end) {
            int mid = -1;
            mid = (start + end) / 2;
            int c = target.compareTo(sortedArray[mid]);
            ++binarySearchComparisionCount;
            return (c == 0) ? mid
                    : (c < 0) ? binarySearch(sortedArray, target, start, mid - 1)
                            : binarySearch(sortedArray, target, mid + 1, end);
        } else {
            return -1;
        }
    }

    static BigDecimal dist(String str1, String str2) {
        int maxlen = str1.length();
        if (str1.length() < str2.length())
            maxlen = str2.length();
        BigDecimal d = BigDecimal.ZERO;
        for (int i = 0; i < maxlen; i++) {
            int dist;
            if (i < str1.length() && i < str2.length()) {
                dist = str1.charAt(i) - str2.charAt(i);
            } else if (i < str1.length()) {
                dist = str1.charAt(i);
            } else {
                dist = -str2.charAt(i);
            }
            d = d.add(new BigDecimal(dist * Math.pow(2, -i * 8)));
        }
        return d;
    }

    public static int interpolationSearch(String[] sortedArray, String toFind) {
        int low = 0;
        int high = sortedArray.length - 1;
        int mid;
        while (sortedArray[low].compareTo(toFind) <= 0 && sortedArray[high].compareTo(toFind) >= 0) {
            if (sortedArray[high].equals(sortedArray[low]))
                return (low + high) / 2;
            // out of range is possible here
            double value = new Double(dist(toFind, sortedArray[low]).doubleValue()) * (high - low);
            mid = (int) (low
                    + (value) / new Double(dist(sortedArray[high], sortedArray[low]).doubleValue()).intValue());
            ++interpolationSearchComparisionCount;
            if (sortedArray[mid].compareTo(toFind) < 0)
                low = mid + 1;
            else if (sortedArray[mid].compareTo(toFind) > 0)
                high = mid - 1;
            else
                return mid;
        }
        if (sortedArray[low].equals(toFind))
            return low;
        // not found
        else
            return binarySearch(sortedArray, toFind);
    }

    public static void doQuickSort(String[] array) {
        doQuickSort(array, 0, array.length - 1);
    }

    public static void doQuickSort(String[] array, int lower, int higher) {
        int i = lower;
        int j = higher;
        String pivot = array[lower + (higher - lower) / 2];
        while (i <= j) {
            while (array[i].compareToIgnoreCase(pivot) < 0) {
                i++;
            }
            while (array[j].compareToIgnoreCase(pivot) > 0) {
                j--;
            }
            if (i <= j) {
                String t = array[i];
                array[i] = array[j];
                array[j] = t;
                i++;
                j--;
            }
        }
        if (lower < j)
            doQuickSort(array, lower, j);
        if (i < higher)
            doQuickSort(array, i, higher);
    }
}

示例运行:

Total Elements : 2095

Binary Search,        Target: Azevedo, Ana         Index: 142    Comparision count: 9     Complexity logn      : 11
Interpolation Search, Target: Azevedo, Ana         Index: 142    Comparision count: 119   Complexity log(logn) : 3

Binary Search,        Target: Silva, Rui           Index: 1742   Comparision count: 8     Complexity logn      : 11
Interpolation Search, Target: Silva, Rui           Index: 1742   Comparision count: 139   Complexity log(logn) : 3

Binary Search,        Target: Boussebough, Imane   Index: 249    Comparision count: 11    Complexity logn      : 11
Interpolation Search, Target: Boussebough, Imane   Index: 249    Comparision count: 114   Complexity log(logn) : 3

Binary Search,        Target: Terracina, Giorgio,  Index: -1     Comparision count: 11    Complexity logn      : 11
Interpolation Search, Target: Terracina, Giorgio,  Index: -1     Comparision count: 51    Complexity log(logn) : 3

Binary Search,        Target: Lefebvre, Peter      Index: -1     Comparision count: 11    Complexity logn      : 11
Interpolation Search, Target: Lefebvre, Peter      Index: -1     Comparision count: 62    Complexity log(logn) : 3

Binary Search,        Target: Houghten, Sher       Index: -1     Comparision count: 12    Complexity logn      : 11
Interpolation Search, Target: Houghten, Sher       Index: -1     Comparision count: 77    Complexity log(logn) : 3

Binary Search,        Target: Revesz, Peter        Index: 1554   Comparision count: 7     Complexity logn      : 11
Interpolation Search, Target: Revesz, Peter        Index: 1554   Comparision count: 18    Complexity log(logn) : 3