大小字母排序

static void Main(string[] args)
        {
            Char[] chars = new char[] { 'a', 'D', 'A', 'd', 'm', 'f', 'F', 'e', 'H', 'E', 'B', 'b', 'c', 'C', 'G', 'h', 'g', 'M', 'i', 'I', 'j', 'J', 'k', 'K', 'L', 'N', 'l', 'n', 'a', 'D', 'A', 'd', 'm', 'f', 'F', 'e', 'H', 'E', 'B', 'b', 'c', 'C', 'G', 'h', 'g', 'M', 'i', 'I', 'j', 'J', 'k', 'K', 'L', 'N', 'l', 'n' };//A a D d F f m
            DateTime dtstart = DateTime.Now;
            #region --me--
            ArrayList al = new ArrayList(chars);
            al.Sort();
            ArrayList alist = BublleSort(al);
            for (int i = 0; i < alist.Count; i++)
            {
                Console.WriteLine(alist[i]);
            }
            #endregion
            #region --companion--
            //ArrayList list2 = new ArrayList();
            //list2.Add(chars[0].ToString().ToUpper());
            //foreach (var cha in chars)
            //{
            //    if (!list2.Contains(cha.ToString()))
            //    {
            //        list2.Add(cha.ToString());
            //    }
            //}
            //list2.Sort();
            //for (var i = 0; i < chars.Length; i++)
            //{
            //    for (var j = chars.Length - 1; j > i; j--)
            //    {
            //        string str = "";
            //        if (list2[i].ToString().ToUpper() == list2[j].ToString())
            //        {
            //            str = list2[i].ToString();
            //            list2[i] = list2[j];
            //            list2[j] = str;
            //        }
            //    }
            //}
            //for (var i = 0; i < list2.Count; i++)
            //{
            //    Console.Write(list2[i] + "/r/n");
            //}
            #endregion
            DateTime dtend = DateTime.Now;
            TimeSpan ts = dtend.Subtract(dtstart);
            Console.WriteLine(ts);
            #region --字符排序---
            //char[] newchar = BubbleSort(al);
            //for (int i = 0; i < newchar.Length; i++)
            //{
            //    Console.WriteLine(newchar[i]);
            //}
            #endregion
            #region --字符串排序--
            //string[] strs = new string[] {"a","b","D","f","A","c","B" };
            //Array.Sort(strs);
            //for (int i = 0; i < strs.Length; i++)
            //{
            //    Console.WriteLine(strs[i]);
            //}
            #endregion
            #region --整数数组可以通过集合进行排序--
            //int[] ints = new int[] { 23,55,90,12,23,34,5,42,9};
            //List<int> l = new List<int>();
            //l.AddRange(ints);
            //l.Sort();
            //foreach (int listitem in l)
            //{
            //    Console.WriteLine(listitem.ToString());
            //}
            #endregion
            Console.ReadLine();
        }
        //冒泡
        private static char[] BubbleSort(char[] arrayvalue)
        {
            int loopcount = 0;//循环次数
            int minindex=0;
            char temp;
            for (int i = 0; i < arrayvalue.Length; i++)
            {
                minindex = i;
                for (int j = i; j < arrayvalue.Length; j++)
                {
                    loopcount++;
                    if (arrayvalue[minindex].GetHashCode() > arrayvalue[j].GetHashCode())
                    {
                        minindex = j;
                    }
                }
                temp = arrayvalue[i];
                arrayvalue[i] = arrayvalue[minindex];
                arrayvalue[minindex] = temp;
            }
            return arrayvalue;
        }
        private static ArrayList BublleSort(ArrayList al)
        {
            ArrayList alist = new ArrayList();
            alist.Add(al[0]);
            for (int i = 1; i < al.Count; i++)
            {
                int hcode = Convert.ToInt32(al[i - 1]) + 32;
                if (al.Contains(Convert.ToChar(hcode)))
                {
                    if(!alist.Contains(Convert.ToChar(hcode)))//取消重复
                        alist.Add(Convert.ToChar(hcode));
                }
                if (!alist.Contains(al[i]))
                    alist.Add(al[i]);
            }
            return alist;
        }
        private static int[] BubbleSort(int[] arrayvalue)
        {
            int loopcount = 0;//循环次数
            int minindex = 0;
            int temp;
            for (int i = 0; i < arrayvalue.Length; i++)
            {
                minindex = i;
                for (int j = i; j < arrayvalue.Length; j++)
                {
                    loopcount++;
                    if (arrayvalue[minindex] > arrayvalue[j])
                    {
                        minindex = j;
                    }
                }
                temp = arrayvalue[i];
                arrayvalue[i] = arrayvalue[minindex];
                arrayvalue[minindex] = temp;
            }
            return arrayvalue;
        }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值