多个数组中的排序算法;二叉搜索功能

Sorting Algorithms in Multiple Arrays; Binary Search Function

本文关键字:搜索 功能 算法 排序 数组      更新时间:2023-10-16

所以我应该从一个输入.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