Pagini recente » Cod sursa (job #2518155) | Cod sursa (job #1013961) | Cod sursa (job #3173687) | Cod sursa (job #197354) | Cod sursa (job #978349)
Cod sursa(job #978349)
#include <iostream>
#include <fstream>
using namespace std;
int v[500002], n;
void ShellSort( int *a, int n )
{
int h, i, j, k;
for ( h = n; h; h >>= 1 )
for ( i = h; i <= n; i++ )
{
k = a[i];
for ( j = i; j >= h && k < a[j - h]; j -= h )
a[j] = a[j - h];
a[j] = k;
}
}
void ShellSort1( int *a, int n )
{
for ( int increment = n >> 1; increment > 0; increment >>= 1 )
for ( int i = increment; i <= n; i++ )
{
int temp = a[i];
int j;
for ( j = i; j >= increment; j -= increment )
{
if ( temp < a[j - increment] )
a[j] = a[j - increment];
else
break;
}
a[j] = temp;
}
}
int main()
{
ifstream f("algsort.in");
ofstream g("algsort.out");
f >> n;
for ( int i = 1; i <= n; i++ )
f >> v[i];
ShellSort1( v, n );
for ( int i = 1; i <= n; i++ )
g << v[i] << " ";
return 0;
}