8

Bubble

  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. typedef float List[];
  6.  
  7. static void print( List tab, const int siz )
  8. {
  9.     for ( int i=0; i < siz; i++ )
  10.         cout << tab[ i ] << ((i < siz-1) ? ", " : "\n");
  11. }
  12.  
  13. inline void swap( float &x, float &y )
  14. {
  15.     float t = x;
  16.     x = y;
  17.     y = t;
  18. }
  19.  
  20. void sort( List tab, const int siz )
  21. {   // bubble sort
  22.     for ( int i=siz-1; i > 0; i-- ) {
  23.         print( tab, siz );
  24.         for ( int j=0; j < i; j++ )
  25.             if ( tab[j] > tab[j+1] )
  26.                 swap( tab[j], tab[j+1] );
  27.     }
  28. }
  29.  
  30. int main() {
  31.     List tab = { 55.55, 22.22, 99.99, 66.66, 44.44, 88.88, 33.33, 77.77 };
  32.     int siz = sizeof (tab) / sizeof (float);
  33.  
  34.     cout << "Sorting " << siz << " floats:\n";
  35.     sort( tab, siz );
  36.     print( tab, siz );
  37. }
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6. template <class T>
  7. static void print( T tab[], const int siz )
  8. {
  9.     for ( int i=0; i < siz; i++ )
  10.         cout << tab[ i ] << ((i < siz-1) ? ", " : "\n");
  11. }
  12.  
  13. template <class T>
  14. void sort( T tab[], const int siz )
  15. {   // bubble sort
  16.     T t;
  17.     for ( int i=siz-1; i > 0; i-- )
  18.         for ( int j=0; j < i; j++ )
  19.             if ( tab[j] > tab[j+1] ) {
  20.                 T t = tab[j];
  21.                 tab[j] = tab[j+1];
  22.                 tab[j+1] = t;
  23.             }
  24. }
  25.  
  26. int main() {
  27.     int siz;
  28.  
  29.     float tabi[] = { 55, 22, 99, 66, 44, 88, 33, 77 };
  30.     siz = sizeof (tabi) / sizeof (int);
  31.  
  32.     cout << "Sorting " << siz << " integers:\n";
  33.     sort( tabi, siz );
  34.     print( tabi, siz );
  35.  
  36.     float tabf[] = { 55.55, 22.22, 99.99, 66.66, 44.44, 88.88, 33.33, 77.77 };
  37.     siz = sizeof (tabf) / sizeof (float);
  38.  
  39.     cout << "Sorting " << siz << " floats:\n";
  40.     sort( tabf, siz );
  41.     print( tabf, siz );
  42.  
  43.     string tabs[10] = { "T", "a", "f", "Z", "F", "Q", "z", "q", "t", "A" };
  44.     siz = sizeof (tabs) / sizeof (string);
  45.  
  46.     cout << "Sorting " << siz << " strings:\n";
  47.     sort( tabs, siz );
  48.     print( tabs, siz );
  49. }

Comments

Your comment:
[p] [b] [i] [u] [s] [quote] [pre] [br] [code] [url] [email] strip help 2000

Enter a maximum of 2000 characters.
Improve the presentation of your text with the following formatting tags:
[p]paragraph[/p], [b]bold[/b], [i]italics[/i], [u]underline[/u], [s]strike[/s], [quote]citation[/quote], [pre]as is[/pre], [br]line break,
[url]http://www.izend.org[/url], [url=http://www.izend.org]site[/url], [email]izend@izend.org[/email], [email=izend@izend.org]izend[/email],
[code]command[/code], [code=language]source code in c, java, php, html, javascript, xml, css, sql, bash, dos, make, etc.[/code].