SHARE
TWEET

Sắp xếp mảng tăng dần (Interchange Sort)

anhkiet2507 Sep 28th, 2018 11,290 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5. #define MAX 10
  6. void NhapMang(int arr[], int &n);
  7. void XuatMang(int arr[], int &n);
  8. void hoanVi(int &a, int &b);
  9. void sapXepTang(int arr[], int n);
  10. int main()
  11. {
  12.     int array[MAX];
  13.     int size;
  14.     NhapMang(array, size);
  15.     system("cls");
  16.     sapXepTang(array, size);
  17.     cout << "Mang duoc sap xep theo thu tu tang dan la" << endl;
  18.     XuatMang(array, size);
  19.     system("pause");
  20.     return 0;
  21. }
  22. void XuatMang(int arr[], int &n)
  23. {
  24.     for (int i = 0; i < n; i++)
  25.     {
  26.         cout << "arr [" << i + 1 << "] = " << arr[i] << endl;
  27.     }
  28. }
  29.  
  30. void NhapMang(int arr[], int &n)
  31. {
  32.     cout << "Nhap so phan tu cua mang:";
  33.     cin >> n;
  34.     cout << "Nhap cac phan tu:" << endl;
  35.     for (int i = 0; i < n; i++)
  36.     {
  37.         cout << "Nhap phan tu thu [" << i + 1 << "] = ";
  38.         cin >> arr[i];
  39.     }
  40.  
  41. }
  42. void hoanVi(int &a, int &b)
  43. {
  44.     int temp = a;
  45.     a = b;
  46.     b = temp;
  47. }
  48.  
  49. void sapXepTang(int arr[], int n)
  50. {
  51.     for (int i = 0; i < n - 1; i++)
  52.     {
  53.         for (int j = i + 1; j < n; j++)
  54.         {
  55.             if (arr[i] > arr[j])
  56.                 hoanVi(arr[i], arr[j]);
  57.         }
  58.     }
  59. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top