VasilM

bubble_sort

Nov 20th, 2012
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.61 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. void bubble_sort( int a[], int n ){
  6.     for( int i = n-2; i >= 0; i--)
  7.         for( int j = 0; j <= i; j++)
  8.             if( a[j] > a[j+1] ){
  9.                 int t = a[j];
  10.                 a[j] = a[j+1];
  11.                 a[j+1] = t;
  12.             }
  13. }
  14.  
  15. int main(){
  16.     setlocale(0,"");
  17.  
  18.     int *a, n;
  19.     cout << "бр числа ->";
  20.     cin >> n;
  21.     a = new int[n];
  22.  
  23.     for( int i=0; i<n; i++ ) cin >> a[i];
  24.  
  25.     bubble_sort( a, n );
  26.  
  27.     cout << "след прилагане на сортиране по метод на мехурчето:\n";
  28.     for( int i=0; i<n; i++ ) cout << a[i] << " ";
  29.     delete []a;
  30.     cout << endl;
  31.     return 0;
  32. }
Advertisement
Add Comment
Please, Sign In to add comment