排序函数未正确返回数组

Sorting functions not returning array correctly?

本文关键字:返回 数组 函数 排序      更新时间:2023-10-16

只是一个简单的家庭作业。该程序将获取列出的两个数组,并使用选择排序和气泡排序对它们进行排序。它将跟踪每种排序技术进行的比较次数,然后在程序结束时显示这些值。程序还应显示两个排序的数组,但是,在这种情况下,数组未正确输出。我假设问题出在任何一个排序功能上?

#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;