Created
May 9, 2014 13:49
-
-
Save kimitoboku/2ff2d0f95158dae5f20d to your computer and use it in GitHub Desktop.
merge sort
This file contains 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> | |
int temp[100]; | |
void msort(int A[],int left,int right){ | |
if(left >= right){ | |
return; | |
}else{ | |
int middle = (left+right)/2; | |
msort(A,left,middle); | |
msort(A,middle+1,right); | |
int i,j,k; | |
for(i=left;i<=middle;i++){ | |
temp[i] = A[i]; | |
} | |
for(i=middle+1,j=right;i<=right;i++,j--){ | |
temp[i] = A[j]; | |
} | |
i = left; | |
j = right; | |
for(k=left;k<=right;k++){ | |
if(temp[i] >= temp[j]){ | |
A[k] = temp[j--]; | |
}else{ | |
A[k] = temp[i++]; | |
} | |
} | |
} | |
} | |
int main(void){ | |
int n; | |
int array[100]; | |
scanf("%d",&n); | |
int i; | |
for(i=0;i<n;i++){ | |
scanf("%d",&array[i]); | |
} | |
msort(array,0,n-1); | |
for(i=0;i<n;i++){ | |
printf("%d ",array[i]); | |
} | |
puts(""); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment