ALGORITMO MERGESORT
Autor: Prof. Nilton | Publicado: 2011-10-11T20:32:00-03:00

Ainda existe uma discussão sobre o assunto, mas apareceram evidências de que o algoritmo mergesort foi proposto por John Von Neumann em 1945. Essa discussão existe, pois estudar as várias contribuições que ele fez é, ao mesmo tempo, complexa e&nbsp;fascinante. Essa complexidade devesse em parte a existência de muitas fontes de informação, algumas pouco acessíveis, outras discordantes entre si ou polêmicas.&nbsp;A atribuição a ele veio de Knuth, que argumentou no seu livro ‘Arte de Programação Computacional: Ordenando e Procurando’ que Von Neumann foi o primeiro a descrever a idéia.
GIACON, André P., et al. MERGESORT. Faculdade de Tecnologia - Unicamp, 2010

void mergesort(int *vet,int inicio,int fim){ &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;if(inicio == fim ) { return; } &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;int meio = (inicio + fim )/2; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;mergesort(vet, inicio, meio );&nbsp; &nbsp; &nbsp;mergesort(vet, meio + 1, fim);&nbsp; &nbsp; &nbsp;int k = 0;&nbsp; &nbsp; &nbsp;int i = inicio;&nbsp; &nbsp; &nbsp;int j = meio + 1;&nbsp; &nbsp; &nbsp;//A linha abaixo cria um novo vetor auxiliar&nbsp; &nbsp; &nbsp;int *vetaux = (int *) malloc(sizeof (int) * (fim-inicio+1)); &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;while( i &lt; meio + 1 || j &lt; fim + 1 ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if( i == meio + 1 ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;vetaux[k] = vet[j];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;j++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;k++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}else{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if ( j == fim + 1 ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; vetaux[k] = vet[i];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; i++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; k++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}else{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if( vet[i] &lt; vet[j] ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;vetaux[k] = vet[i];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;i++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;k++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}else{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;vetaux[k] = vet[j];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;j++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;k++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp;//Este laco copia o vetor auxiliar no vetor principal&nbsp; &nbsp; &nbsp;for( i = inicio; i &lt;= fim; i++ ){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; vet[i] = vetaux[i - inicio];&nbsp; &nbsp; &nbsp;}}