searching rest
This commit is contained in:
@@ -40,5 +40,36 @@
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static int SequentialFind(int[] haystack, int needle)
|
||||
{
|
||||
for (int i = 0; i < haystack.Length; i++)
|
||||
{
|
||||
if (haystack[i] == needle)
|
||||
{
|
||||
return i;
|
||||
}
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
public static int BinaryFind(int[] haystack, int needle)
|
||||
{
|
||||
int min = 0;
|
||||
int max = haystack.Length -1;
|
||||
int i = -1;
|
||||
|
||||
while (min <= max && i == -1)
|
||||
{
|
||||
int mid = (min + max) / 2;
|
||||
|
||||
if (needle > haystack[mid]) min = mid + 1;
|
||||
else if (needle < haystack[mid]) max = mid - 1;
|
||||
else i = mid;
|
||||
}
|
||||
|
||||
return i;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user