by
3 6
0
3
3,900
3
Top 1% !
Famous
Specified
Popularity: 2923rd place

Published on:

No tags for this snippet yet.
LanguageC
SourceGitHub

#sort #sorting #puc apc-b 2016

#sort #sorting #puc apc-b 2016: 
mergesort.c
Copy Embed Code
<iframe id="embedFrame" style="width:600px; height:300px;"
src="https://www.snip2code.com/Embed/842903/-sort--sorting--puc-apc-b-2016?startLine=0"></iframe>
Click on the embed code to copy it into your clipboard Width Height
Leave empty to retrieve all the content Start End
#include <stdio.h> void merge(int v[], int inicio, int meio, int fim) { int aux[100]; int aux_inicio = inicio, aux_meio = meio + 1, k=0; while(aux_inicio <= meio && aux_meio <= fim) if(v[aux_inicio] <= v[aux_meio]) aux[k++] = v[aux_inicio++]; else aux[k++] = v[aux_meio++]; for(aux_inicio = aux_inicio; aux_inicio <= meio; aux_inicio++) aux[k++] = v[aux_inicio]; for(aux_meio = aux_meio; aux_meio <= fim; aux_meio++) aux[k++] = v[aux_meio]; k=0; for(aux_inicio = inicio; aux_inicio <= fim; aux_inicio++) //copia aux para v v[aux_inicio] = aux[k++]; } void merge_sort(int v[], int inicio, int fim) { int meio = (inicio + fim) / 2; if(inicio < fim) { merge_sort(v, inicio, meio); merge_sort(v, meio + 1, fim); merge(v, inicio, meio, fim); } } int main (void) { int var[] = {0, 6, 4, 2, 16, 22, 3, 9}; merge_sort(var, 0, 7); int i; for (i = 0; i < 8; i++) printf("%d ", var[i]); return 0; }
If you want to be updated about similar snippets, Sign in and follow our Channels

blog comments powered by Disqus