domingo, 7 de abril de 2019

Ordenando um vetor com merge sort

Ordenação por fusão, que é um dos sinônimos
em português da palavra "merge", ou mistura.
é um tipo de ordenação de alta eficiência
mas de grande complexidade para entender.
Ficou popularmente dito que se deve dividir
para conquistar, e seu funcionamento envolve
recursividade, portanto um alto nível de memória
deve ser notado pelo seu tempo de execução
acima de outros, concorrentes, mesmo assim
em alguns casos o uso é recomendado.

Veja abaixo imagens do programa em execução:




Veja abaixo o código do programa:


#include <iostream.h>
#include <conio.h>
#define TAM 100
bool x = false;
/*============================================================================*/
void Informe ( ) {
     textcolor ( LIGHTBLUE );
     gotoxy ( 26, 20 );
     printf ( "Criado por: " );
     textcolor ( LIGHTMAGENTA );
     gotoxy ( 126, 20 );
     printf ( "Samuel Lima" );
     textcolor ( BLACK );
     gotoxy ( 23, 23 );
     printf ( "                                " );
     gotoxy ( 26, 21 );
     printf ( "sa_sp10@hotmail.com" );
     textcolor ( LIGHTRED );
     gotoxy ( 36 , 23 );
     printf ( "MUITO OBRIGADO" );
}
/*============================================================================*/
//Criando uma moldura na tela do dos
int Moldura ( int tam_lin_ini, int tam_lin_fim,
         int tam_ini_col, int tam_fim_col ) {
     int i, c;
     for ( i = tam_lin_ini; i < tam_lin_fim; i++ ) {
         for ( c = tam_ini_col; c < tam_fim_col; c++ ) {
              gotoxy ( c, i );
              textbackground ( WHITE );
              printf ( " " );
         }
     }
     return 0;
}
/*============================================================================*/
int Cria_Vetor ( int vet [ ] ) {
     textcolor ( LIGHTRED );
     gotoxy ( 23 , 3 );
     printf ( "ORDENANDO UM VETOR COM MERGE SORT" );
     int i, r = 0, temp;
     for ( i = 0; i < TAM; i++ ) {
         vet [ i ] = i;
     }
     srand ( time ( NULL ) );
     for ( i = 0; i < TAM; i++ ) {
         r = rand ( ) % TAM;
         temp = vet [ i ];
         vet [ i ] = vet [ r ];
         vet [ r ] = temp;
     }
     return 0;
}
/*============================================================================*/
void merge ( int vet [ ], int tam_vet_1, int tam_vet_2 ) {
     int *tempVet_1 = new int [ tam_vet_1 + tam_vet_2 ];
     int cont_1 = 0;
     int cont_2 = 0;
     int cont_3 = 0;
     while ( cont_2 < tam_vet_1 && cont_3 < tam_vet_2 ) {
         if ( vet [ cont_2 ] <= vet [ tam_vet_1 + cont_3 ] ) {
              tempVet_1 [ cont_1 ] = vet [ cont_2 ];
              cont_2++;
         } else {
              tempVet_1 [ cont_1 ] = vet [ tam_vet_1 + cont_3 ];
              cont_3++;
         }
         cont_1++;
     }
     while ( cont_2 < tam_vet_1 ) {
         tempVet_1 [ cont_1 ] = vet [ cont_2 ];
         cont_1++;
         cont_2++;
     }
     while ( cont_3 < tam_vet_2 ) {
         tempVet_1 [ cont_1 ] = vet [ tam_vet_1 + cont_3 ];
         cont_3++;
         cont_1++;
     }
     for ( int cont_4 = 0; cont_4 < tam_vet_1 + tam_vet_2; cont_4++ )
         vet [ cont_4 ] = tempVet_1 [ cont_4 ];
     delete tempVet_1;
}
/*============================================================================*/
void merge_sort ( int vet [ ], int tam_vet ) {
     if ( tam_vet   > 1 ) {
         int tempVet_1 = tam_vet   / 2;
         int tempVet_2 = tam_vet   - tempVet_1;
         merge_sort ( vet, tempVet_1 );
         merge_sort ( vet + tempVet_1, tempVet_2 );
         merge ( vet, tempVet_1, tempVet_2 );
     }
}
/*============================================================================*/
void Imprime_Vetor ( int vet [ ] ) {
     int i;
     gotoxy ( 25, 7 );
     for ( i = 0; i < TAM; i++ ) {
         if ( i == 10 ) {
              gotoxy ( 25, 8 );
         }
         if ( i == 20 ) {
              gotoxy ( 25, 9 );
         }
         if ( i == 30 ) {
              gotoxy ( 25, 10 );
         }
         if ( i == 40 ) {
              gotoxy ( 25, 11 );
         }
         if ( i == 50 ) {
              gotoxy ( 25, 12 );
         }
         if ( i == 60 ) {
              gotoxy ( 25, 13 );
         }
         if ( i == 70 ) {
              gotoxy ( 25, 14 );
         }
         if ( i == 80 ) {
              gotoxy ( 25, 15 );
         }
         if ( i == 90 ) {
              gotoxy ( 25, 16 );
         }
         if ( vet [ i ] >= 0 && vet [ i ] < 10 ) {
              textcolor ( BLUE );
              printf ( "0" );
         }
         //Para impressão do vetor desordenado
         if ( x == false ) {
              textcolor ( BLUE );
              printf ( "%d ", vet [ i ] );
         }
         //Para impressão do vetor ordenado
         if ( x == true ) {
              textcolor ( BLUE );
              printf ( "%d ", vet [ i ] );
         }
     }//chave de fechamento do laço for
     textcolor ( LIGHTRED );
     gotoxy ( 29, 23 );
     printf ( "PRESSIONE QUALQUER TECLA" );
     getche ( );
}
/*============================================================================*/
int main ( ) {
     system ( "title ORDENANDO UM VETOR COM MERGE SORT" );
     Moldura ( 2, 25, 2, 79 );
     textbackground ( WHITE );
     int vet [ TAM ];
     Cria_Vetor ( vet );
     textcolor ( LIGHTBLUE );
     gotoxy ( 25 , 5 );
     printf ( "Vetor gerado desordenado" );
     Imprime_Vetor ( vet );
     merge_sort ( vet, TAM );
     textcolor ( LIGHTBLUE );
     gotoxy ( 25 , 5 );
     printf ( "Vetor ordenado por merge sort" );
     Imprime_Vetor ( vet );
     Sleep ( 1800 );
     Informe ( );
     getche ( );
     return 0;
}

/*============================================================================*/

Nenhum comentário:

Postar um comentário

Observação: somente um membro deste blog pode postar um comentário.