Advertisement
rihardmarius

ordenar array

Nov 23rd, 2013
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.82 KB | None | 0 0
  1. #include <iostream>
  2. #include <array>
  3.  
  4. using namespace std;
  5.  
  6. void swap (int& a, int& b)
  7. {
  8.     int aux = a;
  9.     a = b;
  10.     b = aux;
  11. }
  12.  
  13. void ordenar_array (array<int,10>& a)
  14. {
  15.     int j; int c=0; bool b = false;
  16.     while (not b)
  17.     {
  18.         for (int i=0; i<9; i++)
  19.         {
  20.             if (a.at(i) > a.at(i+1))
  21.                 swap(a.at(i),a.at(i+1));
  22.             c++;
  23.         }
  24.         for (j=0; j<9; j++)
  25.             if (a.at(j) > a.at(j+1))
  26.             {
  27.                 b = false;
  28.                 break;
  29.             }
  30.  
  31.         if (j == 9)
  32.             b = true;
  33.     }
  34.  
  35.     cout << c << endl;
  36.  
  37. }
  38.  
  39. int main()
  40. {
  41.     array<int,10> arr = {0,1,4,7,2,5,8,3,6,9};
  42.  
  43.     ordenar_array(arr);
  44.  
  45.  
  46.  
  47.     for (int i=0; i<10; i++)
  48.         cout << arr.at(i) << ' ';
  49.     cout << endl;
  50.  
  51.  
  52.     return 0;
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement