C#算法设计排序篇之09-基数排序(附带动画演示程序)
基数排序(Radix Sort)
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/691 访问。
基数排序属于“分配式排序”(Distribution Sort),它是透过键值的部份信息,将要排序的元素分配至某些“桶”中,藉以达到排序的作用,基数排序法是属于稳定性的排序,其时间复杂度为 ,其中r为所采取的基数,而m为堆数,在某些时候,基数排序法的效率高于其它的稳定性排序法。
示例:
public class Program {
public static void Main(string[] args) {
int[] array = { 43, 69, 11, 72, 28, 21, 56, 80, 48, 94, 32, 8 };
RadixSort(array, 10);
ShowSord(array);
Console.ReadKey();
}
private static void ShowSord(int[] array) {
foreach (var num in array) {
Console.Write($"{num} ");
}
Console.WriteLine();
}
public static void RadixSort(int[] array, int bucketNum) {
int maxLength = MaxLength(array);
//创建bucket时,在二维中增加一组标识位,其中bucket[x, 0]表示这一维所包含的数字的个数
//通过这样的技巧可以少写很多代码
int[,] bucket = new int[bucketNum, array.Length + 1];
for (int i = 0; i < maxLength; i++) {
foreach (var num in array) {
int bit = (int)(num / Math.Pow(10, i) % 10);
bucket[bit, ++bucket[bit, 0]] = num;
}
for (int count = 0, j = 0; j < bucketNum; j++) {
for (int k = 1; k <= bucket[j, 0]; k++) {
array[count++] = bucket[j, k];
}
}
//最后要重置这个标识
for (int j = 0; j < bucketNum; j++) {
bucket[j, 0] = 0;
}
}
}
private static int MaxLength(int[] array) {
if (array.Length == 0) return 0;
int max = array[0];
for (int i = 1; i < array.Length; i++) {
if (array[i] > max) max = array[i];
}
int count = 0;
while (max != 0) {
max /= 10;
count++;
}
return count;
//return (int)Math.Log10(max) + 1;
}
}
以上是基数排序算法的一种实现,以下是这个案例的输出结果:
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/691 访问。
8 11 21 28 32 43 48 56 69 72 80 94
分析:
基数排序算法的时间复杂度为: ,其中r为所采取的基数,m为堆数。
AlgorithmMan:
AlgorithmMan by Iori,AlgorithmMan是使用C#开发的一套用于算法演示的工具。
C#算法设计排序篇之09-基数排序(附带动画演示程序)的更多相关文章
- C#算法设计排序篇之04-选择排序(附带动画演示程序)
选择排序(Selection Sort) 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/681 访问. 选择排序是一种简 ...
- C#算法设计排序篇之06-堆排序(附带动画演示程序)
堆排序(Heap Sort) 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/685 访问. 堆排序是指利用堆积树(堆)这 ...
- C#算法设计排序篇之08-计数排序(附带动画演示程序)
计数排序(Counting Sort) 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/689 访问. 计数排序是一个非基 ...
- C#算法设计排序篇之07-希尔排序(附带动画演示程序)
希尔排序(Shell's Sort) 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/687 访问. 希尔排序是插入排序的 ...
- C#算法设计排序篇之05-归并排序(附带动画演示程序)
归并排序(Merge Sort) 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/683 访问. 归并排序是建立在归并操作 ...
- C#算法设计排序篇之03-直接插入排序(附带动画演示程序)
直接插入排序(Straight Insertion Sort) 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/679 访 ...
- C#算法设计排序篇之02-快速排序(附带动画演示程序)
快速排序(Quick Sort) 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/677 访问. 快速排序由C. A. R ...
- C#算法设计排序篇之01-冒泡排序(附带动画演示程序)
冒泡排序(Bubble Sort) 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/672 访问. 它重复地访问要排序的元 ...
- C#算法设计排序篇之10-桶排序(附带动画演示程序)
桶排序(Bucket Sort) 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/693 访问. 桶排序的工作原理是将数组 ...
随机推荐
- python发送邮件插件
github链接:https://github.com/573734817pc/SendEmailPlug-in.git 说明: 1.该插件功能为发送邮件. 2.基于python编写. 3.使用的时候 ...
- Web For Pentester靶场(xss部分)
配置 官网:https://pentesterlab.com/ 下载地址:https://isos.pentesterlab.com/web_for_pentester_i386.iso 安装方法:虚 ...
- 理解Spring(一):Spring 与 IoC
目录 什么是 Spring Spring 的整体架构 什么是 IoC Bean 的概念 Spring 的基本使用 Spring 的两种 IoC 容器 Spring 容器的基本工作原理 Spring B ...
- Python Ethical Hacking - Malware Analysis(2)
Filtering Command Output using Regex #!/usr/bin/env python import smtplib import subprocess import r ...
- scratch编程体感游戏
体感游戏有很多种,最常见的就是摄像头和声控了,今天我们要用scratch编写一系列的体感游戏!!!是不是很激动呢? 首先我们来编摄像头类的: No.1拳头打幽灵 挥动头就能打到幽灵了哟! 具体程序如下 ...
- 从零开始一起学Blazor WebAssembly 开发(4)
登录模块基本完成了,登录主要用了以下几个点: 1.后端采用的Abp Vnext 框架,这个框架自带的IdentityServer4用户角色权限控制,这个框架登录研究了好一阵子,有几个坑这里说下: 1) ...
- 理解Spring(二):AOP 的概念与实现原理
目录 什么是 AOP AOP 的基本术语 Spring AOP 的简单应用 Spring AOP 与动态代理 Spring AOP 的实现原理(源码分析) 扩展:为什么 JDK 动态代理要求目标类必须 ...
- NanoHTTPD服务
需要导入nanohttpd2.3,jar包 继承NanoHTTPD public class HttpServer extends NanoHTTPD { public HttpServer(int ...
- Python2.7 PicklingError: Can't pickle <type 'instancemethod'>: attribute lookup __builtin__.instancemethod failed 问题解决
# 报错信息 PicklingError: Can't pickle <type 'instancemethod'>: attribute lookup __builtin__.insta ...
- map 函数基本写法
map(需要对对象使用的函数,要操作的对象) 函数可以是自定义的,也可以是内置函数的,或者 lambda 匿名函数 操作的对象多为 可迭代对象可以是函数名的列表集合 2020-05-04