LSD基数排序c++代码
LSD radix sort c++ code
我编写了这段代码来实现字符串的lsd基数排序算法。
逻辑:我们从最低有效数字开始,首先对这个数字进行排序。然后,我们移动到左边的下一位,以此类推。count[]
包含每个英文字母在特定数字位置的频率。count[1]
代表'a'
的频率,count[2]
代表'b'
的频率。等等……和count[0]=0
。然后,我们计算累积计数。现在我们再次遍历字符串数组并计算aux
数组中的适当位置。例如:如果count[2] = 4
和count[1]=1
对应一个特定的数字位置,这意味着'b'
在该位置的单词将占据aux[1]
, aux[2]
, aux[3]
。
#include<iostream>
#include<string>
using namespace std;
int main()
{
string arr[]={"dab","cab","fad","bad","dad","ebb","ace","add","fed","bed","fee","bee"};
string aux[12]={"dab","cab","fad","bad","dad","ebb","ace","add","fed","bed","fee","bee"};
int count[27]={0};
for(int i=2;i>=0;i--)
{
for(int j=0;j<12;j++)
count[static_cast<int>(arr[j][i])-64]++;
cout<<"here"<<endl; //////////////THIS GETS PRINTED
for(int j=0;j<26;j++)
count[j+1]+=count[j]; //calculating cumulative value
cout<<"HERE"<<endl; ///////////////THIS GETS PRINTED
for(int j=0;j<12;j++)
{
int x=count[static_cast<int>(arr[j][i])-65]; //65 because positions for 'a' will be
//determined by count[0], for 'b' will be
// determined by count[1] etc.
aux[x]=arr[j];
cout<<j<<endl; //even j=0 doesn't get printed
count[static_cast<int>(arr[j][i])-65]++;
}
cout<<"here"<<endl;
for(int j=0;j<12;j++)
cout<<aux[j]<<endl;
} //i governs which digit is being compared
return 0;
}
代码在打印here和here后出现段错误。问题出在第三个for j循环中。谁能找出问题所在吗?
有一个小错误…我要减去65这是ascii码代表A而不是A
相关文章:
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 代码在main()中运行,但在函数中出现错误
- 在VS代码中交叉编译Windows与Linux上的MinGW的SDL程序
- 编译包含字符串的代码时遇到问题
- 我在c++代码中生成了一个运行时#3异常
- 如何在linux终端中同时编译和运行c++代码
- 为cl.exe(Visual Studio代码)指定命令行C++版本
- 在Linux for Windows上编译C++代码时出错
- 我的字符计数代码计算错误.为什么
- 孤立代码块在结构中引发异常
- 在编译C++代码(具有dlib和opencv)到WASM时面临问题
- 为什么我的C#代码在调用回C++COM直到Task时会暂停.等待/线程.加入
- 处理小于cpu数据总线的数据类型.(c++转换为机器代码)
- 此代码是否违反一个定义规则
- 为什么我的代码在输出中增加了93天
- 我的简单if-else语句是如何无法访问的代码
- 使用动态分配的数组会导致代码分析发出虚假的C6386缓冲区溢出警告
- 为什么在这个代码结束循环中没有得到结束
- 在c代码之间共享数据的最佳方式
- 这个指针和内存代码打印是什么?我不知道是打印垃圾还是如何打印我需要的值