C++向量冒泡排序
c++ vector bubble sort
我使用 g++ -std=c++11 Sort.cpp 来编译我的文件。
我的问题是冒泡排序不排序。
也许我正在按值传递矢量,但我不知道这是不是我第一次尝试使用 C++,所以我选择了使用矢量库。
我的代码是:
#include <iostream>
#include <vector>
using namespace std;
void bubbleSort(vector<int> a);
void printVector(vector<int> a);
int main(int argc, char const *argv[])
{
vector<int> a{3,2,6,1};
printVector(a);
bubbleSort(a);
printVector(a);
}
void bubbleSort(vector<int> a)
{
bool swapp = true;
while(swapp)
{
swapp = false;
for (int i = 0; i < a.size()-1; i++)
{
if (a[i]>a[i+1] )
{
a[i] += a[i+1];
a[i+1] = a[i] - a[i+1];
a[i] -=a[i+1];
swapp = true;
}
}
}
}
void printVector(vector<int> a)
{
for (int i=0; i <a.size(); i++)
{
cout<<a[i]<<" ";
}
cout<<endl;
}
主要是我声明了一个 int 向量类型,并制作了列表 {3,2,6,1}
之后 e 调用函数 printVector
假装在控制台上打印所有矢量数字并调用 bubbleSort
函数,最后再次打印。
您正在将向量作为值传递给您的函数,这意味着您正在对副本而不是原始向量进行排序,然后打印原始向量的副本。
在bubbleSort
函数中将参数更改为vector<int> &a
,在printVector
函数中将参数更改为vector<int> const &a
(因为不需要更改矢量内容从这里)。
顺便说一下,您的代码可能会受到签名者整数溢出导致的未定义行为的影响。
您应该使用另一种方法来交换元素,例如 std::swap
:
std::swap(a[i], a[i + 1]);
你需要传参;通过制作副本,您可以对临时副本进行排序,然后就是这样;它消失了,原始文件仍未排序,因为,再一次,你只排序了整个向量的临时副本。
因此将 vector<int> a
更改为 vector<int>& a
。
这是固定的代码:
http://coliru.stacked-crooked.com/a/2f118555f585ccd5
#include <iostream>
#include <vector>
using namespace std;
void bubbleSort(vector<int>& a);
void printVector(vector<int> a);
int main(int argc, char const *argv[])
{
vector<int> a {3,2,6,1};
printVector(a);
bubbleSort(a);
printVector(a);
}
void bubbleSort(vector<int>& a)
{
bool swapp = true;
while(swapp){
swapp = false;
for (size_t i = 0; i < a.size()-1; i++) {
if (a[i]>a[i+1] ){
a[i] += a[i+1];
a[i+1] = a[i] - a[i+1];
a[i] -=a[i+1];
swapp = true;
}
}
}
}
void printVector(vector<int> a){
for (size_t i=0; i <a.size(); i++) {
cout<<a[i]<<" ";
}
cout<<endl;
}
我也坚持了一段时间。 VermillionAzure 已经很好地回答了这个问题,但仍然粘贴我的解决方案。我没有使用 std::swap 只是因为我喜欢用手做更多事情。
#include <iostream>
#include <vector>
//function to swap values
//need to pass by reference to sort the original values and not just these copies
void Swap (int *a, int *b)
{
int temp = *a;
*a = *b;
*b = temp;
}
void BubbleSort (std::vector<int> &array)
{
std::cout<<"Elements in the array: "<<array.size()<<std::endl;
//comparisons will be done n times
for (int i = 0; i < array.size(); i++)
{
//compare elemet to the next element, and swap if condition is true
for(int j = 0; j < array.size() - 1; j++)
{
if (array[j] > array[j+1])
Swap(&array[j], &array[j+1]);
}
}
}
//function to print the array
void PrintArray (std::vector<int> array)
{
for (int i = 0; i < array.size(); i++)
std::cout<<array[i]<<" ";
std::cout<<std::endl;
}
int main()
{
std::cout<<"Enter array to be sorted (-1 to end)\n";
std::vector<int> array;
int num = 0;
while (num != -1)
{
std::cin>>num;
if (num != -1)
//add elements to the vector container
array.push_back(num);
}
//sort the array
BubbleSort(array);
std::cout<<"Sorted array is as\n";
PrintArray(array);
return 0;
}
我使用 g++ -std=c++11 Sort.cpp 来编译我的文件。
我的问题是冒泡排序不排序。
也许我正在按值传递矢量,但我不知道这是不是我第一次尝试使用 C++,所以我选择了使用矢量库。
我的代码是:
#include <iostream>
#include <vector>
using namespace std;
void bubbleSort(vector<int> a);
void printVector(vector<int> a);
int main(int argc, char const *argv[])
{
vector<int> a{3,2,6,1};
printVector(a);
bubbleSort(a);
printVector(a);
}
void bubbleSort(vector<int> a)
{
bool swapp = true;
while(swapp)
{
swapp = false;
for (int i = 0; i < a.size()-1; i++)
{
if (a[i]>a[i+1] )
{
a[i] += a[i+1];
a[i+1] = a[i] - a[i+1];
a[i] -=a[i+1];
swapp = true;
}
}
}
}
void printVector(vector<int> a)
{
for (int i=0; i <a.size(); i++)
{
cout<<a[i]<<" ";
}
cout<<endl;
}
主要是我声明了一个 int 向量类型,并制作了列表 {3,2,6,1}
之后 e 调用函数 printVector
假装在控制台上打印所有矢量数字并调用 bubbleSort
函数,最后再次打印。
您正在将向量作为值传递给您的函数,这意味着您正在对副本而不是原始向量进行排序,然后打印原始向量的副本。
在bubbleSort
函数中将参数更改为vector<int> &a
,在printVector
函数中将参数更改为vector<int> const &a
(因为不需要更改矢量内容从这里)。
顺便说一下,您的代码可能会受到签名者整数溢出导致的未定义行为的影响。
您应该使用另一种方法来交换元素,例如 std::swap
:
std::swap(a[i], a[i + 1]);
你需要传参;通过制作副本,您可以对临时副本进行排序,然后就是这样;它消失了,原始文件仍未排序,因为,再一次,你只排序了整个向量的临时副本。
因此将 vector<int> a
更改为 vector<int>& a
。
这是固定的代码:
http://coliru.stacked-crooked.com/a/2f118555f585ccd5
#include <iostream>
#include <vector>
using namespace std;
void bubbleSort(vector<int>& a);
void printVector(vector<int> a);
int main(int argc, char const *argv[])
{
vector<int> a {3,2,6,1};
printVector(a);
bubbleSort(a);
printVector(a);
}
void bubbleSort(vector<int>& a)
{
bool swapp = true;
while(swapp){
swapp = false;
for (size_t i = 0; i < a.size()-1; i++) {
if (a[i]>a[i+1] ){
a[i] += a[i+1];
a[i+1] = a[i] - a[i+1];
a[i] -=a[i+1];
swapp = true;
}
}
}
}
void printVector(vector<int> a){
for (size_t i=0; i <a.size(); i++) {
cout<<a[i]<<" ";
}
cout<<endl;
}
我也坚持了一段时间。 VermillionAzure 已经很好地回答了这个问题,但仍然粘贴我的解决方案。我没有使用 std::swap 只是因为我喜欢用手做更多事情。
#include <iostream>
#include <vector>
//function to swap values
//need to pass by reference to sort the original values and not just these copies
void Swap (int *a, int *b)
{
int temp = *a;
*a = *b;
*b = temp;
}
void BubbleSort (std::vector<int> &array)
{
std::cout<<"Elements in the array: "<<array.size()<<std::endl;
//comparisons will be done n times
for (int i = 0; i < array.size(); i++)
{
//compare elemet to the next element, and swap if condition is true
for(int j = 0; j < array.size() - 1; j++)
{
if (array[j] > array[j+1])
Swap(&array[j], &array[j+1]);
}
}
}
//function to print the array
void PrintArray (std::vector<int> array)
{
for (int i = 0; i < array.size(); i++)
std::cout<<array[i]<<" ";
std::cout<<std::endl;
}
int main()
{
std::cout<<"Enter array to be sorted (-1 to end)\n";
std::vector<int> array;
int num = 0;
while (num != -1)
{
std::cin>>num;
if (num != -1)
//add elements to the vector container
array.push_back(num);
}
//sort the array
BubbleSort(array);
std::cout<<"Sorted array is as\n";
PrintArray(array);
return 0;
}