排序函数未正确返回数组
Sorting functions not returning array correctly?
只是一个简单的家庭作业。该程序将获取列出的两个数组,并使用选择排序和气泡排序对它们进行排序。它将跟踪每种排序技术进行的比较次数,然后在程序结束时显示这些值。程序还应显示两个排序的数组,但是,在这种情况下,数组未正确输出。我假设问题出在任何一个排序功能上?
#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;
}
您在排序函数中多次正确使用数组。 例如,array[count] = array[count + 1];
将一个数组元素分配给另一个数组元素。
然后突然在你的显示函数中,你似乎认为array1[SIZE]
要显示整个数组。
如果你想显示一个数组,你应该使用循环和索引来完成,就像你在排序函数中所做的那样
for (int i = 0; i < SIZE; ++i)
cout << array1[i] << ' ';
cout << endl;
相关文章:
- 从 C++ 中的函数返回数组地址问题
- 如何在 c++ 函数中返回数组
- 如何从 c++ 中的函数返回数组
- 为什么 c++ 函数可以正确返回数组的大小?
- 解释通过从函数引用返回数组的语法
- (C++)我的函数不返回数组
- 如何按 C++ 中的值从函数返回数组
- 返回数组中值的指针地址
- 创建一个函数来转换数组元素的类型并返回数组的地址
- 使用数组类返回数组c++
- 如何使函数返回数组?用于制作在VB.NET中使用的DLL
- 从函数 BY VALUE 返回数组,返回结构时会发生什么?
- 调用和打印函数,在 C++ 中返回数组
- 如何使用递归函数返回数组中整数的索引?
- 从函数返回数组时是否需要删除
- 为什么从函数返回数组时需要将数组声明为静态数组.(C++)
- C++返回数组而不进行动态内存分配?
- 在函数 c++ 中返回数组时出现问题
- 如何从C 中的函数返回数组
- 如何使类函数返回数组,然后将其调用并存储在 main 中