C# 基础·算法篇

1、冒泡排序

  排序

int[] ArrayList = new int[] {81,23,66,34,99,77,98 };
            for (int i = 0; i < ArrayList.Count(); i++)
            {
                for (int j = i; j < ArrayList.Count(); j++)
                {
                    int temp = 0;
                    if (ArrayList[i]>ArrayList[j])
                    {
                        temp = ArrayList[j];
                        ArrayList[j] = ArrayList[i];
                        ArrayList[i] = temp;
                    }
                }
            }
View Code

2、递归算法

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

public static int RecursiveAlgorithm(int num)
        {
            int numOut=0;
            if (num==1||num==2)
            {
                numOut = 1;
            }
            else
            {
                numOut = RecursiveAlgorithm(num-1) + RecursiveAlgorithm(num - 2);
            }
            return numOut;
        }
View Code

 3、字符串反转

public static string Sort1(int[] num)
        {
            int temp;
            for (int i = 0; i < num.Length; i++)
            {
                for (int j = i+1; j < num.Length; j++)
                {
                    if (num[i]<num[j])
                    {
                        temp = num[j];
                        num[j] = num[i];
                        num[i] = temp;
                    }
                }
            }
            StringBuilder sb = new StringBuilder();
            foreach (var item in num)
            {
                sb.Append(item+"|");
            }
            return sb.ToString();
        }

        public static string Sort2(int[] num)
        {
            int temp;
            for (int i = 0; i < num.Length; i++)
            {
                temp = num[i];
                int j = i;
                while (j > 0 && num[j - 1] > temp)
                {  //通过盘点,值一次次提前
                    num[j] = num[j - 1];
                    --j;
                }
                num[j] = temp;
            }
            StringBuilder sb = new StringBuilder();
            foreach (var item in num)
            {
                sb.Append(item + "|");
            }
            return sb.ToString();
        }

        public static string test1(string str)
        {
            Char[] arr = str.ToCharArray();
            Array.Reverse(arr);
            return new string(arr);
        }
        public static string Test2(string str)
        {
            int length = str.Length;
            char[] chr = new char[length];
            for (int i = 0; i < length; i++)
            {
                chr[i] =str[length-i-1];
            }
            return new string(chr);
        }

        public static string Test3(string str)
        {
            StringBuilder sb = new StringBuilder(str.Length);
            for (int i = 0; i < str.Length; i++)
            {
                sb.Append(str[str.Length-i-1]);
            }
            return sb.ToString();
        }
View Code

 

posted on 2016-06-20 14:47  月&&生  阅读(225)  评论(0编辑  收藏  举报