C#封装:

C#支持的访问修饰符:

  • public:所有对象都可以访问
  • private:对象本身在对象内部可以访问
  • protected:只有该类对象及其子类对象可以访问
  • internal:同一个程序集的对象可以访问
  • protected internal:访问限于当前程序集或派生自包含类的类型

C#方法:

用C#编写的阶乘方法:
C#学习笔记2

C#实现二分法:

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

namespace Bisection
{
    class Bisection
    {
        public void Bise(int[] list) {
            int start;
            int end;
            int temp, mid;
            
            for (int i = 1; i < list.Length;i++ )
            {
                start = 0;
                temp = list[i];
                end = i - 1;
                //定位
                
                while (start <= end)
                {
                    mid = (start + end) / 2;
                    if (list[mid] > temp)
                    {
                        end = mid - 1;
                    }
                    else if (list[mid] < temp)
                    {
                        start = mid + 1;
                    }
                }
                //移动数组
                for (int j = i; j > start; j--)
                {
                    list[j] = list[j - 1];
                }
                //插入
                list[start] = temp;
                
                foreach (int a in list)
                {
                    Console.Write("{0} ",a);
                }
                Console.WriteLine("第{0}次",i);
                
            }
            Console.ReadKey();
        }
        static void Main(string[] args)
        {
            int[] list = { 1, 13, 22, 43, 21, 35, 2, 33, 65, 8, 19 };
            Bisection bise = new Bisection();
            bise.Bise(list);
        }
    }
}

相关文章:

  • 2023-01-12
  • 2021-05-06
  • 2021-12-21
  • 2022-02-17
  • 2021-06-20
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-11-19
  • 2021-06-24
  • 2021-11-15
  • 2021-10-04
  • 2021-10-02
相关资源
相似解决方案