算法 顺序查找

顺序查找

也叫线性查找,从列表的第一个元素开始,一个一个查直到查到为止,没错,其实就是循环,时间复杂度是O(n)

        public static void shunxu(int[] array, int num)
        {
            bool flag = false;
            for (int i = 0;i<array.Length;i++)
            {
                if (array[i] == num)
                {
                    Console.WriteLine(string.Format("数字{0}在数组的第{1}个位置", num, i+1));
                    flag = true;
                    break;
                }
            }
            if (!flag)
                Console.WriteLine("这个数字在数组中不存在");
        }

 

posted @ 2021-05-20 22:10  luytest  阅读(128)  评论(0编辑  收藏  举报