只需一个简单的作业。该程序将采用两个列出的数组,并使用选择排序和气泡排序对它们进行排序。它将跟踪每种排序技术进行的比较次数,然后在程序结尾显示这些值。该程序还应显示两个排序后的数组,但是,在这种情况下,数组输出不正确。我假设问题出在哪一个排序功能上?
#include <iostream>
#include <iomanip>
using namespace std;
const int SIZE = 20;
void bubbleSort(int[], int&);
void selectionSort(int[], int&);
void displayResults(int[], int[], int, int);
int main()
{
int list1[SIZE] = { 6, 9, 56, 78, 45, 64, 80, 4, 67, 89, 38, 61, 40,
71, 54, 92, 19, 69, 30, 99 };
int list2[SIZE] = { 6, 9, 56, 78, 45, 64, 80, 4, 67, 89, 38, 61, 40,
71, 54, 92, 19, 69, 30, 99 };
int exchange1 = 0;
int exchange2 = 0;
bubbleSort(list1, exchange1);
selectionSort(list2, exchange2);
displayResults(list1, list2, exchange1, exchange2);
}
void displayResults(int array1[], int array2[], int exchange1, int
exchange2)
{
cout << endl;
cout << "Number of exchanges made by each sort algorithm:\n";
cout << "************************************************" << endl;
cout << "List 1: " << array1[SIZE] << endl;
cout << "List 2: " << array2[SIZE] << endl;
cout << "************************************************" << endl;
cout << setw(42) << left << "Bubble sort:" << right << exchange1 << endl;
cout << setw(42) << left << "Selection sort:" << right << exchange2
<< endl;
cout << "************************************************" << endl;
cout << endl;
}
void bubbleSort(int array[], int& exchange)
{
int temp;
bool swap;
do {
swap = false;
for (int count = 0; count < (SIZE - 1); count++) {
if (array[count] > array[count + 1]) {
temp = array[count];
array[count] = array[count + 1];
array[count + 1] = temp;
swap = true;
exchange++;
}
}
} while (swap);
}
void selectionSort(int array[], int& exchange2)
{
int startScan = 0;
int minIndex;
int minValue;
for (int startScan = 0; startScan < (SIZE - 1); startScan++) {
minIndex = startScan;
minValue = array[startScan];
for (int i = startScan + 1; i < SIZE; i++) {
if (array[i] < minValue) {
minValue = array[i];
minIndex = i;
exchange2++;
}
}
array[minIndex] = array[startScan];
array[startScan] = minValue;
}
}
答案 0 :(得分:0)
您的代码未显示数组,因为您尚未在代码中实现任何内容来显示数组,
void displayResults(int array1[], int array2[], int exchange1, int exchange2)
{
cout << endl;
cout << "Number of exchanges made by each sort algorithm:\n";
cout << "************************************************" << endl;
cout << "List 1: " << array1[SIZE] << endl;
cout << "List 2: " << array2[SIZE] << endl;
cout << "************************************************" << endl;
cout << setw(42) << left << "Bubble sort:" << right << exchange1 << endl;
cout << setw(42) << left << "Selection sort:" << right << exchange2
<< endl;
cout << "************************************************" << endl;
cout << endl;
}
这里array1[SIZE]
和array2[SIZE]
不显示数组,它们访问数组的超出范围的值(数组的最大索引为19),因此它们的行为是undefined。
现在要实际显示数组,您将需要编写适当的代码以将数组显示为
void display(int *arr , int size)
{
for(unsigned i=0;i<size;++i)
cout<<*(arr+i)<<ends;
cout<<endl;
}
答案 1 :(得分:0)
您在排序函数中多次正确使用了数组。 array[count] = array[count + 1];
例如将一个数组元素分配给另一个。
然后突然在显示函数中,您似乎认为array1[SIZE]
将显示整个数组。
如果要显示数组,则应使用循环和索引来完成它,就像在排序函数中一样
for (int i = 0; i < SIZE; ++i)
cout << array1[i] << ' ';
cout << endl;