Cod sursa(job #660421)

Utilizator the_snyper06FMI - ALexandru Mihai the_snyper06 Data 12 ianuarie 2012 23:16:30
Problema Sortare prin comparare Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.74 kb
#include<cstdio>
#include<algorithm>

using namespace std;
int n;
int v[ 1 << 21 ], gaps[22];

void shell_sort(int *v) {
	int i, j, poz;
	
	for (i = 20, gaps[21] = 1; i >= 0; i--) gaps[i] = gaps[i + 1] * 2 + 1;	
	for (i = 0; i < 22; i++) {
		if (gaps[i] > n) continue;
		
		for(j = gaps[i]; j < n; j++) { // insertion sort
			poz = j;
	
			while(poz >= gaps[i] && v[poz] < v[poz - gaps[i]]) {
				swap(v[poz], v[poz - gaps[i]]);
				poz = poz - gaps[i];
			}
		}
	}
}

int main() {
	int i;
	
	freopen("algsort.in", "r", stdin), freopen("algsort.out", "w", stdout);
	scanf("%d", &n);
	for(i = 0; i < n; i++) scanf("%d", &v[i]);
	
	shell_sort(v);
	//sort(v, v + n);
	for(i = 0; i < n; i++) printf("%d ", v[i]);
	
	return 0;
}