C# 数据结构基本算法 持续更新

这里是一些c#的基本算法 排序查找之类 小白必看 大佬绕道.... 

我会持续更新.....

 
    /// 
    /// 查找一个乱序数组中 第K大的值
    /// 
    /// 数组
    /// 第几大的数字
    /// 
    public int FindKthLargest(int[] array, int k)
    {
        return FindK(array, k, 0, array.Length - 1);
    }

    private int FindK(int[] array, int k, int start, int end)
    {
        int low = start;
        int high = end;
        int temp = array[low];

        while (low < high)
        {
            while (low < high && array[high] <= temp)
            {
                high--;
            }

            array[low] = array[high];
            while (low < high && array[low] >= temp)
            {
                low++;
            }

            array[high] = array[low];
        }

        array[high] = temp;
        if (high == k - 1)
        {
            return temp;
        }
        else if (high > k - 1)
        {
            return FindK(array, k, start, high - 1);
        }
        else
        {
            return FindK(array, k, high + 1, end);
        }
    }    

      /// 
        /// 字符串相乘
        /// 
        /// 第一个值
        /// 第二个值
        /// 
        public string multiply(string num1, string num2) {
            if (num1 == "0" || num2 == "0") return "0";
            char[] num1CharArr = num1.ToCharArray();
            char[] num2CharArr = num2.ToCharArray();
            Array.Reverse(num1CharArr) ;
            Array.Reverse(num2CharArr) ;
            //声明一个存储数据的空list 并且初始化
            List sumTemp = new List();
            for (int i = 0; i < (num1.Length + num2.Length); i++)
            {
                sumTemp.Add(0);
            }
            for (int i = 0; i < num1CharArr.Length; i++)
            {
                for (int j = 0; j < num2CharArr.Length; j++)
                {
                    sumTemp[i + j] += int.Parse((num1CharArr[i]).ToString()) * int.Parse((num2CharArr[j]).ToString());
                }
            }

            int temp = 0;
            string sumStr = ""; 
            List resTemp = new List();
            for (int i = 0; i < (num1.Length + num2.Length); i++)
            {
                resTemp.Add(0);
            }

            for (int i = 0; i < sumTemp.Count; i++)
            {
                sumTemp[i] += temp;
                temp = sumTemp[i] / 10;
                sumTemp[i] = sumTemp[i] % 10;
                resTemp[i] = sumTemp[i];
                sumStr += sumTemp[i].ToString();
            }
            char[] resArr = sumStr.ToCharArray();
            Array.Reverse(resArr);

            string resStr = new string(resArr);
            int zeroInedx = 0;
            while (resArr[zeroInedx] == '0') {
                zeroInedx++;
            }
            return resStr.Substring(zeroInedx);
        }
    }        

         /// 
        /// 选择排序 
        /// 
        /// 需要排序的int数组
        /// minToMax 从小到大
        /// 
        public static int[] SelectSort(int[] array, bool minToMax = true) {
            for (int i = 0; i < array.Length-1; i++)
            {
                for (int j = i+1; j < array.Length; j++)
                {   
                     if (minToMax ? array[i] > array[j] : array[i] < array[j]) {
                        int tempNum = array[i];
                        array[i] = array[j];
                        array[j] = tempNum;
                    } 
                }
            }
            return array;
        }
   

        /// 
        /// 冒泡排序 
        /// 
        /// 需要排序的int数组
        /// minToMax 从小到大
        /// 
        public static int[] BubbleSort (int[] array, bool minToMax = true) {
            for (int i = 0; i < array.Length - 1; i++) {
                for (int j = array.Length - 1; j > i; j--) {
                    if (minToMax ? array[j] < array[j - 1] : array[j] > array[j - 1]) {
                        int tempNum = array[j];
                        array[j] = array[j - 1];
                        array[j - 1] = tempNum;
                    }
                }
            }
            return array;
        }


        /// 
        /// 二分查找
        /// 
        /// 有序的int数组
        /// 要找的值
        /// 开始索引 = 0
        /// 结束索引= array.length 
        /// 
        public static int BinarySearch (int[] array, int key, int low, int high) {
            if (low > high) return -1;
            int tempIndex = low + (high - low) / 2;
            if (array[tempIndex] == key) return tempIndex;
            else if (array[tempIndex] > key) return BinarySearch (array, key, low, tempIndex - 1);
            else return BinarySearch (array, key, tempIndex + 1, high);
        }

 

你可能感兴趣的:(Unity)