#include <iostream>
#include <string>
using namespace std;
void bubbleSort(int data[], int n);
int main()
{
cout << "Enter ten unsorted integers..." << endl;
int a[10];
for (int i = 0; i < 10; ++ i)
{
cout << "[" << i << "] = ";
cin >> a[i];
}
cout << endl << "Unsorted List = ";
for (int i = 0; i < 10; ++i)
cout << a[i] << ", ";
cout << endl;
cout << "Sorting..." << endl;
cout << "Sorted List = ";
bubbleSort(a, 10);
}
void bubbleSort(int data[], int n)
{
int j = 0;
bool nextEnd = true;
while (nextEnd)
{
nextEnd = false;
++j;
for (int i = 0; i < n - j; ++i)
{
if (data[i] > data[i+1])
{
int temp = data[i];
data[i] = data[i+1];
data[i+1] = data[i];
nextEnd = true;
}
}
}
for (int i = 0; i < 10; ++i)
cout << data[i] << ", ";
}
该程序非常简单。将十个值输入到数组中。显示它们未排序。将它们发送到bubbleSort函数中,对它们进行排序,最后显示排序列表。我遇到的问题是我没有让输出恢复工作。我用最后一行代码进行了测试,但这不起作用。我也不认为我的排序搞砸了。如何正确显示此排序列表?