双重选择对数组进行排序 - 老实说难倒了
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);
}
我需要使用选择排序或冒泡排序算法。
我有 银行帐号 必须相应地按升序排序账户余额,所有数据都是从一个文件。
整理数据后,我必须将其重写到另一个文件中,这是我最不担心的事情。
所以问题是我如何按升序对我的 accountNumbers
以及他们后面的 accountBalance
进行排序。
要执行冒泡排序算法,您必须执行 2 个 for
循环和一个临时变量
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;
}
只需将其实现到执行冒泡排序的函数中
您可以声明一个结构:
struct Account{
int accountNum;
int accountBalance;
bool operator<(const Account& a);
};
然后重载比较运算符:
bool Account::operator<(const Account& a);
{
return (accountNum < a.accountNum);
}
然后使用 for 循环将所有数据放入结构向量中:
std::vector<Account> accVec;
最后使用 std::sort()
对向量进行排序
std::sort(accVec.begin(), accVec.end());
现在您的数据已按帐号升序整齐地存储在向量中。
或者,您可以应用常规的 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]);
您需要根据 accountNumbers
排序,但将每个交换操作应用于两个数组。
下面是使用选择排序的代码:
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]);
}
}
通过使用一些更现代的 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);
}
会鼓励您学习 类,甚至只是开始学习结构,并熟悉 std::vector
,因为您应该经常使用它。
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);
}
我需要使用选择排序或冒泡排序算法。
我有 银行帐号 必须相应地按升序排序账户余额,所有数据都是从一个文件。
整理数据后,我必须将其重写到另一个文件中,这是我最不担心的事情。
所以问题是我如何按升序对我的 accountNumbers
以及他们后面的 accountBalance
进行排序。
要执行冒泡排序算法,您必须执行 2 个 for
循环和一个临时变量
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;
}
只需将其实现到执行冒泡排序的函数中
您可以声明一个结构:
struct Account{
int accountNum;
int accountBalance;
bool operator<(const Account& a);
};
然后重载比较运算符:
bool Account::operator<(const Account& a);
{
return (accountNum < a.accountNum);
}
然后使用 for 循环将所有数据放入结构向量中:
std::vector<Account> accVec;
最后使用 std::sort()
对向量进行排序std::sort(accVec.begin(), accVec.end());
现在您的数据已按帐号升序整齐地存储在向量中。
或者,您可以应用常规的 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]);
您需要根据 accountNumbers
排序,但将每个交换操作应用于两个数组。
下面是使用选择排序的代码:
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]);
}
}
通过使用一些更现代的 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);
}
会鼓励您学习 类,甚至只是开始学习结构,并熟悉 std::vector
,因为您应该经常使用它。