Cod sursa(job #978543)

Utilizator AlexandruValeanuAlexandru Valeanu AlexandruValeanu Data 29 iulie 2013 00:09:31
Problema Sortare prin comparare Scor 20
Compilator cpp Status done
Runda Arhiva educationala Marime 2.15 kb
#include <stdio.h>
#include <math.h>
#include <stdlib.h>

const int THRESHOLD = 175;
const int CLASS_SIZE = 175;     /* minimum value for m */

void flashsort(int a[],int n,int m,int *ctr)
{
  /* declare variables */
  int *l,nmin,nmax,i,j,k,nmove,nx,mx;
  int c1,c2,flash,hold;

  /* allocate space for the l vector */
  l=(int*)calloc(m,sizeof(int));

  /***** CLASS FORMATION ****/

  nmin=nmax=0;
  for (i=0 ; i<n ; i++)
     if (a[i] < a[nmin]) nmin = i;
     else if (a[i] > a[nmax]) nmax = i;

  if ( (a[nmax]==a[nmin]) && (ctr==0) )
  {
     printf("All the numbers are identical, the list is sorted\n");
     return;
  }

  c1=(m-1.0)/(a[nmax]-a[nmin]) ;
  c2=a[nmin];

  l[0]=-1; /* since the base of the "a" (data) array is 0 */
  for (k=1; k<m ; k++) l[k]=0;

  for (i=0; i<n ; i++)
  {
    k=floor(c1*(a[i]-c2) );
    l[k]+=1;
   }

   for (k=1; k<m ; k++) l[k]+=l[k-1];

   hold=a[nmax];
   a[nmax]=a[0];
   a[0]=hold;
  /**** PERMUTATION *****/

   nmove=0;
   j=0;
   k=m-1;

   while(nmove<n)
   {
      while  (j  >  l[k] )
      {
	j++;
	k=floor(c1*(a[j]-c2) ) ;
      }

      flash=a[ j ] ;

      while ( j <= l[k] )
      {
	k=floor(c1*(flash-c2));
	hold=a[ l[k] ];
	a[ l[k] ] = flash;
	l[k]--;
	flash=hold;
	nmove++;
      }
   }

 /**** Choice of RECURSION or STRAIGHT INSERTION *****/

  for (k=0;k<(m-1);k++)
   if ( (nx = l[k+1]-l[k]) > THRESHOLD )  /* then use recursion */
   {
      flashsort(&a[l[k]+1],nx,CLASS_SIZE,ctr);
      (*ctr)++;
   }

   else  /* use insertion sort */
      for (i=l[k+1]-1; i > l[k] ; i--)
	  if (a[i] > a[i+1])
	  {
	     hold=a[i];
	     j=i;
	     while  (hold  >  a[j+1] )  a[j++]=a[j+1] ;
	     a[j]=hold;
	  }
  free(l);   /* need to free the memory we grabbed for the l vector */
}

int v[500002];
int crt[500002];


int main()
{
    freopen("algsort.in", "r", stdin);
    freopen("algsort.out", "w", stdout);

    int n;

    scanf("%d ", &n);

    for ( int i = 0; i < n; ++i )
            scanf("%d", &v[i]);

    flashsort(v,n,CLASS_SIZE, crt);

    for ( int i = 0; i < n; ++i )
            printf("%d ", v[i]);
}