Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- using namespace std;
- //bubble sort from stackoverflow
- void bubbleSort(vector<int>&myVector)
- {
- bool swapp = true;
- while(swapp)
- {
- swapp = false;
- for (int i = 0; i < myVector.size()-1; i++)
- {
- if (myVector[i]>myVector[i+1] )
- {
- myVector[i] += myVector[i+1];
- myVector[i+1] = myVector[i] - myVector[i+1];
- myVector[i] -=myVector[i+1];
- swapp = true;
- }
- }
- }
- }
- void printMyVector(vector<int>myVector)
- {
- for(int i = 0; i < myVector.size(); i++)
- {
- cout << "index:" << i << " " << myVector[i] << endl;
- }
- }
- int main()
- {
- //seeds random numbers
- srand((unsigned)time(NULL));
- //my testing data- size from 1-20
- vector<int>myVector;
- int a = rand() % 20 + 1; //1 to 20
- //pushes back a ran num from 1-20
- for (int i =0; i < a; i++)
- {
- int b = rand() % 20 + 1; //1 to 20
- myVector.push_back(b);
- cout << "index:" << i << " random num: " << myVector[i] << endl;
- }
- cout << endl;
- cout << "bubble sort: " << endl;
- //calling bubble sort
- bubbleSort(myVector);
- printMyVector(myVector);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement