在C#.net中搜索,搜索类型和使用C#程序的实现
搜索是一种用于查找特定项目的技术,这里我们讨论了一些流行的搜索技术,用于从数组中查找项目。
例如,我们有一个列表:12151183448
在这里,我们会发现这是对3存在项目11RD(根据数组索引它是在2次)位置。
在C#的上下文中,我们将采用一个包含整数元素的数组。然后,我们将编写代码以从数组中搜索元素。
搜索类型
基本上,计算机技术中使用两种类型的搜索:
内部搜寻
外部搜寻
1)内部搜索:将项目搜索到主存储器或主存储器中被称为内部搜索。
2)外部搜索:将项目搜索到外部或辅助存储器中称为外部搜索。
在数组或链表中,我们始终执行内部搜索。下面给出了一些用于搜索的流行方法:
线性或顺序搜索
二进制搜索
1)C#中的线性或顺序搜索实现
在线性/顺序搜索中,我们依次搜索给定的项目,如果找到了项目,则返回位置。也可能直到列表的最后一项才找到项目。
例如,我们有以下列表:1213102547
假设我们有25个要搜索的项目。
在第一次比较中,我们将25与12进行了比较,但她没有找到匹配。
在第二次比较中,我们将25与13进行了比较,但是在这里我们找不到匹配项。
在第三次比较中,我们将25与10进行了比较,但仍然没有找到匹配的结果。
在第四次比较中,我们将25与25进行比较。在这里,我们找到了匹配元素,然后我们可以说25在给定列表中排名第四。
看程序:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main()
{
int i = 0 ;
int item = 0 ;
int pos = 0 ;
int[] arr = new int[5];
//将数字读入数组
Console.WriteLine("Enter elements : ");
for (i = 0; i < arr.Length; i++)
{
Console.Write("Element[" + (i + 1) + "]: ");
arr[i] = int.Parse(Console.ReadLine());
}
Console.Write("Enter item to search : ");
item = int.Parse(Console.ReadLine());
//循环搜索数组中的元素。
for (i = 0; i < arr.Length; i++)
{
if (item == arr[i])
{
pos = i + 1;
break;
}
}
if (pos == 0)
{
Console.WriteLine("Item Not found in array");
}
else
{
Console.WriteLine("Position of item in array: "+pos);
}
}
}
}输出结果
Enter elements : Element[1]: 25 Element[2]: 12 Element[3]: 13 Element[4]: 16 Element[5]: 29 Enter item to search : 16 Position of item in array: 4
C#中的二进制搜索实现
在此搜索中,我们将列表划分为搜索对象,这里我们使用3个变量LOW,HIGH,MID。
LOW-表示列表的下标。
高-表示列表的最高索引。
MID-指示列表的中间索引。
例如,我们有一个升序的列表。102030405060708090100
我们在上面的列表中找到了项目30。
注意:数组必须排序
1st comparison: LOW = 1 HIGH = 10 MID = (1+10) /2 = 5 Now if LIST[MID] == ITEM, (50 == 30) No Match. Then find new indices by dividing list. Here ITEM is less than mid value then: LOW = 1 HIGH = MID -1 =4 MID = (1+4)/2 = 2 2nd comparison:IF LIST[MID] == ITEM, (20==30) No Match. Then find new indices by dividing list. Here ITEM is greater than mid value then: LOW = MID + 1 = 3 HIGH = 4 MID = (3+4)/2 = 33rd comparison:IF LIST[MID] == ITEM, (30==30), No ITEM found at position 3.
看程序:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main()
{
int i = 0 ;
int item = 0 ;
int pos = 0 ;
int[] arr = new int[5];
int LOW = 0;
int HIGH = 0;
int MID = 0;
//将数字读入数组
Console.WriteLine("Enter elements : ");
for (i = 0; i < arr.Length; i++)
{
Console.Write("Element[" + (i + 1) + "]: ");
arr[i] = int.Parse(Console.ReadLine());
}
Console.Write("Enter item to search : ");
item = int.Parse(Console.ReadLine());
HIGH =arr.Length- 1;
MID = (LOW + HIGH) / 2;
//循环搜索数组中的元素。
while (LOW <= HIGH)
{
if (item < arr[MID])
{
HIGH = MID - 1;
MID = (LOW + HIGH) / 2;
}
else if (item > arr[MID])
{
LOW = MID + 1;
MID = (LOW + HIGH) / 2;
}
else if (item == arr[MID])
{
pos = MID + 1;
break;
}
}
if (pos == 0)
{
Console.WriteLine("Item Not found in array");
}
else
{
Console.WriteLine("Position of item in array: "+pos);
}
}
}
}输出结果
Enter elements : Element[1]: 10 Element[2]: 20 Element[3]: 30 Element[4]: 40 Element[5]: 50 Enter item to search : 40 Position of item in array: 4