003_基础查找算法

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();
        }

/// 
        /// 顺序查找
        /// 
        /// 
        /// 
        /// 
        static bool SeqSearch(int[] arr,int sValue) {
            for (int index = 0; index < arr.Length; index++)
            {
                if (arr[index]==sValue)
                {
                    return true;
                }
            }
            return false;
        }
        /// 
        /// 顺序查找返回位置
        /// 
        /// 
        /// 
        /// 
        static int SeqSearchIndex(int[] arr, int sValue)
        {
            for (int index = 0; index < arr.Length; index++)
                if (arr[index] == sValue)            
                    return index; 
            return -1;
        }
        /// 
        /// 查找最小值
        /// 
        /// 
        /// 
        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;               
        }
        /// 
        /// 查找最大值
        /// 
        /// 
        /// 
        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 };
        /// 
        /// 自组织数据加快顺序查找速度
        /// 
        /// 
        /// 
        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;

        }
        /// 
        /// 递归的二分查找
        /// 
        /// 
        /// 
        /// 
        /// 
        public static int RbinSearch(int value,int lower,int upper) {

            if (lower>upper)
            {
                return -1;
            }
            else
            {
                int mid = (upper+lower) / 2;
                if (value

你可能感兴趣的:(003_基础查找算法)