c++ - 排序函数没有正确返回数组?
问题描述
只是一个简单的家庭作业。该程序将获取两个列出的数组并使用选择排序和冒泡排序对它们进行排序。它将跟踪对每种排序技术进行的比较次数,然后在程序结束时显示这些值。该程序还应该显示两个排序后的数组,但是,在这种情况下,数组没有正确输出。我假设问题在于任何一个排序功能?
#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;
}
}
解决方案
您的代码没有显示数组,因为您没有在代码中实现任何内容来显示数组,
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),因此它们的行为是未定义的。现在要实际显示数组,您需要编写适当的代码来将数组显示为
void display(int *arr , int size)
{
for(unsigned i=0;i<size;++i)
cout<<*(arr+i)<<ends;
cout<<endl;
}
推荐阅读
- excel - 将列表框的内容导出为pdf
- javascript - 组件根目录上的 Vue 2 事件监听器
- c++ - 在 C++ 中的类赋值运算符和向量中默认初始化的问题
- python - 如何处理丑陋的代码格式化程序输出?(黑色的)
- python - 如何为子图提供两行多列
- sql-server - T-SQL 2012 charindex比较几个字符串不匹配
- jmeter - 如何使用 Swagger 代码生成生成 jmeter 客户端?
- python - 切片多索引的两个不同级别时出错
- javascript - 如何读取发送到我的特快路线的数据?
- php - 如何修复 PHP 要求结果而不是 mysqli_fetch_assoc 的布尔值?