- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
因此,我正在为一个学校项目试验不同类型的基数排序,我们必须尽快对 500,000 个随机整数(我自己生成,每个整数的界限在 0 到 MaxValue 之间)进行排序。我首先进行了基数为 10 的 LSD(最低有效数字)基数排序,对 500,000 个随机整数进行排序平均需要 110 到 115 毫秒。这是它的代码:
public static int[] RadixSort(int[] RandomNumbers)
{
List<int>[] Buckets = new List<int>[10];
int singleDigit = 0;
int[] temp;
int[] mult = new int[10] {1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000};
for (int z = 0; z < 10; z++)
{
Buckets[0] = new List<int>();
Buckets[1] = new List<int>();
Buckets[2] = new List<int>();
Buckets[3] = new List<int>();
Buckets[4] = new List<int>();
Buckets[5] = new List<int>();
Buckets[6] = new List<int>();
Buckets[7] = new List<int>();
Buckets[8] = new List<int>();
Buckets[9] = new List<int>();
if (z == 0)
{
temp = (int[])RandomNumbers.Clone();
}
else
{
temp = (int[])RandomNumbers.Clone();
for (int i = 0; i < SIZE; i++)
{
temp[i] /= (mult[z]);
}
}
for (int i = 0; i < SIZE; i++)
{
singleDigit = temp[i] % 10;
Buckets[singleDigit].Add(RandomNumbers[i]);
}
List<int> NewList = new List<int>(SIZE);
for (int i = 0; i < 10; i++)
{
NewList.AddRange(Buckets[i]);
}
int[] NewArray = NewList.ToArray();
RandomNumbers = NewArray;
}
return RandomNumbers;
}
但是,我听说在位移位基数排序中使用二进制更快。因此,我创建了一个基于掩码的位移位基数排序,总体上看起来不那么困惑,其中进行的操作也更少,但它的平均排序速度约为 250 毫秒。这是它的代码:
public static int[] BitShiftRadixSort(int[] RandomNumbers)
{
List<int>[] Buckets = new List<int>[2];
int binary;
int mask;
for (int shift = 0; shift < 32; shift++)
{
Buckets[0] = new List<int>(SIZE);
Buckets[1] = new List<int>(SIZE);
mask = 1 << shift;
for (int i = 0; i < SIZE; i++)
{
binary = RandomNumbers[i] & mask;
if (binary != 0)
{
Buckets[1].Add(RandomNumbers[i]);
}
else
{
Buckets[0].Add(RandomNumbers[i]);
}
}
List<int> NewList = new List<int>(SIZE);
for (int i = 0; i < 2; i++)
{
NewList.AddRange(Buckets[i]);
}
int[] NewArray = NewList.ToArray();
RandomNumbers = NewArray;
}
return RandomNumbers;
}
我原以为位移比 LSD 基数排序更快,但事实并非如此。 C# 中的数学运算是否经过高度优化?我会感谢大家的意见!
最佳答案
根据马特·蒂默曼 (Matt Timmerman) 关于使用基数 256 的建议,此版本的无符号整数对于 500,000 个 32 位无符号整数大约需要 10 毫秒。对于有符号整数,您需要对符号位进行补码并将它们视为无符号整数(然后将符号补回来)。
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
namespace RadixSort
{
class Program
{
static void RadixSort(uint [] a, uint count)
{
uint [,] mIndex = new uint [4,256]; // count / index matrix
uint [] b = new uint [count]; // allocate temp array
uint i,j,m,n;
uint u;
for(i = 0; i < count; i++){ // generate histograms
u = a[i];
for(j = 0; j < 4; j++){
mIndex[j,(u & 0xff)]++;
u >>= 8;
}
}
for(j = 0; j < 4; j++){ // convert to indices
m = 0;
for(i = 0; i < 256; i++){
n = mIndex[j,i];
mIndex[j,i] = m;
m += n;
}
}
for(j = 0; j < 4; j++){ // radix sort
for(i = 0; i < count; i++){ // sort by current lsb
u = a[i];
m = (u>>((int)(j<<3)))&0xff;
b[mIndex[j,m]++] = u;
}
uint [] t = a; // swap references
a = b;
b = t;
}
}
static void Main(string[] args)
{
const int SIZE = 500000;
uint [] a = new uint[SIZE];
uint u;
Random r = new Random(1);
Stopwatch sw = new Stopwatch();
for (uint i = 0; i < a.Length; i++)
{
u = (uint)r.Next(1 << 16);
u = (u << 16) | (uint)r.Next(1 << 16);
a[i] = u;
}
sw.Start();
RadixSort(a, (uint)a.Length);
sw.Stop();
for (uint i = 1; i < a.Length; i++)
{
if(a[i] < a[i-1])
{
Console.WriteLine("failed");
break;
}
}
Console.WriteLine("milliseconds: {0:D}\n",sw.ElapsedMilliseconds);
}
}
}
关于c# - 为什么我的 Base 10 LSD 基数排序比我的位移位基数排序更快?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55984171/
如果我不定义自己的构造函数,Base *b = new Base; 与 Base *b = new Base(); 之间有什么区别吗? 最佳答案 初始化是标准中要遵循的一种 PITA...然而,这两个
是否有现成的函数可以在 C# 中进行基本转换?我希望将以 26 为基数和以 27 为基数的数字转换为以 10 为基数。我可以在纸上完成,但我不是一个非常有经验的程序员,如果可能的话,我宁愿不要从头开始
JNA 中'base'是什么意思 Pointer.getPointerArray(long base) Pointer.getStringArray(long base) ? JNA Document
我正在做一个将数字从 10 进制转换为 2 进制的基本程序。我得到了这段代码: #include #include #include #include using namespace std;
“假设以下代码: public class MultiplasHerancas { static GrandFather grandFather = new GrandFather();
当我分析算法的时候,我突然问自己这个问题,如果我们有三元计算机时间复杂度会更便宜吗?还是有任何基础可以让我们构建计算机,这样时间复杂度分析就无关紧要了?我在互联网上找不到太多,但是基于三元的计算机在给
一个简化的场景。三个类,GrandParent,Parent 和 Child。我想要做的是利用 GrandParent 和 Parent 构造函数来初始化一个 Child 实例。 class Gran
我编写了一个简单的函数来将基数为 10 的数字转换为二进制数。我编写的函数是我使用我所知道的简单工具的最佳尝试。我已经在这个网站上查找了如何执行此操作的其他方法,但我还不太了解它。我确定我编写的函数非
我尝试了以下代码将数字从 base-10 转换为另一个 base。如果目标基地中没有零(0),它就会工作。检查 79 和 3 并正确打印正确的 2221。现在尝试数字 19 和 3,结果将是 21 而
这个问题在这里已经有了答案: Is Big O(logn) log base e? (7 个答案) 关闭 8 年前。 Intro 练习 4.4.6 的大多数解决方案。算法第三版说,n*log3(n)
如何判断基类(B)的指针是否(多态)重写了基类的某个虚函数? class B{ public: int aField=0; virtual void f(){}; }; class C
我测试了这样的代码: class A { public A() { } public virtual void Test () { Console.WriteL
两者都采用相同的概念:定义一些行和列并将内容添加到特定位置。但是 Grid 是最常见的 WPF 布局容器,而 html 中基于表格的布局是 very controversial .那么,为什么 WPF
我试图在 JS 中“获得”继承。我刚刚发现了一种基本上可以将所有属性从一个对象复制到另一个对象的简洁方法: function Person(name){ this.name="Mr or Miss
class A { public override int GetHashCode() { return 1; } } class B : A { pu
我有一个 Base32 信息哈希。例如IXE2K3JMCPUZWTW3YQZZOIB5XD6KZIEQ ,我需要将其转换为base16。 我怎样才能用 PHP 做到这一点? 我的代码如下所示: $ha
我已经使用其实验界面对 Google Analytics 进行了一些实验,一切似乎都运行良好,但我无法找到 Google Analytics 属性如何达到变体目标的答案,即归因 session - 基
if (state is NoteInitial || state is NewNote) return ListView.builder(
MSVC、Clang 和 GCC 不同意此代码: struct Base { int x; }; struct Der1 : public Base {}; struct Der2 : public
我已经尝试构建一个 Base 10 到 Base 2 转换器... var baseTen = window.prompt("Put a number from Base 10 to conver
我是一名优秀的程序员,十分优秀!