Created
May 22, 2013 19:40
-
-
Save ccjmk/5630295 to your computer and use it in GitHub Desktop.
MergeSort sorting algorithm written in C with recursive functions.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
void mergesort(int a[],int low,int high); | |
void merge(int a[], int low, int high); | |
int main() | |
{ | |
int a[40],fin,i; | |
printf("ingrese dim\n"); | |
scanf("%d",&fin); | |
for(i=0; i<fin; i++){ | |
printf("ingr valor\n"); | |
scanf("%d",&a[i]); | |
} | |
mergesort(a, 0, fin-1); | |
printf("\n el vector ordenado por merge"); | |
for(i=0; i<fin; i++){ | |
printf("\n %d",a[i]); | |
} | |
return 0; | |
} | |
void mergesort(int a[],int low,int high){ | |
int aux[40],i; | |
if (low<high){ | |
//divide | |
mergesort(a,low,(low+high)/2); | |
mergesort(a,(low+high)/2+1,high); | |
//combina | |
merge(a, low, high); | |
} | |
return; | |
} | |
void merge(int a[], int low, int high){ | |
int m1=0; | |
int m2=(low+high)/2-low+1; | |
int i, aux[40]={0}; | |
for ( i=0; i<high-low+1; i++) | |
aux[i]=a[low+i]; | |
for( i=0; i<high-low+1; i++){ | |
if(m2 <= high-low) | |
if(m1 <= (low+high)/2 -low) | |
if(aux[m1] > aux[m2]) | |
a[i+low] = aux[m2++]; | |
else | |
a[i+low] = aux[m1++]; | |
else | |
a[i+low]=aux[m2++]; | |
else | |
a[i+low]=aux[m1++]; | |
} | |
return; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment