Skip to content

Commit 43e4f93

Browse files
committed
Add 基数排序算法
1 parent 69422c3 commit 43e4f93

4 files changed

Lines changed: 75 additions & 9 deletions

File tree

‎DotNetGuidePractice/HelloDotNetGuide/Program.cs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ static void Main(string[] args)
1111

1212
#region 常见算法
1313

14+
//基数排序算法.RadixSortRun();
1415
//桶排序算法.BucketSortRun();
1516
//计数排序算法.CountingSortRun();
1617
//堆排序算法.HeapSortRun();
Lines changed: 72 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,78 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Text;
5-
using System.Threading.Tasks;
6-
7-
namespace HelloDotNetGuide.常见算法
1+
namespace HelloDotNetGuide.常见算法
82
{
93
public class 基数排序算法
104
{
5+
public static void RadixSort(int[] array)
6+
{
7+
if (array == null || array.Length < 2)
8+
{
9+
return;
10+
}
11+
12+
//获取数组中的最大值,确定排序的位数
13+
int max = GetMaxValue(array);
14+
15+
//进行基数排序
16+
for (int exp = 1; max / exp > 0; exp *= 10)
17+
{
18+
CountingSort(array, exp);
19+
}
20+
}
21+
22+
private static void CountingSort(int[] array, int exp)
23+
{
24+
int arrayLength = array.Length;
25+
int[] output = new int[arrayLength];
26+
int[] count = new int[10];
27+
28+
//统计每个桶中的元素个数
29+
for (int i = 0; i < arrayLength; i++)
30+
{
31+
count[(array[i] / exp) % 10]++;
32+
}
33+
34+
//计算每个桶中最后一个元素的位置
35+
for (int i = 1; i < 10; i++)
36+
{
37+
count[i] += count[i - 1];
38+
}
39+
40+
//从原数组中取出元素,放入到输出数组中
41+
for (int i = arrayLength - 1; i >= 0; i--)
42+
{
43+
output[count[(array[i] / exp) % 10] - 1] = array[i];
44+
count[(array[i] / exp) % 10]--;
45+
}
46+
47+
//将输出数组复制回原数组
48+
for (int i = 0; i < arrayLength; i++)
49+
{
50+
array[i] = output[i];
51+
}
52+
}
53+
54+
private static int GetMaxValue(int[] arr)
55+
{
56+
int max = arr[0];
57+
for (int i = 1; i < arr.Length; i++)
58+
{
59+
if (arr[i] > max)
60+
{
61+
max = arr[i];
62+
}
63+
}
64+
return max;
65+
}
66+
67+
public static void RadixSortRun()
68+
{
69+
int[] array = { 19, 27, 46, 48, 99, 888, 50, 2, 4, 44, 47, 36, 38, 15, 26, 5, 3 };
70+
71+
Console.WriteLine("排序前数组:" + string.Join(", ", array));
72+
73+
RadixSort(array);
1174

75+
Console.WriteLine("排序后数组:" + string.Join(", ", array));
76+
}
1277
}
1378
}

‎DotNetGuidePractice/HelloDotNetGuide/常见算法/桶排序算法.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public static void BucketSort(int[] array)
5555

5656
public static void BucketSortRun()
5757
{
58-
int[] array = { 19, 27, 46, 48, 50, 2, 4, 44, 47, 36, 38, 15, 26, 5, 3, 99, 888, 0, -1 };
58+
int[] array = { 19, 27, 46, 48, 50, 2, 4, 44, 47, 36, 38, 15, 26, 5, 3, 99, 888 };
5959

6060
Console.WriteLine("排序前数组:" + string.Join(", ", array));
6161

‎DotNetGuidePractice/HelloDotNetGuide/常见算法/计数排序算法.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public static void CountingSort(int[] array)
5252

5353
public static void CountingSortRun()
5454
{
55-
int[] array = { 19, 27, 46, 48, 50, 2, 4, 44, 47, 36, 38, 15, 26, 5, 3, 99, 888, 0, -1 };
55+
int[] array = { 19, 27, 46, 48, 50, 2, 4, 44, 47, 36, 38, 15, 26, 5, 3, 99, 888 };
5656

5757
Console.WriteLine("排序前数组:" + string.Join(", ", array));
5858

0 commit comments

Comments
 (0)