Best_Project

Sort The Array_BBSort

Jun 18th, 2018
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.79 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4.  
  5. void BubbleSort(int a[],int n)
  6. {
  7.     int i,j;
  8.     for(i = 0; i < n-1;i ++)
  9.     {
  10.         int flag = 0;
  11.         for(j = 0;j < n-i-1;j++)
  12.         {
  13.             if(a[j] > a[j+1]){
  14.                 int temp = a[j];
  15.                 a[j]=a[j+1];
  16.                 a[j+1]=temp;
  17.             flag++;}
  18.         }
  19.         if(flag == 0) break;
  20.     }
  21. }
  22. void Nhap(int a[],int n)
  23. {
  24.     for(int i = 0; i < n;i++)
  25.     {
  26.         cin>>a[i];
  27.     }
  28. }
  29. void XuatMang(int a[],int n)
  30. {
  31.     for(int i = 0; i < n;i++)
  32.     {
  33.         cout<<a[i]<<" ";
  34.     }
  35.    cout<<endl;
  36. }
  37. int main() {
  38.     //code
  39.     int testcase,n;
  40.     cin>>testcase;
  41.     while(testcase--)
  42.     {
  43.         cin>>n;
  44.        int  a[n];
  45.         Nhap(a,n);
  46.         BubbleSort(a,n);
  47.         XuatMang(a,n);
  48.     }
  49.     return 0;
  50. }
Advertisement
Add Comment
Please, Sign In to add comment