c++ - 冒泡排序问题 C++

标签 c++ bubble-sort

使用 C++,我使用冒泡排序(升序)进行排序,程序似乎可以正常工作,但我得到的最终传递是重复值。我是编程新手,一直无法弄清楚如何纠正这个问题。有什么建议么?

我的代码是:

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

抱歉,我知道这不是一个性感的问题,我只是希望能找到一些正确方向的指示。

最佳答案

您的错误在 do{ ... } while(swapTookPlace) 逻辑中。

在您的第四遍中,swapTookPlacetrue(因为确实发生了交换),因此您再次重复 do while 循环.

在这第五次迭代中没有发生交换(swapTookPlace == false)但您仍然打印出传递/数组。最简单的解决方法是将循环调整为:

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

输出:

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

关于c++ - 冒泡排序问题 C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42824149/

相关文章:

c++ - operator= on pointers 可能导致 mac 上的段错误

c++ - 如何以相反的顺序读取 C++ 程序的输入?

c++ - 删除字符串中分隔符之间的字符

c++ - 不同的冒泡排序算法代码——有什么区别? C++

c++ - 头文件应该插入两次吗?

c++ - 为什么这不被视为默认构造函数?

c - 我对 c 中的列表进行了冒泡排序,我想知道我是否犯了任何特定错误

javascript - Javascript 中的冒泡排序算法

java - 我该如何冒泡排序?

c - C语言冒泡排序中额外零的问题