//字符集 private string _myCharSet = "0123456789qwertyuiopasdfghjklzxcvbnm";
private Random _random = new Random();
string[] GetRandStrings(int size, int minLength, int maxLength)
{
string[] strs = new string[size];
int len = 0;
StringBuilder sb = new StringBuilder(maxLength);
for (int i = 0; i < strs.Length; i++)
{
//先随机确定一个长度
len = _random.Next(minLength, maxLength);
for (int j = 0; j < len; j++)
{
//随机选取一个字符
sb.Append(_myCharSet[_random.Next(_myCharSet.Length)]);
}
strs[i] = sb.ToString();
sb.Clear();
}
return strs;
}
void StringRadixSort(string[] strArray)
{
if (strArray == null
|| strArray.Length == 0
|| strArray.Contains(null))
{
return;
}
//获得字符串的最大长度
int maxLength = 0;
foreach (string s in strArray)
{
if (s.Length > maxLength)
{
maxLength = s.Length;
}
}
//确定字符的整数范围
int rangeStart = _myCharSet[0];
int rangeEnd = _myCharSet[0];
foreach (char ch in _myCharSet)
{
if (ch < rangeStart)
rangeStart = ch;
if (ch >= rangeEnd)
rangeEnd = ch + 1;
}
//也要为"空字符"分配一个桶,其索引为0
int bucketCount = rangeEnd - rangeStart + 1;
LinkedList<string>[] buckets = new LinkedList<string>[bucketCount];
//初始化所有的桶
for (int i = 0; i < buckets.Length; i++)
{
buckets[i] = new LinkedList<string>();
}
//从最后一个字符开始排序
int currentIndex = maxLength - 1;
while (currentIndex >= 0)
{
foreach (string theString in strArray)
{
//如果超出索引,返回'\0'字符(default(char))
char ch = theString.ElementAtOrDefault(currentIndex);
if (ch == default(char))
{ //"空字符"的处理
buckets[0].AddLast(theString);
}
else
{ //将字符映射到桶
int index = ch - rangeStart + 1;
buckets[index].AddLast(theString);
}
}
//从桶里依次取回字符串,完成一趟排序
int i = 0;
foreach (LinkedList<string> bucket in buckets)
{
while (bucket.Count > 0)
{
strArray[i++] = bucket.First();
bucket.RemoveFirst();
}
}
currentIndex--;
}
}
private Dictionary<char, int> _charOrderDict =
new Dictionary<char, int>(_myCharSet.Length);
void BuildCharOrderDict()
{
char[] sortedCharSet = _myCharSet.ToArray();
//使用默认的比较器排序
Array.Sort(sortedCharSet);
//为"空字符"单独创建映射
_charOrderDict.Add(default(char), 0);
for (int i = 0; i < sortedCharSet.Length; i++)
{
// 保存的是字符及其对应的桶的索引
_charOrderDict.Add(sortedCharSet[i], i + 1);
}
}
void StringRadixSort(string[] strArray)
{
if (strArray == null
|| strArray.Length == 0
|| strArray.Contains(null))
{
return;
}
//获得字符串的最大长度
int maxLength = 0;
foreach (string s in strArray)
{
if (s.Length > maxLength)
{
maxLength = s.Length;
}
}
//为每一个字符(包括空字符'\0')分配一个桶
//"空字符"索引应为0
int bucketCount = _myCharSet.Length + 1;
LinkedList<string>[] buckets = new LinkedList<string>[bucketCount];
//初始化所有的桶
for (int i = 0; i < buckets.Length; i++)
{
buckets[i] = new LinkedList<string>();
}
//从最后一个字符开始排序
int currentIndex = maxLength - 1;
while (currentIndex >= 0)
{
foreach (string theString in strArray)
{
//如果超出索引,返回'\0'字符(default(char))
char ch = theString.ElementAtOrDefault(currentIndex);
//根据字符顺序的定义查询字符
int index = _charOrderDict[ch];
buckets[index].AddLast(theString);
}
//从桶里依次取回字符串,完成一趟排序
int i = 0;
foreach (LinkedList<string> bucket in buckets)
{
while (bucket.Count > 0)
{
strArray[i++] = bucket.First();
bucket.RemoveFirst();
}
}
currentIndex--;
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有