• 二分查找


            #region 查找
            public static bool SeqSearch(int[] arr, int value)
            {
                for (int i = 0; i < arr.Length; i++)
                {
                    if (arr[i] == value)
                    {
                        return true;
                    }
                }
                return false;
            }
    
            public static int SeqSearch(int[] arr, int value)
            {
                for (int i = 0; i < arr.Length; i++)
                {
                    if (arr[i] == value)
                    {
                        return i;
                    }
                }
                return -1;
            }
    
            static int FindMin(int[] arr)
            {
                int min = arr[0];
                for (int i = 0; i < arr.Length; i++)
                {
                    if (arr[i] < min)
                    {
                        min = arr[i];
                    }
                }
                return min;
            }
    
            static int FindMax(int[] arr)
            {
                int max = arr[0];
                for (int i = 0; i < arr.Length - 1; i++)
                {
                    if (arr[i] > max)
                    {
                        max = arr[i];
                    }
                }
                return max;
            }
    
            public static int SearchSearch(int[] arr, int value)
            {
                for (int i = 0; i < arr.Length; i++)
                {
                    if (arr[i] == value)
                    {
                        int j = i - 1;
                        swap(arr, ref i, ref j);
                        return i;
                    }
                }
                return -1;
            }
            public static void swap(int[] arr, ref int i, ref int j)
            {
                int temp = arr[i];
                arr[i] = arr[j];
                arr[j] = temp;
            }
    
            public static int SeqSearch(int[] arr, int value)
            {
                for (int i = 0; i < arr.Length - 1; i++)
                {
                    if (arr[i] == value && i > arr.Length * 2)
                    {
                        int j = i - 1;
                        swap(arr, ref i, ref j);
                        return i;
                    }
                    else if (arr[i] == value)
                    {
                        return i;
                    }
                }
                return -1;
            }
    
            public static int SeqSearch(int[] arr, int value)
            {
                for (int i = 0; i < arr.Length - 1; i++)
                {
                    if (arr[i] == value)
                    {
                        int j = i - 1;
                        swap(arr, ref i, ref j);
                        return i;
                    }
                }
                return -1;
            }
            #endregion
    
            #region 二分查找
            public int BinarySearch(int[] arr, int value)
            {
                int low = 0, high = arr.Length - 1, mid;
                while (low <= high)
                {
                    mid = (low + high) / 2;
                    if (mid == value)
                    {
                        return mid;
                    }
                    else
                    {
                        if (value < arr[mid])
                        {
                            high = mid - 1;
                        }
                        else
                        {
                            low = mid + 1;
                        }
                    }
                }
                return -1;
            }
    
            public int BinarySearch(int[] arr, int value, int low, int high)
            {
                if (low > high)
                {
                    return -1;
                }
                else
                {
                    int mid = (low + high) / 2;
                    if (value<mid)
                    {
                        return BinarySearch(arr,value,low,high-1);
                    }
                    else if (value==mid)
                    {
                        return mid;
                    }
                    else
                    {
                        return BinarySearch(arr,value,mid+1,high);
                    }
                }
            }
            #endregion
    View Code
  • 相关阅读:
    树梅派学习之——nano编辑器
    树梅派学习之——输入法安装
    Qt的正则表达式类(QRegExp)
    虚拟机VMware上网配置
    Qt Android环境搭建
    qt5.8.0之静态编译
    qt编译之CONFIG += console
    面向对象的3个特性:封装、继承、多态
    Python笔记:df.loc[]和df.iloc[]的区别
    Python报错ValueError: arrays must all be same length
  • 原文地址:https://www.cnblogs.com/futengsheng/p/7837153.html
Copyright © 2020-2023  润新知