Advertisement
paulogp

Bubble sort

Aug 7th, 2011
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.82 KB | None | 0 0
  1. // apple xcode
  2. // paulogp
  3.  
  4. /* bubble sort */
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7.  
  8. void bubble_sort (int v[], int n);
  9.  
  10. // ordenacao por troca (bubble)
  11. void bubble_sort (int v[], int n)
  12. {
  13.     int i, j, temp;
  14.    
  15.     for (i = n; i > 0; i--)
  16.     {
  17.         for (j = 1; j <= i; j++)
  18.         {
  19.             if (v[j - 1] > v[j])
  20.             {
  21.                 temp = v[j - 1];
  22.                 v[j - 1] = v[j];
  23.                 v[j] = temp;
  24.             }
  25.         }
  26.        
  27.         printf("%i", v[i]);
  28.     }
  29. }
  30.  
  31. int main (int argc, const char * argv[])
  32. {
  33.     int the_table[6] = {1, 3, 5, 4, 6, 2};
  34.     int the_table_size = sizeof(the_table) / sizeof(int); // length of array
  35.    
  36.     printf("inicio\n");
  37.     bubble_sort(the_table, the_table_size);
  38.     printf("\n");
  39.     printf("fim\n");
  40.    
  41.     return 0;
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement