Pagini recente » Cod sursa (job #1462175) | Statistici Moraru Vlad (vladutu) | Cod sursa (job #2954598) | Cod sursa (job #227845) | Cod sursa (job #658935)
Cod sursa(job #658935)
#include <stdio.h>
int v[500001],lung;
void citire();
void afisare();
void mergesort(int a[100], int l, int r);
void interclasare(int a[100],int l,int m,int r);
int main()
{
citire();
mergesort(v,1,lung);
afisare();
return 0;
}
void citire()
{
FILE* file=fopen("algsort.in","r");
fscanf(file,"%d\n",&lung);
for(int i=1;i<=lung;++i)
fscanf(file,"%d",&v[i]);
fclose(file);
}
void mergesort(int a[100], int l, int r)
{
if (l==r) return;
int m=(l+r)/2;
mergesort(a,l,m);
mergesort(a,m+1,r);
interclasare(a,l,m,r);
}
void interclasare(int a[100],int l,int m,int r)
{
int i=l,j=m+1,ind=0,c[100];
while (ind<=r-l+1)
{
if ((i<=m)&&(j<=r))
{
if (a[i]<a[j])
c[ind++]=a[i++];
else c[ind++]=a[j++];
}
else if ((i>m)&&(j<=r)) c[ind++]=a[j];
else if ((i<=m)&&(j>r)) c[ind++]=a[i];
}
for (int b=0;b<r-l+1;++b)
a[l+b]=c[b];
}
void afisare()
{
FILE* file=fopen("algsort.out","w+");
for(int i=1;i<=lung;++i)
printf("%d ",v[i]);
printf("\n");
}