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;
}
大小字母排序
最新推荐文章于 2022-06-02 10:43:57 发布