当我调用sort数组函数时,这个程序运行得非常慢。如果我在排序之前打印出数组的元素,它会在几秒钟内运行。如果没有,有时需要一分钟来运行一个非常短的文本文件。这个程序的第一个命令行参数是一本"书" (包含全部小写字的文本文件)和第二个命令行参数(x)表示最常出现的x个字出现的次数。任何有关如何快速排序结构数组的帮助都会非常有用!
using namespace std;
//void insertionSort(struct count array[],int size);
void sortArray(struct count array[], int size);
bool ExcludeCommon(string word);
struct count
{
string word;
int frequency;
};
int main(int argc, char *argv[])
{
ifstream filename;
filename.open(argv[1]);
ifstream num_words;
int num = stoi((argv[2]));
string one_word;
int size = 100;
count *count_array = new count[size];
int total_non_comm=0;
int words_in_arr = 0;
int double_count=0;
while (filename >> one_word)
{
int i;
int freq = 1;
bool found = false;
if(ExcludeCommon(one_word)==0)
{
if(total_non_comm>=size)
{
count *new2= new count[size*2];
for(int i=0; i<size; i++)
{
new2[i]=count_array[i];
}
size = size*2;
delete[] count_array;
count_array=new2;
double_count++;
}
for(i=0; i<=words_in_arr; i++)
{
if(count_array[i].word == one_word)
{
count_array[i].frequency++;
found=true;
total_non_comm++;
}
}
if(found == false)
{
struct count add;
add.word = one_word;
add.frequency = freq++;
count_array[words_in_arr]=add;
words_in_arr++;
total_non_comm++;
}
found = false;
}
}
sortArray(count_array, size);
for(int i=0; i<num;i++)
{
cout<<count_array[i].frequency<< " - " << count_array[i].word<<endl;
}
cout<<"#"<<endl;
cout<<"Array doubled: "<<double_count-3<<endl;
cout<<"#"<<endl;
cout<<"Unique non-common words: " <<words_in_arr<<endl;
cout<<"#"<<endl;
cout<<"Total non-common words: " << total_non_comm <<endl;
};
bool ExcludeCommon(string word)
{
if (word == "the" || word == "be" || word == "to" || word == "of" ||
word == "and" || word == "a" || word == "in" || word == "that" ||
word == "have" ||word == "i" || word == "it" || word == "for" || word == "not" ||
word == "on" || word == "with" || word == "he" || word == "as" ||
word == "you" || word == "do" || word == "at" || word == "this" ||
word == "but" || word == "his" || word == "by" || word == "from"||
word == "they" || word == "we" || word == "say" || word == "her" ||
word == "she" || word == "or" || word == "an" || word == "will" ||
word == "my" || word == "one" || word == "all" || word == "would" ||
word == "there" || word == "their" || word == "what" || word == "so" ||
word == "up" || word == "out" || word == "if" || word == "about" ||
word == "who" || word == "get" || word == "which" ||word == "go" || word == "me")
{return 1;
}
return 0;
};
void sortArray(struct count array[],int size)
{
int cur_pos = 0;
string the_word;
for(int i=0; i<(size); i++)
{
for(int j=size-1; j>=i; j--)
{
if((array[j+1].frequency)>(array[j].frequency))
{
cur_pos = array[j].frequency;
the_word = array[j].word;
array[j].frequency = array[j+1].frequency;
array[j].word = array[j+1].word;
array[j+1].frequency = cur_pos;
array[j+1].word = the_word;
}
}
}
};
答案 0 :(得分:1)
您有一个struct count
,然后您还有using namespace std;
。您是否意识到有std::count
?
尽量不要使用
using namespace std;
另外,将struct count
更改为struct Count
。
出现排序功能问题,您可以使用std::sort
实现它。
struct CountCompare
{
bool operator()(Count const& lhs, Count const& rhs)
{
return (lhs.frequency < rhs.frequency);
}
};
void sortArray(struct Count array[],int size)
{
std::sort(array, array+size, CountCompare());
};
这应该比你拥有的要快得多(N.logN vs N ^ 2)。