我怎样才能让这个c++编程速度更快?
我正在尝试使用合并排序和标准字符串比较对字符串进行排序。问题是,这个程序本来就很慢。当我尝试检查我的解决方案时,我超出了运行时预期。有没有办法可以使用其他数据结构来做同样的事情,但访问速度更快?这是代码。感谢您的帮助。
编辑:我知道使用 sort() 而不是我自己的合并排序算法会更快,但我的要求是使用合并排序。问题在于超出了 SPOJ 的运行时要求。
#include <iostream>
using namespace std;
void MergeSortA(int low , int high);
void MergeA(int low ,int mid ,int high);
string stringData[50000];
int main()
{
int testCases;
int linesInTestCase;
scanf("%d", &testCases);
while(testCases--)
{
scanf("%d", &linesInTestCase);
for(int i = 0; i < linesInTestCase; ++i)
cin >> stringData[i];
// Sort the data
MergeSortA(0, linesInTestCase - 1);
// Print array of strings one by one
for(int i = 0; i < linesInTestCase; ++i) {
cout << stringData[i] << endl;
}
}
return 0;
}
void MergeSortA(int low , int high)
{
int mid = 0;
if(low < high)
{
mid = ((low+high)/2);
MergeSortA(low , mid);
MergeSortA(mid+1,high);
MergeA(low,mid,high);
}
}
void MergeA(int low ,int mid , int high)
{
int i = low, j = mid+1 , k = low;
string Temp[50000];
while(i <= mid && j <= high) {
if( stringData[i] < stringData[j] ) {
Temp[k].assign(stringData[i]);
i++;
}
else {
Temp[k].assign(stringData[j]);
j++;
}
k++;
}
if(i > mid ) {
for(int h = j ;h <= high ; h++ ) {
Temp[k].assign(stringData[h]);
k++;
}
}
else
for(int h = i; h<= mid ; h++ ) {
Temp[k].assign(stringData[h]);
k++;
}
// Copy from low to high
for(int l = low; l <= high ; l++) {
stringData[l].assign(Temp[l]);
}
}
I am trying to sort strings using merge sort and standard string comparison. The problem is, this program is very slow as is. I am exceeding runtime expectations when I am attempting to check my solution. Is there a way I can use some other data structure to do the same thing but be accessed faster? Here is the code. Thank you for your help.
Edit: I understand that using sort() rather than my own merge sort algorithm will be faster, but my requirement states to use merge sort. The problem is exceeding runtime requirements on SPOJ.
#include <iostream>
using namespace std;
void MergeSortA(int low , int high);
void MergeA(int low ,int mid ,int high);
string stringData[50000];
int main()
{
int testCases;
int linesInTestCase;
scanf("%d", &testCases);
while(testCases--)
{
scanf("%d", &linesInTestCase);
for(int i = 0; i < linesInTestCase; ++i)
cin >> stringData[i];
// Sort the data
MergeSortA(0, linesInTestCase - 1);
// Print array of strings one by one
for(int i = 0; i < linesInTestCase; ++i) {
cout << stringData[i] << endl;
}
}
return 0;
}
void MergeSortA(int low , int high)
{
int mid = 0;
if(low < high)
{
mid = ((low+high)/2);
MergeSortA(low , mid);
MergeSortA(mid+1,high);
MergeA(low,mid,high);
}
}
void MergeA(int low ,int mid , int high)
{
int i = low, j = mid+1 , k = low;
string Temp[50000];
while(i <= mid && j <= high) {
if( stringData[i] < stringData[j] ) {
Temp[k].assign(stringData[i]);
i++;
}
else {
Temp[k].assign(stringData[j]);
j++;
}
k++;
}
if(i > mid ) {
for(int h = j ;h <= high ; h++ ) {
Temp[k].assign(stringData[h]);
k++;
}
}
else
for(int h = i; h<= mid ; h++ ) {
Temp[k].assign(stringData[h]);
k++;
}
// Copy from low to high
for(int l = low; l <= high ; l++) {
stringData[l].assign(Temp[l]);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如您所说,这是 SPOJ 问题,我会尝试不改变您的程序结构,但是,这里存在真正的瓶颈:
无论何时调用 MergeA,都会构造 50000 个字符串。每次都会被破坏,阻碍算法。只需添加
static
即可:As you stated that this is SPOJ problem, I will try not alter your structure of your program, However, there is real bottleneck here:
Wnenever MergeA is called, 50000
string
s are constructed & destructed every time, encumbering the algorithm. Just addstatic
there: