多个数组中的排序算法;二叉搜索功能
Sorting Algorithms in Multiple Arrays; Binary Search Function
所以我应该从一个输入.txt文件中获取1000000个数字,并将它们存储到一个大小为10000的术语数组和另一个大小为10000的每个术语的频率数组中。现在,我只是尝试对一系列 7 个数字进行排序:
0
1 阿拉伯数字
0
3 阿拉伯数字
0
我应该得到输出:
0 3
2 2
1 1
3 1
但相反。我正在得到
0 3
1 2
1 1
3 1
出于某种原因,它没有将两者放在它需要的位置。我假设Insert()
函数存在问题。但我找不到它。另外,作为旁注。我尝试使用1,000,000个数字运行该程序,但它只是说SearchAndSort.exe失败了,需要关闭。
请帮帮我!我敢打赌,某处只是一些小的语法错误。这是我的主要.cpp文件
#include <cstdlib>
#include <iostream>
#include <fstream>
#include "TermTable.h"
using namespace std;
int main()
{
TermTable termTableObj;
ifstream fin("input.txt");
if (fin.is_open())
{
cout << "Open" << endl;
int num;
while (fin >> num)
{
termTableObj.Insert(num);
}
termTableObj.Sort();
termTableObj.Display();
}
return 0;
}
这是我的术语表.cpp实现文件。
#include <iostream>
#include <fstream>
#include <cstdlib>
#include "TermTable.h"
using namespace std;
int TermTable::BinarySearch(int searchValue)
{
int first, last, middle, position;
bool found;
first = 0;
last = currentAmount - 1;
found = false;
position = -1;
while (!found && first <= last)
{
middle = (first + last) / 2;
if (termArray[middle] == searchValue)
{
found = true;
position = middle;
}
else if (termArray[middle] > searchValue)
last = middle - 1;
else
first = middle + 1;
}
return position;
}
void TermTable::Sort()
{
int temp;
bool swapOccurred;
do {
swapOccurred = false;
for (int count = 0; count < (currentAmount - 1); count++)
{
if (frequencyArray[count] < frequencyArray[count + 1])
{
temp = frequencyArray[count];
frequencyArray[count] = frequencyArray[count + 1];
frequencyArray[count + 1] = temp;
temp = termArray[count];
termArray[count] = frequencyArray[count + 1];
termArray[count + 1] = temp;
swapOccurred = true;
}
}
} while (swapOccurred);
}
void TermTable::Insert(int value)
{
int position = BinarySearch(value);
if (position != -1)
{
frequencyArray[position] += 1;
}
else
{
int temp;
termArray[currentAmount] = value;
frequencyArray[currentAmount] = 1;
currentAmount++;
for (int i = currentAmount + 1; i >= 0; i--)
{
if (termArray[i] < termArray[i - 1])
{
temp = termArray[i];
termArray[i] = termArray[i - 1];
termArray[i - 1] = temp;
temp = frequencyArray[i];
frequencyArray[i] = frequencyArray[i - 1];
frequencyArray[i - 1] = temp;
}
else
{
break;
}
}
}
}
void TermTable::Display()
{
ofstream fout("output.txt");
for (int j = 0; j < 10000; j++)
{
fout << termArray[j] << " " << frequencyArray[j] << endl;
}
}
这是我的类定义:
#include <cstdlib>
#include <iostream>
using namespace std;
// class specification
class TermTable
{
public:
// constructor
TermTable()
{
currentAmount = 0;
for (int i = 0; i < 10000; i++)
{
termArray[i] = 0;
}
for (int i = 0; i < 10000; i++)
{
frequencyArray[i] = 0;
}
};
// member functions
int BinarySearch(int searchValue);
void Insert(int value);
void Sort();
void Display();
// destructor
~TermTable()
{
return;
}
private:
// data
int currentAmount;
int termArray[10000];
int frequencyArray[10000];
};
看起来您只需要输入值的标准直方图,并按频率递减的顺序列出它们。
因此,让我们执行通常的插入到映射中(按值作为键),然后按频率递减对进行排序。
住在科里鲁
#include <iostream> // cin/cout
#include <algorithm> // for_each
#include <iterator> // istream_iterator
#include <map>
#include <vector>
#include <functional> // mem_fn
using namespace std;
using Histo = map<int, size_t>;
int main() {
Histo histo;
std::for_each(std::istream_iterator<int>(std::cin), {}, [&](int i) { histo[i]++; });
vector<pair<int, size_t>> v(begin(histo), end(histo));
sort(begin(v), end(v), [](Histo::value_type a, Histo::value_type b) { return a.second > b.second; });
for(auto& p : v)
std::cout << p.first << " " << p.second << "n";
}
指纹
0 3
2 2
1 1
3 1
相关文章:
- 防弹站树搜索功能C++
- 迭代向量以获得搜索功能
- Sinlge 链表,C++,删除所有和搜索功能的问题
- 如何将搜索功能添加到数组中
- 搜索功能使程序崩溃
- 替罪羊搜索功能
- 二叉搜索功能始终返回 -1 值而不是索引
- 如何改进我的矢量搜索功能
- 链接列表中的搜索功能问题
- 如何将搜索功能添加到WXWIDGET C 中的TextCtrl
- 二进制搜索功能不起作用
- 这个树搜索功能让我发疯.它返回 NULL,但不知何故主函数中的值发生了变化
- 看不懂二叉搜索功能
- C++ 二叉搜索功能不打印
- 二进制树搜索功能
- 二叉搜索树:搜索功能问题
- C++调用-搜索功能
- 我需要在C++中构建一些单词搜索功能,但似乎它不起作用.任何线索
- 多个数组中的排序算法;二叉搜索功能
- 链表中的搜索功能 - C++