1、递归算法运用

一列数的规则如下: 1、1、2、3、5、8、13、21、34...... 求第30位数是多少

public class MainClass
    {
        public static void Main()
        {
            Console.WriteLine(Foo(30));
        }

        public static int Foo(int i)
        {
            if (i <= 0)
                return 0;
            else if (i > 0 && i <= 2)
                return 1;
            else return Foo(i - 1) + Foo(i - 2);
        }
    }

2、冒泡排序算法

 public void Bubble()
        {
            int[] array = new int[] { 52, 14, 8, 65, 2, 41, 59, 12 };
            int temp = 0;
            for (int i = 0; i < array.Length - 1; i++)
            {
                bool isSort = true;
                for (int j = i + 1; j < array.Length; j++)
                {
                    if (array[j] < array[i])//每次将后面的与前面的[i]比,小则往前冒泡
                    {
                        temp = array[i];
                        array[i] = array[j];
                        array[j] = temp;
                        isSort = false;
                    }
                }
                if (isSort)  //已经是(从小到大)排好序的,那么就不用再比较了,提高性能
                {
                    break;
                }
            }
            for (int i = 0; i < array.Length; i++)
            {
                Console.Write(array[i] + " ");
            }
        }
View Code

相关文章: