static void Main(string[] args)
{
int[] numbers = new int[20];
StreamReader numFile = File.OpenText(@"H:\工作学习unity\CSharpTest\ConsoleApplication1\demo\Test.txt");
for (int i = 0; i < numbers.Length; i++)
numbers[i]=Convert.ToInt32( numFile.ReadLine());
int searchNumber;
Console.WriteLine("Enter a Number to search for:");
searchNumber = Convert.ToInt32(Console.ReadLine());
Console.WriteLine( SeqSearch(numbers, searchNumber));
Console.WriteLine(SeqSearchIndex(numbers, searchNumber));
Console.WriteLine("最大值"+FindMax(numbers));
Console.WriteLine("最小值" + FindMin(numbers));
Console.WriteLine( binSearch(58));
Console.WriteLine(RbinSearch(64, 0,9));
Console.Read();
}
/// <summary>
/// 顺序查找
/// </summary>
/// <param name="arr"></param>
/// <param name="sValue"></param>
/// <returns></returns>
static bool SeqSearch(int[] arr,int sValue) {
for (int index = 0; index < arr.Length; index++)
{
if (arr[index]==sValue)
{
return true;
}
}
return false;
}
/// <summary>
/// 顺序查找返回位置
/// </summary>
/// <param name="arr"></param>
/// <param name="sValue"></param>
/// <returns></returns>
static int SeqSearchIndex(int[] arr, int sValue)
{
for (int index = 0; index < arr.Length; index++)
if (arr[index] == sValue)
return index;
return -1;
}
/// <summary>
/// 查找最小值
/// </summary>
/// <param name="arr"></param>
/// <returns></returns>
static int FindMin(int[] arr) {
int min= arr[0];
for (int index = 1; index < arr.Length; index++)
if (min>arr[index])
min = arr[index];
return min;
}
/// <summary>
/// 查找最大值
/// </summary>
/// <param name="arr"></param>
/// <returns></returns>
static int FindMax(int[] arr)
{
int max = arr[0];
for (int index = 1; index < arr.Length; index++)
if (max < arr[index])
max = arr[index];
return max;
}
public static Int32[] IntArr = new Int32[10] { 2, 23, 34, 56, 58, 64, 89, 90, 97, 99 };
/// <summary>
/// 自组织数据加快顺序查找速度
/// </summary>
/// <param name="aValue"></param>
/// <returns></returns>
static int SeqSearchCustom(int aValue) {
for (int i = 0; i < IntArr.Length; i++)
{
if (IntArr[i]==aValue&&i>(IntArr.Length*0.2))
{
swap(i,i-1);
return i - 1;
}
else
{
if (IntArr[i] == aValue)
{
return i;
}
}
}
return -1;
}
static void swap(int index1,int index2) {
int temp = IntArr[index1];
IntArr[index1] = IntArr[index2];
IntArr[index2] = temp;
}
//二分查找(有顺序的)
static int binSearch(int value) {
int upperBound, lowerBound, mid;
upperBound = IntArr.Length - 1;
lowerBound = 0;
while (lowerBound<= upperBound)
{
mid = (lowerBound+upperBound) / 2;
if (IntArr[mid]==value)
return mid;
else
{
if (value< IntArr[mid])
upperBound = mid - 1;
else
lowerBound = mid + 1;
}
}
return -1;
}
/// <summary>
/// 递归的二分查找
/// </summary>
/// <param name="value"></param>
/// <param name="lower"></param>
/// <param name="upper"></param>
/// <returns></returns>
public static int RbinSearch(int value,int lower,int upper) {
if (lower>upper)
{
return -1;
}
else
{
int mid = (upper+lower) / 2;
if (value<IntArr[mid])
{
return RbinSearch(value, lower, mid - 1);
}
else if (value ==IntArr[mid])
{
return mid;
}
else return RbinSearch(value, mid + 1, upper);
}
}
}
}
003_基础查找算法
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 本系列博客习题来自《算法(第四版)》,算是本人的读书笔记,如果有人在读这本书的,欢迎大家多多交流。为了方便讨论,本...
- Java基础算法:堆排,快排,二分查找 1. 堆排 满二叉树:所有叶结点都有同样的深度,每个内部结点都有两个儿子 ...
- 还是要好好学习英文啊,笔者只能看中文版的,下载地址如下:http://download.csdn.net/deta...