GothLoli
GothLoli

Reputation: 1

Bubble sort, Selection sort, and insertion sort

https://i.sstatic.net/QvWSG.jpg

This is the link that shows the result.

And this result is how my result of C++ must be like

As this result shows, there are Bubble sort, Selection Sort, and Insertion Sort,

and every process of each sort is shown to the black screen.

For example, (Bubble Sort)

20 10 40 30

20 10 30 40

10 20 30 40.

I have to use void displayPtrArray to show like that.

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

I use the function for every sort, but if that is wrong please tell me.

And teach me how to show the process of each sort with

void displayPtrArray

I really don't know..... T_T...........

PLEASE HELP !!

Upvotes: 0

Views: 2276

Answers (2)

Arun A S
Arun A S

Reputation: 7016

Actually, viewing the process of the sorting is rather simple. You just have to output during the sorting process. You don't need to call another function for that. Try something like this

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);
}

Upvotes: 1

rswords
rswords

Reputation: 31

You already have the exact code you need for displayPtrArray! The for loop defined in reset should do it.

Looking at the expected output, the array gets displayed every time there's a change to it. For bubble sort, the array changes when you do your swap (at the end of the if statement), so you want to add your call to displayPtrArray in the line after swap = true.

For selection sort, the array changes at the end of your outer for loop, so you should add the call to displayPtrArray in the line after array[startScan] = minValue;.

For insertion sort, the array changes at every iteration of the inner for loop and also in the last line of the outer for loop, so you'll probably have to call displayPtrArray in both of those places.

Note: just in case you aren't familiar with the syntax, you call the function like this: displayPtrArray(array, size); where array is the name of your array variable (the name is array in bubble and selection sorts; it's a in insertion sort) and size is the size of the array (named consistently in all three of your sorting functions).

Upvotes: 1

Related Questions