Pagini recente » Cod sursa (job #1472239) | Cod sursa (job #43378) | Cod sursa (job #343682) | Cod sursa (job #2070787) | Cod sursa (job #1015726)
#include<iostream>
#include<fstream>
using namespace std;
void interclasare(int a[100],int low,int mid,int high)
{int i=low,j=mid+1,c[100],k=low,t;
while (i<=mid && j<=high)
{if(a[i]<a[j])
c[k++]=a[i++];
else
c[k++]=a[j++];
}
for(t=i;t<=mid;t++)
c[k++]=a[t];
for(t=j;t<=high;t++)
c[k++]=a[t];
for(t=low;t<=high;t++)
a[t]=c[t];
}
void mergesort(int a[100],int low,int high)
{int mid;
if (low<high)
{mid=(low+high)/2;
mergesort(a,low,mid);
mergesort(a,mid+1,high);
interclasare(a,low,mid,high);
}
}
void citire (int a[100], int &n)
{ifstream f("algsort.in");
int i;
f>>n;
for(i=0;i<n;i++)
f>>a[i];
f.close();
}
void afisare (int a[100],int n)
{int i;
ofstream g("algsort.out");
for(i=0;i<n;i++)
g<<a[i]<<" ";
g.close();
}
int main()
{int a[100],n;
citire(a,n);
mergesort(a,0,n-1);
afisare(a,n);
return 0;
}