Advertisement
ChristianBarzalobre

Inserción C

Nov 28th, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.23 KB | None | 0 0
  1. #include <stdio.h>
  2. int main()
  3. {
  4.     int a[10] = {75,33,35,16,91,24,25,43,27,83};
  5.     int pasadas,i,j,almacena,intercambios = 0,comparaciones = 0,arreglo = 10;
  6.    
  7.     printf( "Arreglo horizontal antes de ordenar" );
  8.     printf( "\n" );
  9.     printf( "Valor   \t" );
  10.    
  11.     for ( i = 0; i < arreglo; i++ )
  12.     {
  13.         printf( "%d \t", a[ i ] );
  14.     }
  15.    
  16.     printf( "\n" );
  17.     printf( "Posicion\t" );
  18.    
  19.     for ( i = 1; i < arreglo+1; i++ )
  20.     {
  21.         printf( "%d \t", i );
  22.     }
  23.    
  24.    
  25.     for (pasadas = 1; pasadas < arreglo; pasadas++)
  26.     {
  27.         almacena = a[ pasadas ];
  28.         j = pasadas - 1;
  29.         while (j >= 0 && a[j] > almacena)
  30.         {
  31.             a[j + 1] = a[j];
  32.             j--;
  33.             comparaciones++;
  34.         }
  35.         a[j + 1] = almacena;
  36.         intercambios++;
  37.     }
  38.    
  39.    
  40.     printf( "\n\n" );
  41.     printf( "Una vez ordenado" );
  42.     printf( "\n" );
  43.     printf( "Valor   \t" );
  44.    
  45.     for ( i = 0; i < arreglo; i++ )
  46.     {
  47.         printf( "%d \t", a[ i ] );
  48.     }
  49.    
  50.     printf( "\n" );
  51.     printf( "Posicion\t" );
  52.    
  53.     for ( i = 1; i < arreglo+1; i++ )
  54.     {
  55.         printf( "%d \t", i );
  56.     }
  57.    
  58.     printf( "\n\n" );
  59.     printf( "Numero de comparaciones efectuadas = %d", comparaciones );
  60.     printf( "\n\n" );
  61.     printf( "Numero de intercambios = %d", intercambios );
  62.     getchar();
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement