【发布时间】: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