1 Star 0 Fork 65

欢喜派 / C-Sharp

forked from 编程语言算法集 / C-Sharp 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
RadixSorter.cs 1.67 KB
一键复制 编辑 原始数据 按行查看 历史
Andrii Siriak 提交于 2021-05-08 21:42 . Cleanup minor things (#221)
namespace Algorithms.Sorters.Integer
{
/// <summary>
/// Radix sort is a non-comparative integer sorting algorithm that sorts data with integer keys by grouping keys by the
/// individual
/// digits which share the same significant position and value. A positional notation is required, but because integers
/// can represent
/// strings of characters (e.g., names or dates) and specially formatted floating point numbers, radix sort is not
/// limited to integers.
/// </summary>
public class RadixSorter : IIntegerSorter
{
/// <summary>
/// Sorts array in ascending order.
/// </summary>
/// <param name="array">Array to sort.</param>
public void Sort(int[] array)
{
var bits = 4;
var b = new int[array.Length];
var rshift = 0;
for (var mask = ~(-1 << bits); mask != 0; mask <<= bits, rshift += bits)
{
var cntarray = new int[1 << bits];
foreach (var t in array)
{
var key = (t & mask) >> rshift;
++cntarray[key];
}
for (var i = 1; i < cntarray.Length; ++i)
{
cntarray[i] += cntarray[i - 1];
}
for (var p = array.Length - 1; p >= 0; --p)
{
var key = (array[p] & mask) >> rshift;
--cntarray[key];
b[cntarray[key]] = array[p];
}
var temp = b;
b = array;
array = temp;
}
}
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C#
1
https://gitee.com/Joypai/C-Sharp.git
git@gitee.com:Joypai/C-Sharp.git
Joypai
C-Sharp
C-Sharp
master

搜索帮助

344bd9b3 5694891 D2dac590 5694891