mirror of
https://github.com/krahets/hello-algo.git
synced 2026-05-12 11:37:08 +08:00
build
This commit is contained in:
@@ -219,35 +219,29 @@ comments: true
|
||||
|
||||
```csharp title="bucket_sort.cs"
|
||||
/* 桶排序 */
|
||||
void bucketSort(float[] nums)
|
||||
{
|
||||
void bucketSort(float[] nums) {
|
||||
// 初始化 k = n/2 个桶,预期向每个桶分配 2 个元素
|
||||
int k = nums.Length / 2;
|
||||
List<List<float>> buckets = new List<List<float>>();
|
||||
for (int i = 0; i < k; i++)
|
||||
{
|
||||
for (int i = 0; i < k; i++) {
|
||||
buckets.Add(new List<float>());
|
||||
}
|
||||
// 1. 将数组元素分配到各个桶中
|
||||
foreach (float num in nums)
|
||||
{
|
||||
foreach (float num in nums) {
|
||||
// 输入数据范围 [0, 1),使用 num * k 映射到索引范围 [0, k-1]
|
||||
int i = (int)num * k;
|
||||
// 将 num 添加进桶 i
|
||||
buckets[i].Add(num);
|
||||
}
|
||||
// 2. 对各个桶执行排序
|
||||
foreach (List<float> bucket in buckets)
|
||||
{
|
||||
foreach (List<float> bucket in buckets) {
|
||||
// 使用内置排序函数,也可以替换成其他排序算法
|
||||
bucket.Sort();
|
||||
}
|
||||
// 3. 遍历桶合并结果
|
||||
int j = 0;
|
||||
foreach (List<float> bucket in buckets)
|
||||
{
|
||||
foreach (float num in bucket)
|
||||
{
|
||||
foreach (List<float> bucket in buckets) {
|
||||
foreach (float num in bucket) {
|
||||
nums[j++] = num;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user