我想在一个函数,而不是在线使用冒泡排序,而我似乎有它返回结果的问题。就像目前一样,它根本没有给我任何结果。这是我迄今编写的代码...C++冒泡排序(升序)函数没有返回结果
基本上,用户告诉程序他们要输入多少个数字(最多允许输入20个),然后按输入的顺序将它们输入到数组中。然后打印输入的值,排序并打印排序后的值。或者至少这是应该发生的事情。
感谢您的帮助!
#include <iostream>
using namespace std;
int vault[20] = {}; // array to store the 20 values
int val = 0; // variable to pass values to the array
int amt = 0; // variable to get the amount of numbers the user is going to enter
int i = 0; // loop counter
// Bubble Sort Function Prototype
void bubble (int (&vault)[20], int val);
// Bubble Sort Function
void bubble (int (&vault)[20], int val)
{
int swap = 1; // flag used to indicate swaps occuring
int temp = 0; // holder variable
int x = 0; // loop counter
int y = 0; // second loop counter
for (x = 0; (x < val) && (swap = 1); x++)
{
swap = 0;
for (y = x+1; y < val; y++)
{
if (vault[x] > vault[y])
{
temp = vault[x];
vault[x] = vault[y];
vault[y] = temp;
swap = 1;
}
}
}
return;
}
int main()
{
cout << "Welcome to the Bubble Sort exe\n\n" << endl;
cout << "Please enter in the amount of numbers you would like to enter: ";
cin >> amt;
cout << "Please enter the values you wish to enter: ";
for(i = 0; i < amt; i++)
{
cin >> vault[i];
}
cout << "The values you entered in order are: ";
for (i = 0; i < amt; i++)
{
cout << vault[i] << ' ';
}
cout << "\n\nLet me just sort that for you!" << endl;
bubble(vault, val);
cout << "\n\nHere are the values in ascending order:\n" << endl;
for (i = 0; i < val; i++)
{
cout << vault[i] << ' ';
}
system("pause");
return 0;
}
Altough它不是一个真正的泡沫排序算法的问题是,你存储的大小所述阵列的在AMT进行排序和你VAL传递给排序函数,它以0初始化此外你检查是否I> amt; ... –
这样做......当然,它的东西很小,让我在这上面绊倒。感谢您的帮助!此外,大家都口口声声说这是不是一个真正的冒泡排序,但我看到这个确切的形式遍布论坛,并从文件直接从我的教授,我想我失去了一些东西... – Dpry12