Skip to content

Instantly share code, notes, and snippets.

@fddemora
Last active July 16, 2021 02:55
Show Gist options
  • Save fddemora/0ade390cad7d9712e8b67e75c3e86d48 to your computer and use it in GitHub Desktop.
Save fddemora/0ade390cad7d9712e8b67e75c3e86d48 to your computer and use it in GitHub Desktop.
C++ bubble sort
#include<iostream>
#include<vector>
using namespace std;
void SortVector(vector<int>& myVec);
int main(){
int size, input;
cin >> size; // get size from user.
vector<int> x(size);
for(int i = 0; i < size; i++){ // list of int from user.
cin >> input;
x.at(i) = input;
}
SortVector(x);
for(int i = 0; i < size; i++){
cout << x.at(i) << ' ';
}
cout << endl;
}
void SortVector(vector<int>& myVec){
int n = myVec.size();
for (int i = 0; i < n-1; i++)
for (int j = 0; j < n-i-1; j++)
if (myVec.at(j) > myVec.at(j+1))
{
// swap temp and arr[i]
int temp = myVec.at(j);
myVec.at(j) = myVec.at(j+1);
myVec.at(j+1) = temp;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment