Advertisement
Guest User

Bai2

a guest
May 22nd, 2019
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.26 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4. template <class T>
  5.  
  6. void Swap(T &a, T&b)
  7. {
  8. T temp = a;
  9. a = b;
  10. b = temp;
  11. }
  12.  
  13. void arrayIntOutput(const vector<int>&a)
  14. {
  15. for (int i = 0; i < a.size(); i++)
  16. cout << "a[" << i << "] = " << a[i] << endl;
  17. }
  18.  
  19. void arrayOutput1(int *a, int n)
  20. {
  21. for (int i = 0; i < n; i++)
  22. cout << "a[" << i << "] = " << *(a + i) << endl;
  23. }
  24.  
  25. void arrayIntInput(vector <int>&a)
  26. {
  27. int x;
  28. while (cin >> x)
  29. {
  30. a.push_back(x);
  31. }
  32. }
  33.  
  34. void Bubble_Sort(int *a, int n)
  35. {
  36. bool haveSwap = false;
  37. for (int i = 0; i < n - 1; i++)
  38. {
  39. haveSwap = false;
  40. for (int j = 0; j < n - i - 1; j++)
  41. {
  42. if (*(a + j) >*(a + j + 1))
  43. {
  44. Swap(*(a + j), *(a + j + 1));
  45. haveSwap = true;
  46. }
  47.  
  48. }
  49. if (haveSwap == false)
  50. break;
  51. }
  52. }
  53.  
  54. void Process(vector<int>&a, int *p)
  55. {
  56. cout << "Nhap cac phan tu cua mang, nhan ki tu bat ki de dung viec nhap." << endl;
  57. arrayIntInput(a);
  58. cout << "Mang ban dau khi nhap vao la: " << endl;
  59. arrayIntOutput(a);
  60. int n = a.size();
  61. p = &a[0];
  62. Bubble_Sort(p, n);
  63. cout << "Mang dau khi da \"Bubble Sort\" la: " << endl;
  64. arrayOutput1(p, n);
  65. cout << endl;
  66. }
  67.  
  68. int main()
  69. {
  70. vector <int> a;
  71. int p;
  72. Process(a, &p);
  73. system("pause");
  74. return 0;
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement