Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- using namespace std;
- template <class T>
- void Swap(T &a, T&b)
- {
- T temp = a;
- a = b;
- b = temp;
- }
- void arrayIntOutput(const vector<int>&a)
- {
- for (int i = 0; i < a.size(); i++)
- cout << "a[" << i << "] = " << a[i] << endl;
- }
- void arrayOutput1(int *a, int n)
- {
- for (int i = 0; i < n; i++)
- cout << "a[" << i << "] = " << *(a + i) << endl;
- }
- void arrayIntInput(vector <int>&a)
- {
- int x;
- while (cin >> x)
- {
- a.push_back(x);
- }
- }
- void Bubble_Sort(int *a, int n)
- {
- bool haveSwap = false;
- for (int i = 0; i < n - 1; i++)
- {
- haveSwap = false;
- for (int j = 0; j < n - i - 1; j++)
- {
- if (*(a + j) >*(a + j + 1))
- {
- Swap(*(a + j), *(a + j + 1));
- haveSwap = true;
- }
- }
- if (haveSwap == false)
- break;
- }
- }
- void Process(vector<int>&a, int *p)
- {
- cout << "Nhap cac phan tu cua mang, nhan ki tu bat ki de dung viec nhap." << endl;
- arrayIntInput(a);
- cout << "Mang ban dau khi nhap vao la: " << endl;
- arrayIntOutput(a);
- int n = a.size();
- p = &a[0];
- Bubble_Sort(p, n);
- cout << "Mang dau khi da \"Bubble Sort\" la: " << endl;
- arrayOutput1(p, n);
- cout << endl;
- }
- int main()
- {
- vector <int> a;
- int p;
- Process(a, &p);
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement