Cod sursa(job #1477742)

Utilizator AlexandruValeanuAlexandru Valeanu AlexandruValeanu Data 26 august 2015 20:42:24
Problema Sortare prin comparare Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.82 kb
#include <bits/stdc++.h>

using namespace std;

void shell_sort(int A[], int n)
{
    const int gaps[] = {1, 2, 4, 8, 21, 56, 149, 404, 1098, 2982, 8104, 22027, 59875, 162756, 442414};

    for (int p = 14; p >= 0; p--)
    {
        int gap = gaps[p];

        for (int i = gap; i < n; ++i)
        {
            int temp = A[i];
            int j = i;

            while (j >= gap && A[j - gap] > temp)
            {
                A[j] = A[j - gap];
                j -= gap;
            }

            A[j] = temp;
        }
    }
}

const int MAX_N = 500000 + 1;

int A[MAX_N];
int N;

int main()
{
    ifstream in("algsort.in");
    ofstream out("algsort.out");

    in >> N;

    for (int i = 0; i < N; ++i)
        in >> A[i];

    shell_sort(A, N);

    for (int i = 0; i < N; ++i)
        out << A[i] << " ";

    return 0;
}