Advertisement
Guest User

Untitled

a guest
Oct 18th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.31 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5. //bubble sort from stackoverflow
  6. void bubbleSort(vector<int>&myVector)
  7. {
  8. bool swapp = true;
  9. while(swapp)
  10. {
  11. swapp = false;
  12. for (int i = 0; i < myVector.size()-1; i++)
  13. {
  14. if (myVector[i]>myVector[i+1] )
  15. {
  16. myVector[i] += myVector[i+1];
  17. myVector[i+1] = myVector[i] - myVector[i+1];
  18. myVector[i] -=myVector[i+1];
  19. swapp = true;
  20. }
  21. }
  22. }
  23. }
  24.  
  25. void printMyVector(vector<int>myVector)
  26. {
  27. for(int i = 0; i < myVector.size(); i++)
  28. {
  29. cout << "index:" << i << " " << myVector[i] << endl;
  30. }
  31. }
  32.  
  33. int main()
  34. {
  35. //seeds random numbers
  36. srand((unsigned)time(NULL));
  37.  
  38. //my testing data- size from 1-20
  39. vector<int>myVector;
  40. int a = rand() % 20 + 1; //1 to 20
  41.  
  42. //pushes back a ran num from 1-20
  43. for (int i =0; i < a; i++)
  44. {
  45. int b = rand() % 20 + 1; //1 to 20
  46. myVector.push_back(b);
  47. cout << "index:" << i << " random num: " << myVector[i] << endl;
  48. }
  49.  
  50. cout << endl;
  51. cout << "bubble sort: " << endl;
  52.  
  53. //calling bubble sort
  54. bubbleSort(myVector);
  55. printMyVector(myVector);
  56.  
  57. return 0;
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement