【问题标题】:Interpolation Search, searching on a descending array插值搜索,在降序数组上搜索
【发布时间】:2018-10-07 12:00:24
【问题描述】:

我想修改此算法,以便它可以在已按降序排序的数组中找到值。这目前仅适用于升序。

public static int interpo(double[] array, double key, int order)
{
    int low = 0, high = array.Length - 1;
    int pos = 0;
    int count = 0;

    while ((low <= high) && (key >= array[low]) && (key <= array[high]))
    {
        count++;
        pos = Convert.ToInt32(low + (high - low) / (array[high] - array[low]) * 
            (key - array[low]));

        if (array[pos] == key)
        {
            // Write out the position and the value of whats in the position
            Console.WriteLine("\n     {0} : {1}", pos, array[pos]);                   
            break;
        }

        if (array[pos] < key)
            low = pos + 1;
        else
            high = pos - 1;

        // If the count is greater than the array size, do the following
        if (count > array.Length) 
        {
            // Pass through the position within the array to the close element 
            // method, which will display the closest values within the array
            closeelement(array, key, pos);

            // Return nothing                      
            return -2;  
        }
    }

    return -1;
}

【问题讨论】:

    标签: c# arrays algorithm sorting interpolation


    【解决方案1】:

    假设您希望能够在参数order 中传递数组的排序顺序,您只需更改终止条件,如下所示(使用 0=descending,1=ascending):

    public static int interpo(double[] array, double key, int order)
    {
        int low = 0, high = array.Length - 1;
        int pos = 0;
        int count = 0;
    
        while ((low <= high) && ((order == 1 && (key >= array[low]) && (key <= array[high])) || (order == 0 && (key <= array[low]) && (key >= array[high]))))
        {
            count++;
            pos = Convert.ToInt32(low + (high - low) / (array[high] - array[low]) *
                (key - array[low]));
    
            if (array[pos] == key)
            {
                // Write out the position and the value of whats in the position
                Console.WriteLine("\n     {0} : {1}", pos, array[pos]);
                break;
            }
    
            if (array[pos] < key)
                low = pos + 1;
            else
                high = pos - 1;
    
            // If the count is greater than the array size, do the following
            if (count > array.Length)
            {
                // Pass through the position within the array to the close element 
                // method, which will display the closest values within the array
                closeelement(array, key, pos);
    
                // Return nothing                      
                return -2;
            }
        }
    
        return -1;
    }
    

    编辑

    如果您需要该函数能够在降序数组中查找值,只需像这样更改终止条件:

    public static int interpo(double[] array, double key, int order)
    {
        int low = 0, high = array.Length - 1;
        int pos = 0;
        int count = 0;
    
        while ((low <= high) && ((key <= array[low]) && (key >= array[high])))
        {
            count++;
            pos = Convert.ToInt32(low + (high - low) / (array[high] - array[low]) * 
                (key - array[low]));
    
            if (array[pos] == key)
            {
                // Write out the position and the value of whats in the position
                Console.WriteLine("\n     {0} : {1}", pos, array[pos]);                   
                break;
            }
    
            if (array[pos] < key)
                low = pos + 1;
            else
                high = pos - 1;
    
            // If the count is greater than the array size, do the following
            if (count > array.Length) 
            {
                // Pass through the position within the array to the close element 
                // method, which will display the closest values within the array
                closeelement(array, key, pos);
    
                // Return nothing                      
                return -2;  
            }
        }
    
        return - 1;
    }
    

    【讨论】:

    • 不知道这有什么帮助,抱歉。
    • 您是否费心尝试提供的代码?结果与您的预期结果相比如何?
    • 我不需要 order 参数,我只需要修改代码使其适用于降序数组。之后我会添加订单内容。很抱歉造成混乱。
    • 好的。编辑了我的答案...Here's 我为您制作的小提琴来测试它。
    • 我知道你要去哪里了,我试过了,没有。
    【解决方案2】:

    既然你说这个算法适用于升序排序的数组,我可以想到以下方法:

    1. 切换索引访问,即不访问索引i处的元素,而是访问array.Length - i - 1处的元素

      例子:

      不要写array[low],而是写array[array.Length - 1 - low]。你可以 通过在方法的开头引入一个变量来简化这一点:

      int l = array.Length - 1;
      

      然后在你的代码中做例如:

      while ((low <= high) && (key >= array[l - low]) && (key <= array[l - high])) 
      { /* ... */ }
      
    2. 在执行算法之前反转数组。您可以使用Array.Reverse() 执行此操作。

    【讨论】:

    • 我不想反转数组,抱歉。而且我不确定您所说的索引访问是什么意思。
    • 我有,索引超出了数组的范围。抱歉,如果我遗漏了什么,我不是 c# 专家
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-19
    • 1970-01-01
    相关资源
    最近更新 更多