Ai
1 Star 1 Fork 0

AndyZhang/C-Sharp-Algorithms

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
PigeonHoleSorter.cs 1.63 KB
一键复制 编辑 原始数据 按行查看 历史
Lucas Lemaire 提交于 2015-10-07 19:23 +08:00 . Add PigeonHole sort algorithm with test
using System.Collections.Generic;
using System.Linq;
namespace Algorithms.Sorting
{
/// <summary>
/// Only support IList<int> Sort
/// Also called CountSort (not CountingSort)
/// </summary>
public static class PigeonHoleSorter
{
public static void PigeonHoleSort(this IList<int> collection)
{
collection.PigeonHoleSortAscending();
}
public static void PigeonHoleSortAscending(this IList<int> collection)
{
int min = collection.Min();
int max = collection.Max();
int size = max - min + 1;
int[] holes = new int[size];
foreach (int x in collection)
{
holes[x - min]++;
}
int i = 0;
for (int count = 0; count < size; count++)
{
while (holes[count]-- > 0)
{
collection[i] = count + min;
i++;
}
}
}
public static void PigeonHoleSortDescending(this IList<int> collection)
{
int min = collection.Min();
int max = collection.Max();
int size = max - min + 1;
int[] holes = new int[size];
foreach (int x in collection)
{
holes[x - min]++;
}
int i = 0;
for (int count = size-1; count >= 0; count--)
{
while (holes[count]-- >0)
{
collection[i] = count + min;
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

搜索帮助