C# 给定一个由n个元素组成的排序数组,编写一个函数来搜索数组中的给定元素x
问题解决:给定一个由n个元素组成的排序数组,编写一个函数来搜索数组中的给定元素x $查找给定排序数组中的元素 $查找数字的第一个或最后一个匹配项。算法:二进制搜索: 问题解决:给定一个由n个元素组成的排序数组,编写一个函数来搜索数组中的给定元素x 一种简单的方法是进行线性搜索,该算法的时间复杂度为O(n)。 执行相同任务的另一种方法是使用二进制搜索 二进制搜索: 通过重复将搜索间隔减半来搜索已排序的数组。 从覆盖整个阵列的间隔开始。 如果搜索关键字的值小于间隔中间的项,则将间隔缩小到下半部分。 否则,将其缩小到上半部分。反复检查,直到找到值或间隔为空 二进制搜索在排序数组上工作。将该值与数组的中间元素进行比较。 如果找不到相等,则消除不存在该值的一半。 以同样的方式,搜索另一半 二进制搜索的思想是使用数组被排序的信息,并将时间复杂度降低到O(logn) 在一次比较之后,我们基本上忽略了一半的元素:C# 给定一个由n个元素组成的排序数组,编写一个函数来搜索数组中的给定元素x,c#,data-structures,binary-search,array-algorithms,C#,Data Structures,Binary Search,Array Algorithms,问题解决:给定一个由n个元素组成的排序数组,编写一个函数来搜索数组中的给定元素x $查找给定排序数组中的元素 $查找数字的第一个或最后一个匹配项。算法:二进制搜索: 问题解决:给定一个由n个元素组成的排序数组,编写一个函数来搜索数组中的给定元素x 一种简单的方法是进行线性搜索,该算法的时间复杂度为O(n)。 执行相同任务的另一种方法是使用二进制搜索 二进制搜索: 通过重复将搜索间隔减半来搜索已排序的数组。 从覆盖整个阵列的间隔开始。 如果搜索关键字的值小于间隔中间的项,则将间隔缩小到下半部分。
实际的问题是什么?我猜问题是为什么这不总是有效。如果调用第一个
Exists
来查找数组[0,1]
中的值1
,我认为它将错误地返回-1
。因为我在路上,所以无法检查或帮助phone@Knoop我检查过了,可以用了fine@WaelMourad你是对的,我看错了。因此,基本上这个问题根本不是一个问题,而是试图在stackoverflow上写博客?其中一些似乎是从互联网上复制的(例如)。没有署名的复制是剽窃,这是被禁止的。
public class BinarySearch
{
#region helpers.
/// <summary>
/// Returns index of x if it is present in int[], else return -1,
/// </summary>
/// <param name="array">array of sorted numbers</param>
/// <param name="l">start index</param>
/// <param name="r">end index</param>
/// <param name="x">searched number</param>
/// <returns>index of x</returns>
public static int Exist(int[] array, int l, int r, int x)
{
while (l <= r)
{
int med = (l + r) / 2;
// Check if x is present at mid
if (x == array[med])
return med;
// If x is smaller, ignore right half
if (x < array[med])
r = (med - 1);
// If x greater, ignore left half
else
l = (med + 1);
}
return -1;
}
/// <summary>
/// Returns index of first or last occurrence of a number if it is
present in int[], else return -1,
/// </summary>
/// <param name="array">array of sorted numbers</param>
/// <param name="l">start index</param>
/// <param name="r">end index</param>
/// <param name="x">searched number</param>
/// <param name="first">Boolean: true if searching first occurrence
or false if last</param>
/// <returns>index of x</returns>
public static int Exist(int[] array, int l, int r, int x, bool first)
{
int result = -1;
while (l <= r)
{
int med = (l + r) / 2;
// Check if x is present at mid
if (x == array[med])
{
result = med;
if (first)
r = (med - 1); //ignore right half
else
l = (med + 1); //ignore left half
}
else if (x < array[med])
r = (med - 1);
else
l = (med + 1);
}
return result;
}
#endregion
}
class Program
{
#region props.
public BinarySearch BinarySearch { get; set; }
#endregion
#region cst.
public Program()
{
this.BinarySearch = new BinarySearch();
}
#endregion
#region publics
public static void Main()
{
//int[] array = { 2, 3, 4, 10, 40 };
//int count = array.Length - 1;
//var result = BinarySearch.Exist(array, 0, count, 4);
int[] array = { 2, 10, 10, 10, 40 };
int count = array.Length - 1;
var result = BinarySearch.Exist(array, 0, count, 40, false);
Console.WriteLine(result == -1 ? "Element not present" :
$"Element
found at index {result}");
Console.ReadKey();
}
#endregion
}