冒泡排序、选择排序和插入排序

Bubble sort, Selection sort, and insertion sort

本文关键字:插入排序 排序 选择 冒泡排序      更新时间:2023-10-16

https://i.stack.imgur.com/QvWSG.jpg

这是显示结果的链接。

这个结果就是我的C++结果必须像一样

如该结果所示,有气泡排序、选择排序和插入排序、

并且每种处理都显示在黑屏上。

例如,(Bubble Sort)

20 10 40 30

20 10 30 40

10 20 30 40。

我必须使用void displayPtrArray来显示这样的内容。

#include <iostream>
#include <string>
#include <array>
#include <iomanip>
using namespace std;
void reset(int array[], const int size);
void displayIntArray();
void displayPtrArray(const int array[], int size);
void BubbleSort(int array[], int size);
void SelectionSort(int array[], int size);
void InsertionSort(int a[], int size);
const int RR = 4;
int main()
{
    int arr[RR];
    reset(arr, RR);
}
void reset(int array[], const int size)
{
    cout << "The originial array has been reset to:" << endl;
    int array[RR] = { 20, 40, 10, 30 };
    for (int n = 0; n < size; n++)
    {
        cout << setw(5) << array[n];
    }
    cout << endl << endl;
}
void displayPtrArray(const int array[], int size)
{
}
void displayIntArray()
{
}
void BubbleSort(int array[], int size)
{
    bool swap;
    int temp;
    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;
            }
        }
    } while (swap);
}
void selectionSort(int array[], int size)
{
    int startScan, minIndex, minValue;
    for (startScan = 0; startScan < (size - 1); startScan++)
    {
        minIndex = startScan;
        minValue = array[startScan];
        for (int index = startScan + 1; index < size; index++)
        {
            if (array[index] < minValue)
            {
                minValue = array[index];
                minIndex = index;
            }
        }
        array[minIndex] = array[startScan];
        array[startScan] = minValue;
    }
}
void InsertionSort(int a[], int size)
{
    for (int i = 1; i<size; i++)
    {
        int j;
        int current = a[i];
        for (j = i - 1; j >= 0 && a[j]> current; j--)
        {
            a[j + 1] = a[j];
        }
        a[j + 1] = current;
    }
}

我对每种分拣都使用该功能,但如果这是错误的,请告诉我。

教我如何用显示每种类型的过程

void displayPtrArray

我真的不知道。。。。。T_T。。。。。。。。。。。

请帮忙!!

您已经拥有了displayPtrArray所需的确切代码!重置中定义的for循环应该执行此操作。

从预期的输出来看,每次数组发生变化时都会显示它。对于冒泡排序,数组在进行交换时(在if语句的末尾)会发生变化,因此您需要在swap = true之后的行中添加对displayPtrArray的调用。

对于选择排序,数组在外部For循环的末尾发生变化,因此应该在array[startScan] = minValue;之后的行中添加对displayPtrArray的调用。

对于插入排序,数组在内部For循环的每次迭代中都会发生变化,在外部For循环的最后一行也会发生变化。因此,您可能必须在这两个位置调用displayPtrArray。

注意:如果您不熟悉语法,可以这样调用函数:displayPtrArray(array, size);,其中array是数组变量的名称(在冒泡排序和选择排序中,名称为array;在插入排序中,为a),size是数组的大小(在所有三个排序函数中都一致命名)。

实际上,查看排序过程相当简单。您只需要在排序过程中输出即可。您不需要为此调用另一个函数。试试这个

void BubbleSort(int array[], int size)
{
    bool swap;
    int temp;
    do
    {
        swap = false;
        for (int count = 0; count < (size); count++)
        {
            if (array[count] > array[count + 1])
            {
                temp = array[count];
                array[count] = array[count + 1];
                array[count + 1] = temp;
                swap = true;
            }
           cout << "nThe array is ";
           for ( int i = 0; i < ( size - 1 ); i++ )
           cout << array[i] << "t";                            // Look here
           cout << "nThe value of temp is " << temp << endl;   // and here
        }
    } while (swap);
}