Advertisement
Guest User

Untitled

a guest
Apr 5th, 2020
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.81 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. void bubble_sort();
  4. int a[30], n,cnt=0;
  5. int main()
  6. {
  7.     int i;
  8.     printf("\nEnter size of an array: ");
  9.     scanf("%d", &n);
  10.     printf("\nEnter elements of an array:\n");
  11.     for(i=0; i<n; i++)
  12.         scanf("%d", &a[i]);
  13.     bubble_sort();
  14.     printf("\nAfter sorting:\n");
  15.     for(i=0; i<n; i++)
  16.         printf("\n%d", a[i]);
  17.     return 0;
  18. }
  19. void bubble_sort()
  20. {
  21.     int i,j,k,z=0;
  22.     for(i=0;i<n-1;i++)
  23.     {
  24.         for(j=0;j<n-i-1;j++)
  25.         {
  26.             cout << ++cnt << ": ";
  27.             for(k=0;k<n;k++)
  28.                 cout<<a[k]<<" ";
  29.             cout << endl;
  30.             if(a[j]>a[j+1])
  31.             {
  32.                 swap(a[j],a[j+1]);
  33.                 z=1;
  34.             }
  35.         }
  36.        if(z==0)
  37.          break;
  38.     }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement