Cod sursa(job #1886892)

Utilizator thinkphpAdrian Statescu thinkphp Data 21 februarie 2017 11:09:10
Problema Sortare prin comparare Scor 40
Compilator cpp Status done
Runda Arhiva educationala Marime 0.96 kb
#include <iostream>
#include <fstream>
#include <vector>
#define loop(vec) for(const auto &number : vec)
#define FIN "algsort.in"
#define FOUT "algsort.out"

using namespace std;

template<class ItemTypeVec>
void insertionSort(vector<ItemTypeVec> &vec) {

     int i, 
         j;
 
     for(i = 1; i < vec.size(); ++i) {

         auto tmp = move(vec[ i ]);

         if( vec[ i ] > vec[ i - 1] ) continue;  
 
         for(j = i - 1; (j >= 0 && (vec[ j ] > tmp)); j--) {

             vec[ j + 1 ] = move( vec[ j ] );          
         } 

         vec[ j + 1] = tmp;
     }

}

int main() {

    vector<int> vec; 

    ifstream fin( FIN );
    ofstream fout( FOUT );

    int num,

        n; 
 
      fin>>n;

      while( fin>>num ) vec.push_back( num );   

      //call the function insertion to sort the array
      insertionSort<int>( vec ); 
                
      loop( vec ) fout<<number<<" ";  
      

    return(0);
}