簡體   English   中英

多個數組中的排序算法; 二進制搜索功能

[英]Sorting Algorithms in Multiple Arrays; Binary Search Function

因此,我應該從input.txt文件中提取1000000個數字,並將其存儲到大小為10000的一個術語數組中,並將每個術語的大小為10000的頻率存儲到另一個數組中。現在,我只是在嘗試對一系列序列進行排序7個數字:

0
1個
2
0
3
2
0

我應該得到輸出:

0 3
2 2
1 1
3 1

但反而。 我正進入(狀態

0 3
1 2
1 1
3 1

由於某種原因,它沒有將兩者放在需要的位置。 我假設在Insert()函數中有問題。 但是我找不到。 另外,作為旁注。 我試圖用1,000,000個數字運行該程序,但它只是說SearchAndSort.exe失敗,需要關閉。

請幫幫我! 我敢打賭,某處只是一些小語法錯誤。 這是我的main.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;
}

這是我的TermTable.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];
};

看起來您只需要輸入值的標准直方圖,並按頻率遞減的順序列出它們。

因此,讓我們正常插入到映射中(按值作為鍵),然后按頻率遞減的方式對對進行排序。

生活在Coliru

#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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM