简体   繁体   English

双重选择对数组进行排序-老实说过

[英]Dual selection sorting an array - honestly stumped

void dualSort(int [], double [], int);
int main()
{
    const int ARRAY_SIZE = 1000;            // Array size
    double accountNumbers[ARRAY_SIZE];      // Array with 1000 elements
    double accountBalances[ARRAY_SIZE];     // Loop counter variable
    int count = 0;                              // Input file stream object
    ifstream inputFile;

    // Open the file.
    inputFile.open("FinalNumbers.txt");

    // Read the numbers from the file into the array
    while (count < ARRAY_SIZE && inputFile >> accountNumbers[count] >> accountBalances[count] ) {
        count++;
    }

    inputFile.close();

    // Display the read data
    cout << "The bank account numbers are: " << endl;
    for (int count = 0; count < ARRAY_SIZE; count++) {
        cout << accountNumbers[count] << "\n" << accountBalances[count] << " " << endl;
    }

    void dualSort(int accountNumbers[], double accountBalances, int ARRAY_SIZE);



}

I am required to use the selection sort or bubble sort algorithm. 我需要使用选择排序或气泡排序算法。

I have Bank Account Numbers that have to be correspondingly and ascending sorted with there Account Balance , all the data is read from a file. 我有必须对应的银行帐号 ,并使用Account Balance升序排序,所有数据都是从文件中读取的。

After sorting the data I have to rewrite it into another file, which is the least of my worries. 对数据进行排序后,我必须将其重写到另一个文件中,这是我最少的担心。

So the question is how do I go about sorting my accountNumbers in ascending order along with their accountBalance following them. 所以问题是我该如何对我的accountNumbers及其accountBalance进行升序排序。

to do bubble sort algorithm you must do 2 for loops and a temporary variable 要执行冒泡排序算法,您必须for循环执行2个操作并添加一个临时变量

int tempAccNumber=0;
int tempAccBalance=0;
for(int j=0;j<ARRAY_SIZE-1;++j)
    for(int i=0;i<ARRAY_SIZE-1;++i)
          if(accountNumbers[i]>accountNumbers[i+1])
          {             
              tempAccNumber=accountNumbers[i];
              accountNumbers[i]=accountNumbers[i+1];
              accountNumbers[i+1]=tempAccNumber;
              tempAccBalance=accountBalances[i];
              accountBalances[i]=accountBalances[i+1];
              accountBalances[i+1]=tempAccBalance;
          }

just implement this to your function that do the bubble sort 只需将其实现到您的执行气泡排序的功能

You can declare a struct: 您可以声明一个结构:

struct Account{
    int accountNum;
    int accountBalance;
    bool operator<(const Account& a);
};

Then overload the comparison operator: 然后重载比较运算符:

bool Account::operator<(const Account& a);
{
    return (accountNum < a.accountNum);
}

Then put all your data in a struct vector using for loops: 然后使用for循环将所有数据放入结构向量中:

std::vector<Account> accVec;

Finally sort vector using std::sort() 最后使用std :: sort()对向量进行排序

std::sort(accVec.begin(), accVec.end());

Now you have your data neatly stored in a vector in ascending order of account number. 现在,您已经按照帐号的升序将数据整齐地存储在矢量中。

Alternatively you can apply regular bubbleSort to sort the elements, as shown by "abcOfJavaAndCPP" 或者,您可以应用常规的bubbleSort对元素进行排序,如“ abcOfJavaAndCPP”所示

for(int j = 1; j < accVec.size(); ++j)
    for(int i = 1; i < accVec.size() ; ++i)
        if(accVec[i-1] < accVec[i])
            std::swap(accVec[i], accVec[i+1]);

You need to sort according to accountNumbers but apply every swap operation to both arrays. 您需要根据accountNumbers进行排序,但是将每个交换操作都应用于两个数组。

Here is the code using selection sort: 这是使用选择排序的代码:

void dualSort(int accountNumbers[], double accountBalances[], int ARRAY_SIZE)
{
 int minIndex;

 for(int i = 0; i < ARRAY_SIZE - 1; i++)
 {
  minIndex = i;

  for(int j = i + 1; j < ARRAY_SIZE; j++)
  {
   if(accountNumbers[j] < accountNumbers[minIndex])
   {
    minIndex = j;
   }
  }

  swap(accountNumbers[i], accountNumbers[minIndex]);
  swap(accountBalances[i], accountBalances[minIndex]);
 }
}

Code can be simplified quite a bit by using some more modern C++ techniques: 使用一些更现代的C ++技术可以大大简化代码:

#include <vector>

struct Account {
    double balance;
    int number;
};

bool operator<(const Account& lhs, const Account& rhs) {
    return lhs.number < rhs.number;
}

void dualSort(vector<Account>& v) {
    for (std::size_t i = 0; i != v.size() - 1; ++i) {
        for (std::size_t j = 0; j != v.size() - 1; ++j) {
            if (v[j] < v[j+1]) std::swap(v[j], v[j+1]);
        }
    }
}

int main()
{
    const int ARRAY_SIZE = 1000;            // Array size
    std::vector<Account> v(ARRAY_SIZE);
    std::ifstream inputFile;

    // Open the file.
    inputFile.open("FinalNumbers.txt");

    for (std::size_t i = 0; i != ARRAY_SIZE; ++i) {
        inputFile >> v[i].number >> v[i].balance;
    }

    inputFile.close();

    // Display the read data
    cout << "The bank account numbers are: " << endl;
    for (int count = 0; count < ARRAY_SIZE; count++) {
        cout << v[count].number << "\n" << v[count].balance << " " << endl;
    }

    void dualSort(v);
}

Would encourage you to learn about classes, or even just structs to start, and also get familiar with std::vector as you should be using it a lot. 鼓励您学习类,甚至只是开始学习的结构,还应该熟悉std::vector因为您应该经常使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM