在c++中按降序插入排序
Insertion sort in descending order in C++
我正面临插入排序函数的问题。我正试着把选票和名字按降序打印出来。投票似乎起作用了,但名字是按升序而不是降序打印的。在我插入a b c d e f作为名字和5 4 3 21 1作为投票之后,它看起来像这样:
Please input the canditate 1 name:
a
Please input the canditate 1 votes:
1
Please input the canditate 2 name:
b
Please input the canditate 2 votes:
2
Please input the canditate 3 name:
c
Please input the canditate 3 votes:
3
Please input the canditate 4 name:
d
Please input the canditate 4 votes:
4
Please input the canditate 5 name:
e
Please input the canditate 5 votes:
5
Candidate Votes Received % of Total Votes
a 5 33.33
b 4 26.67
c 3 20.00
d 2 13.33
e 1 6.67
Total 15
The winner of the elections is a.
Program ended with exit code: 0
Press any key to continue . . .
应试者应该是c c b a,而不是a b c d e。
下面是我的代码。任何帮助都将不胜感激。
#include <iostream>
#include <string>
#include<iomanip>
using namespace std;
void insertionSort(double votes[], string name[], double len)
{
for (int i = 0; i < len - 1; i++)
{
int j = i + 1;
double temp = votes[j];
while (j > 0 && temp > votes[j - 1])
{
votes[j] = votes[j - 1];
j--;
votes[j] = temp;
}
string temp2 = name[j];
while (j > 0 && temp2 > name[j - 1])
{
name[j] = name[j - 1];
j--;
name[j] = temp2;
}
}
}
int main()
{
//Declaring variables
string *name;
double *votes;
double *percentage;
double total = 0;
int max = 0;
name = new string[5];
votes = new double[5];
percentage = new double[5];
//for condition for user to input Canditate names and the votes received
for (int i = 0; i < 5; i++)
{
cout << "Please input the canditate " << i + 1 << " " << "name: " << endl;
cin >> name[i];
cout << "Please input the canditate " << i + 1 << " " << "votes: " << endl;
cin >> votes[i];
total = total + votes[i];
}
//Q1 or Q2
//selectionSort(votes, name, 5);
insertionSort(votes, name, 5);
//printing out the Canditate, voters received, and % of total votes
cout << "Candidate" << "tt" << "Votes Received" << "tt" << "% of Total Votes" << endl;
//for loop in order to find % of total votes, winner of election
for (int i = 0; i < 5; i++)
{
if (votes[i]>votes[max])
max = i;
cout << name[i] << "ttt" << fixed << setprecision(0) << votes[i] << "ttt" << fixed << setprecision(2) << (votes[i] * 100 / total) << endl;
}
//printing out the total and winner of the election
cout << "Total" << "ttt" << fixed << setprecision(0) << total << endl;
cout << "The winner of the elections is " << name[max] << "." << endl;
delete[]name;
delete[]votes;
cout << "Program ended with exit code: 0" << endl;
return 0;
}
你需要使数据一致,即保持它在结构中,尝试:
#include <iostream>
#include <string>
#include<iomanip>
using namespace std;
/*void selectionSort(double votes[], string name[], double len)
{
for (int i = 0; i < len-1; i++)
{
double max = (int)i;
for (int j = i+1; j < len; j++)
{
if (votes[j]>votes[(int)max])
{
double temp = votes[i];
votes[i] = votes[j];
votes[j] = temp;
string temp2 = name[i];
name[i] = name[j];
name[j] = temp2;
}
}
}
}*/
struct candidate{
string *name;
double votes;
double percentage;
};
void insertionSort( candidate candidates[], double len)
{
for (int i = 0; i < len - 1; i++)
{
int j = i + 1;
candidate tmp = candidates[i];
while (j > 0 && tmp.votes > candidates[j-1].votes)
{
candidates[j] = candidates[j - 1];
j--;
candidates[j] = tmp;
}
}
}
int main()
{
//Declaring variables
candidate candidates[5];
double total = 0;
int max = 0;
//for condition for user to input Canditate names and the votes received
for (int i = 0; i < 5; i++)
{
candidates[i].name = new string;
cout << "Please input the canditate " << i + 1 << " " << "name: " << endl;
cin >> *(candidates[i].name);
cout << "Please input the canditate " << i + 1 << " " << "votes: " << endl;
cin >> candidates[i].votes;
total = total + candidates[i].votes;
}
//Q1 or Q2
//selectionSort(candidates, 5);
insertionSort(candidates, 5);
//printing out the Canditate, voters received, and % of total votes
cout << "Candidate" << "tt" << "Votes Received" << "tt" << "% of Total Votes" << endl;
//for loop in order to find % of total votes, winner of election
for (int i = 0; i < 5; i++)
{
cout << *(candidates[i].name) << "ttt" << fixed << setprecision(0) << candidates[i].votes << "ttt" << fixed << setprecision(2) << (candidates[i].votes * 100 / total) << endl;
}
//printing out the total and winner of the election
cout << "Total" << "ttt" << fixed << setprecision(0) << total << endl;
cout << "The winner of the elections is " << *(candidates[0].name) << "." << endl;
for(int i=0; i<5; ++i)
delete candidates[i].name;
cout << "Program ended with exit code: 0" << endl;
return 0;
}
相关文章:
- 我正在尝试按降序对地图进行排序,但没有得到预期的结果?
- 如何在 c++ 中根据第二个元素按降序对列表进行排序
- 如何按矩阵行的总和降序对矩阵进行排序 C++.
- 插入按降序排序
- 如何使用cpp编写选择排序算法以降序对元素列表进行排序?
- 我怎么不能按课程代码的降序对数字进行排序
- 如何使用布尔函数按降序对数组进行排序?
- 如何按降序排序?
- 数组将排序排序为降序而不是升序
- 如何按降序对标准数组进行排序 - C++ 11.
- 按降序C 排序矢量
- 使用QuickSort算法以降序排序向量
- 偶数升序排序,奇数降序排序
- 按类向量的降序排序
- 按降序排序的最佳方式
- 快速排序在对降序-升序数据进行排序时的奇怪行为
- C++使用单独的函数按降序对数组进行排序
- 按降序对双链接列表进行排序
- 对结构、降序或升序的向量进行排序:C++
- 在c++中按降序插入排序