简体   繁体   中英

Bubble Sort Issues C++

Using C++, I am sorting with a bubble sort (ascending order) and the program appears to be working, however I get the final pass as a duplicate value. I'm new to programming and have not been able to figure out how to rectify this. Any suggestions?

My code is:

#include <iostream>
#include <Windows.h>
#include <iomanip>
#include <string>

using namespace std;

int main()
{
system("color 02");

HANDLE screen = GetStdHandle(STD_OUTPUT_HANDLE);

string hyphen;
const string progTitle = "Array Sorting Program";
const int numHyphens = 70;

hyphen.assign(numHyphens, '-');

const int size = 8;

int values[size] = { 21, 16, 23, 18, 17, 22, 20, 19 };

cout << hyphen << endl;
cout << "                        " << progTitle << endl;
cout << hyphen << endl;

cout << "\n Array 1 before sorting:   \n" << endl;

printArray(values, size);

cin.ignore(cin.rdbuf()->in_avail());
cout << "\n Press 'Enter' to proceed to sorting Array 1\n";
cin.get();

cout << "\n Sorted ascending:   \n" << endl;
sortArrayAscending(values, size);

cin.ignore(cin.rdbuf()->in_avail());
cout << "\n\n\n\nPress only the 'Enter' key to exit program: ";
cin.get();
}

void sortArrayAscending(int *array, int size)
{
const int regTextColor = 2;
const int swapTextColorChange = 4;

HANDLE screen = GetStdHandle(STD_OUTPUT_HANDLE);

int temp;
bool swapTookPlace;
int pass = 0;

do
{
    swapTookPlace = false;
    for (int count = 0; count < (size - 1); count++)
    {

        if (array[count] > array[count + 1])
        {
            swapTookPlace = true;
            temp = array[count];
            array[count] = array[count + 1];
            array[count + 1] = temp;
        }
    }
    cout << " Pass #" << (pass + 1) << ": ";
    pass += 1;
    printArray(&array[0], size);
 } while (swapTookPlace);
}

void printArray(int *array, int size)
{
for (int count = 0; count < size; ++count)
    cout << " " << array[count] << "   ";
cout << endl;
}

Sorry, I know this isn't a sexy problem, I am just hoping to find some pointers in the right direction.

Your error is in the do{ ... } while(swapTookPlace) logic.

On your fourth pass, swapTookPlace is true (because a swap did take place), and thus you repeat the do while loop again.

On this fifth iteration no swap is taking place ( swapTookPlace == false ) but you still print out the pass/array. The simplest fix would be to adjust your loop to be:

do
{
    swapTookPlace = false;

    for (int count = 0; count < (size - 1); count++)
    {
        if(array[count] > array[count + 1])
        {
            swapTookPlace = true;
            temp = array[count];
            array[count] = array[count + 1];
            array[count + 1] = temp;
        }
    }

    if(swapTookPlace)
    {
        cout << " Pass #" << (pass + 1) << ": ";
        pass += 1;
        printArray(&array[0], size);
    }

 } while(swapTookPlace);

Output:

Pass #1:  16    21    18    17    22    20    19    23
Pass #2:  16    18    17    21    20    19    22    23
Pass #3:  16    17    18    20    19    21    22    23
Pass #4:  16    17    18    19    20    21    22    23

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM