Last active
July 16, 2021 02:55
-
-
Save fddemora/0ade390cad7d9712e8b67e75c3e86d48 to your computer and use it in GitHub Desktop.
C++ bubble sort
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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