Guest User

bubble

a guest
Apr 12th, 2014
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.90 KB | None | 0 0
  1.  
  2. #include <stdio.h>
  3.  
  4. int main(void)
  5. {
  6.     int largest;
  7.     int swap = 0;
  8.     int array[ ]  =  {2, 3, 1, 6, 4, 7};
  9.     int arraySize  =  6;
  10.     int i;
  11.     int j;
  12.    
  13.     for( i = 0; i < arraySize; ++i)
  14.     {
  15.         printf("Array %i is : %i\n", i + 1, array[i]);
  16.     }
  17.  
  18.     printf("\n\n\n");
  19.    
  20.     // this outer loop will search through the array by the amount of elements
  21.     for ( i = 0; i < arraySize; ++i)
  22.     {
  23.    
  24.         largest = array[i];
  25.    
  26.    
  27.    
  28.         // inner loop will check each element against each other
  29.        
  30.         for ( j = i + 1; j < arraySize; j++)
  31.         {
  32.             if (largest < array[j])
  33.             {
  34.                 largest = array[j];
  35.                 swap = j;
  36.                
  37.             }
  38.        
  39.         }
  40.        
  41.         // swap the outer element for the largest, and move what was
  42.         // in the outer to the element it was taken from
  43.        
  44.         array[swap] = array[j];
  45.        
  46.         array[j] = largest;
  47.        
  48.  
  49.     }
  50.    
  51.     for( i = 0; i < arraySize; ++i)
  52.     {
  53.         printf("Array %i is : %i\n", i + 1, array[i]);
  54.     }
  55.  
  56.  
  57. }
Advertisement
Add Comment
Please, Sign In to add comment