Ai
1 Star 1 Fork 0

AndyZhang/C-Sharp-Algorithms

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
LSDRadixSorter.cs 3.24 KB
一键复制 编辑 原始数据 按行查看 历史
Ivandro Ismael 提交于 2018-05-21 07:50 +08:00 . remove redundant "else" / "else if"
using System;
using System.Collections.Generic;
namespace Algorithms.Sorting
{
/// <summary>
/// LSD (Least Significat Digit) Radix Sort.
///
/// Sorts ASCII-encoded strings.
/// Implemented as a static class of extension methods.
/// </summary>
public static class LSDRadixSorter
{
/// <summary>
/// Extension method for sorting strings.
/// </summary>
public static string LSDRadixSort(this string source)
{
if (string.IsNullOrEmpty(source) || source.Length <= 1)
return source;
// LSD Radix Sort the character arrapy representation of the string
var charArray = source.ToCharArray();
charArray.LSDRadixSort();
return new String(charArray);
}
/// <summary>
/// Extension method for sorting character arrays, in place.
/// </summary>
public static void LSDRadixSort(this char[] source)
{
if (source == null || source.Length <= 1)
return;
// extend ASCII alphabet size
int asciiSize = 256;
int length = source.Length;
char[] auxiliary = new char[length];
// compute frequency counts
int[] count = new int[asciiSize + 1];
for (int i = 0; i < length; i++)
count[source[i] + 1]++;
// compute cumulates
for (int r = 0; r < asciiSize; r++)
count[r + 1] += count[r];
// move data
for (int i = 0; i < length; i++)
auxiliary[count[source[i]]++] = source[i];
// copy back
for (int i = 0; i < length; i++)
source[i] = auxiliary[i];
}
/// <summary>
/// Extension method for sorting collections of strings of the same width, in place.
/// </summary>
public static void LSDRadixSort(this IList<String> collection, int stringFixedWidth)
{
// Validate input
if (collection == null || collection.Count <= 1)
return;
for (int i = 0; i < collection.Count; ++i)
if (collection[i] == null || collection[i].Length != stringFixedWidth)
throw new InvalidOperationException("Not all strings have the same width");
// extend ASCII alphabet size
int asciiSize = 256;
int stringsCount = collection.Count;
string[] auxiliary = new string[stringsCount];
for (int d = stringFixedWidth - 1; d >= 0; d--)
{
// compute frequency counts
int[] count = new int[asciiSize + 1];
for (int i = 0; i < stringsCount; i++)
count[collection[i][d] + 1]++;
// compute cumulates
for (int r = 0; r < asciiSize; r++)
count[r + 1] += count[r];
// move data
for (int i = 0; i < stringsCount; i++)
auxiliary[count[collection[i][d]]++] = collection[i];
// copy back
for (int i = 0; i < stringsCount; i++)
collection[i] = auxiliary[i];
}
}
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C#
1
https://gitee.com/strongandyzhang/C-Sharp-Algorithms.git
git@gitee.com:strongandyzhang/C-Sharp-Algorithms.git
strongandyzhang
C-Sharp-Algorithms
C-Sharp-Algorithms
master

搜索帮助