• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

C#实现线性查找(递归,非递归)

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

源文件: http://pan.baidu.com/share/link?shareid=439733&uk=3912660076

参考代码来源自课本

//Main: 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LinearSearch
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Please enter the array length:");
            int length = Convert.ToInt32(Console.ReadLine());
            Function obj = new Function(length);

            int position;

            Console.WriteLine("The array is:");
            Console.WriteLine(obj);

            while (true)
            {
                Console.WriteLine("Please choose:");
                Console.WriteLine("1.LinearSearch;");
                Console.WriteLine("2.RecursiveLinearSearch;");
                Console.WriteLine("3.Exit;");

                int number = Convert.ToInt32(Console.ReadLine());
                Console.WriteLine();

                switch (number)
                {
                    case 1:
                        Console.WriteLine("Please enter an integer target value:");
                        int targetValue = Convert.ToInt32(Console.ReadLine());
                        Console.WriteLine();
                        position = obj.LinearSearch(targetValue);

                        if (position == -1)
                            Console.WriteLine("The integer {0} was not found.\n",
                               targetValue);
                        else
                            Console.Write(
                               "The integer {0} was found in position:{1}\n",
                               targetValue, position);
                        break;
                    case 2:
                        Console.WriteLine("Please enter an integer target value:");
                        int targetValue2 = Convert.ToInt32(Console.ReadLine());
                        Console.WriteLine();
                        position = obj.RecursiveLinearSearch(targetValue2,0);

                        if (position == -1)
                            Console.WriteLine("The integer {0} was not found.\n",
                               targetValue2);
                        else
                            Console.Write(
                               "The integer {0} was found in position:{1}\n",
                               targetValue2, position);
                        break;
                    case 3:
                        Environment.Exit(0);
                        break;
                }
            }
        }
    }
}

//Class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LinearSearch
{
    class Function
    {
        /// <summary>
        /// 声明变量
        /// </summary>
        private int[] array;
        private static Random ran = new Random();

        /// <summary>
        /// 初始化数组
        /// </summary>
        /// <param name="length"></param>
        public Function(int length)
        {
            array = new int[length];
            while (length > 0)
                array[--length] = ran.Next(0, 100);
        }

        /// <summary>
        /// 线性查找:
        ///         在数组中从第一个元素开始依次向后和给定的元素比较找到返回位置,
        ///     否则说明没有找到。
        /// 核心算法时间复杂度:
        ///             T(n)=O(n);
        /// </summary>
        /// <param name="targetValue"></param>
        /// <returns></returns>
        public int LinearSearch(int targetValue)
        {
            int position = -1;
            int index = 0;
            while (index < array.Length)
            {
                if (targetValue != array[index++])
                    position = -1;
                else
                    return position = index - 1;
            }
            return position;
        }

        /// <summary>
        /// 递归算法
        /// </summary>
        /// <param name="targetValue"></param>
        /// <param name="index"></param>
        /// <returns></returns>
        public int RecursiveLinearSearch(int targetValue, int index)
        {
            int position = -1;
            if (index < array.Length)
            {
                if (targetValue != array[index])
                     position = RecursiveLinearSearch(targetValue, ++index);
                else
                    return position = index;
            }
            return position;
        }

        /// <summary>
        /// 输出.
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            string temporary = string.Empty;
            foreach (int element in array)
                temporary += element + " ";
            return temporary += "\n";
        }
    }
}

//运行结果截图


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
ReSharper反编译C#类库发布时间:2022-07-13
下一篇:
深入理解C系列:不同类型变量的变量名和内存间的关系发布时间:2022-07-13
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap