Cod sursa(job #1576852)

Utilizator thinkphpAdrian Statescu thinkphp Data 22 ianuarie 2016 21:57:36
Problema Sortare prin comparare Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.77 kb
/**
 * ShellSort in action.
 */
#include <iostream>
#include <fstream>
#include <vector>
#define push push_back
#define FIN "algsort.in"
#define FOUT "algsort.out"

using namespace std;

class Container {

      public:
      Container(int _n): n( _n ){};

      ~Container(){}; 

      void added(int elem) {

           vec.push( elem );  
      };

      void sort() {

           _shellsort();  
      };

      friend ostream &operator << (ostream &stream, const Container &ob ) {

             vector<int> vec = ob.vec; 

             for(vector<int>::iterator it = vec.begin(); it != vec.end(); ++it)                
            
                 stream<<*it<<" ";

             return stream;
      }; 

      private:
      vector<int> vec;
      int n;

      void _shellsort() {

           int gap = n - 1,

               g,

               i,

               j, temp;

               for(g = gap/2; g > 0; g /= 2 ) {

                   for(i = g; i < n; i++) {

                       temp = vec[ i ];

                       for(j = i; j >= g; j -= g) {

                           if( vec[ j - g ] > temp )

                               vec[ j ] = vec[ j - g ];

                           else 

                               break;
                       } 

                       vec[ j ] = temp;
                   }
               }  
      };
};

int main() {

    int i,

        n;

    std::ifstream fin( FIN );

    std::ofstream fout( FOUT );

    int elem;

    fin>>n; 

    Container cont( n );

    while( n-- ) {

           fin>>elem;

           cont.added( elem );  
    } 

    cont.sort();

    fout<<cont;

    fin.close();

    fout.close();

    return(0);
};