Cod sursa(job #281290)

Utilizator tomescu_alinTomescu Alin tomescu_alin Data 14 martie 2009 05:05:26
Problema Sortare prin comparare Scor 40
Compilator cpp Status done
Runda Arhiva educationala Marime 2.08 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <iterator>
using namespace std;

typedef unsigned char byte;
typedef unsigned short ushort;
typedef unsigned int uint;
typedef unsigned long ulong;
typedef unsigned long long ulonglong;

template<class T>
class Sort {
	public:		
		typedef std::vector<T> Vector;
		typedef void (*FuncPtr)(Vector& vect);
		typedef typename Vector::size_type Index;
		
	public:
		static void Merge(Vector& vect) {
			
		}
		
		static void Quick(Vector& vect) {
			
		}
		
		static void Heap(Vector& vect) {
			
		}
		
		static void Intro(Vector& vect) {
			
		}
		
		static void Bubble(Vector& vect) {
			bool sorted;
			Index size = vect.size();
			
			do {
				sorted = true;
				for(int i = 0; i < size-1; i++) {
					if(vect[i] > vect[i+1]) {
						swap(vect[i], vect[i+1]);
						sorted = false;
					}
				}
			} while(!sorted);
		}
		
		static void Select(Vector& vect) {
			Index size = vect.size(), swapIndex;
			
			for(Index i = 0; i < size-1; i++) {
				swapIndex = i;
				
				for(Index j = i+1; j < size; j++) {
					if(vect[swapIndex] > vect[j]) {
						swapIndex = j;
					}
				}
				
				if(swapIndex != i) {
					swap(vect[i], vect[swapIndex]);
				}
			}
			
		}
		
		static void Insertion(Vector& vect) {
			
		}
			
};

int main(int argc, char * argv[]) {
	
	const char * inFile = "algsort.in";
	const char * outFile = "algsort.out";
	
	ifstream fin(inFile);
	ofstream fout(outFile);
	
	if(!fin || !fout) {
		cerr << "Error opening one of \"" << inFile << "\" or \"" << outFile << "\"" << endl;
		return -1;
	}
	
	Sort<ulong>::FuncPtr sorter = Sort<ulong>::Select;
	std::vector<ulong> vect;
	
	ulong n;
	fin >> n;
	vect.resize(n);
	
	for(ulong i = 0; i < n; i++) {
		fin >> vect[i];
	}
	
	
	//	Sort the vector
	sorter(vect);
	
	//	Store the sorted vector in a file
	ostream& target = fout;	
	copy(vect.begin(), vect.end(), ostream_iterator<ulong>(target, " "));
	target << endl;
	
	fout.close();
	fin.close();
	
	return 0;

}