更改输出以反映气泡排序时出现问题

Trouble with changing the output to reflect bubble sorting

本文关键字:排序 问题 气泡 输出      更新时间:2023-10-16

我有一个我一直在做的项目,当谈到气泡排序和其他一切如何发挥作用时,我感到非常迷茫。我必须使我的输出看起来像注释部分中的分数表。我已经完成了其中的一部分,但迷路了。当谈到气泡排序和空函数时,我有点迷茫。我知道 void 函数不应该返回任何东西,除非在 main 中调用,但我仍然对此感到困惑。这一点以及数组在事物中发挥作用的方式也是如此。谁能解释一下如何修复我的代码?现在我只是想让分数从大到低显示。所以我的气泡排序需要让它减少。

//******************************************************************************
// Programmer:
// Course:
// Program:
// Date:
// Description: This program generates a standings report for the Premier League as of Nov 2017.
//------------------------------------------------------------------------------
//
// Premier League, Nov 26 2017
// Club                      Pts   Wins  Draws
// -----------------------------------------
//  1 Manchester City     37    12   1
//  2 Manchester United   29    9    2
//  3 Chelsea             26    8    2
//  4 Arsenal             25    8    1
//  :
//  :
// 18 West Ham            10    2    4
// 19 Swansea              9    2    3
// 20 Crystal Palace       8    2    2
//
//******************************************************************************
#include <iostream>
#include <iomanip>    // for setw()
using namespace std;
// prototypes
void computePoints(int wins[], int draws[], int points[], int size);
// Receives the number of wins and draws for each club and computes their total points.
void sort(int points[], string club[], int wins[], int draws[], int size);
// Sorts the given arrays based on the points into decreasing order of points.
void swap(int & x, int & y);
// Swaps the integers x and y.
void swap(string & x, string & y);
// Swaps the strings x and y.
void printStandings(string club[], int wins[], int draws[], int points[], int size);
// Prints a table for the current standings.
//-------------------------------------------------------------------------------------------------------------------------------
int main()
{
const int SIZE = 20;    // number of clubs
string club[SIZE] = {"Arsenal", "Bournemouth", "Brighton", "Burnley", "Chelsea", "Crystal Palace", "Everton", "Huddersfield", "Leicester", "Liverpool", "Manchester City", "Manchester United", "Newcastle", "Southampton", "Stoke City", "Swansea City", "Tottenham", "Watford", "West Bromwich", "West Ham"};
int wins[SIZE] =  {8, 4, 4, 6, 8, 2, 3, 4, 3, 6, 12, 9, 4, 4, 3, 2, 7, 6, 2, 2};
int draws[SIZE] = {1, 2, 4, 4, 2, 2, 3, 3, 5, 5,  1, 2, 2, 4, 4, 3, 3, 3, 5, 4 };
int points[SIZE];
// compute the points for each club (function call)
computePoints(wins, draws, points, SIZE);
for (int i = 0; i < SIZE; i++)
{ 
cout << points[i] << endl;
}
cout << endl;  
// sort all club data into decreasing order of points (function call)
sort(points, club, wins, draws, SIZE);
for (int i = 0; i < SIZE; i++)
{ 
cout << points[i] << endl;
}
// print the standings (function call)
// printStandings(points, club, wins, draws, SIZE);
return 0;
}
//-------------------------------------------------------------------------------------------------------------------------------
// implementation
void computePoints(int wins[], int draws[], int points[], int SIZE)
{
for (int i = 0; i < SIZE; i++)
{
points[i] = (wins[i] * 3) + draws[i] * 1;
}

}
//-------------------------------------------------------------------------------------------------------------------------------
void sort(int points[], string club[], int wins[], int draws[], int SIZE)
{
// bubble sort algorithm (see lesson-24)
// Note: when swapping points[i] and points[i+1], the same elements in arrays club, wins, and
// draws must be swapped. 
int i, j;
for (i = 0; i < j; ++i)
{
for (j = 0; j < j-i-1; ++j)
{
// Comparing consecutive data and switching values if value at j > j+1.
if (points[j] > points[j+1])
{
points[j] = points[j]+points[j+1];
points[j+1] = points[j]-points[j + 1];
points[j] = points[j]-points[j + 1];
}
}
// Value at j-i-1 will be maximum of all the values below this index.
} 
for(int points = SIZE + 1; points > 0; points--);    
{ 
for(int i = 0;  i > points[i];  i++)  //make one pass & compare adjacent elements      
{ 
if(points[i] >= points[i+1])  //if adjacent pairs are out of order, swap them.   
swap(points[i], points[i+1]);      
}   
} 
}
//-------------------------------------------------------------------------------------------------------------------------------
void swap(int & x, int & y)
{
int temp = x; 
x = y;
y = temp;
}
//-------------------------------------------------------------------------------------------------------------------------------
void swap(string & x, string & y)
{
string temp = x;
x = y;
y = temp;
}
//-------------------------------------------------------------------------------------------------------------------------------
void printStandings(string club[], int wins[], int draws[], int points[], int SIZE)
{
// output the heading
cout << "Premier League, Nov 26 2017" << endl;
// output the corresponding elements of all four arrays

}
//-------------------------------------------------------------------------------------------------------------------------------

从你的sort开始

// Note: when swapping points[i] and points[i+1], the same elements in arrays club, wins, and
// draws must be swapped. 

在您的sort实现中没有提到clubwinsdraws

此外,第一个循环具有未定义的行为,第一次循环for (i = 0; i < j; ++i)j尚未初始化,因此编译器可以省略整个函数,或执行任何其他它想要的操作

如果你修复了这个问题,那么在内部的第一个循环中,j < j-i-1总是假的,因为i总是0或更高。

第二个循环不是有效的语法,因为您重复使用循环索引的名称points,然后像数组一样使用它。 您也可以在输入数组末尾之外的两个位置启动它,如果要在points[index]处访问元素,则需要从SIZE-1开始。

void sort(int points[], string club[], int wins[], int draws[], int SIZE)
{
// bubble sort algorithm (see lesson-24)
// Note: when swapping points[i] and points[i+1], the same elements in arrays club, wins, and
// draws must be swapped. 
for (int i = SIZE - 1; i; --i) // stops when i is 0, as only 0 converts to false
{
for (int j = 0; j < i; ++j) // loop from 0 to i
{
// do comparison and swap all the necessary values
}
}
}