Advertisement
Guest User

Untitled

a guest
Aug 18th, 2017
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.91 KB | None | 0 0
  1. //---------------------------------------------------------------//
  2. //                        INSERTION SORT                         //
  3. //---------------------------------------------------------------//
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <math.h>
  7. #define n 10
  8. //---------------------------------------------------------------//
  9. void insertion( int v[n] )
  10. {
  11.     int i, j, x, cont = 0;
  12.  
  13.     for( j = 1 ; j < n ; j++ )
  14.     {
  15.         x = v[j];
  16.         for( i = j-1 ; (i >= 0) && (v[i] > x) ; i-- )
  17.         {
  18.             v[i+1] = v[i];
  19.             cont++;
  20.         }
  21.     }
  22.     printf("cont = %d\n\n", cont);
  23. }
  24. //---------------------------------------------------------------//
  25. int main (void)
  26. {
  27.     int y, m[n];
  28.  
  29.     for( y = 0 ; y < n ; y++ ) scanf("%d", &m[y]);
  30.  
  31.     insertion(m[n]);
  32.  
  33.     for( y = 0 ; y < n ; y++ ) printf("m[%d] = %d", y, m[y]);
  34.  
  35.     return 0;
  36.     system("pause");
  37.  
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement