Pagini recente » Cod sursa (job #867875) | Cod sursa (job #1086743) | Cod sursa (job #620745) | Cod sursa (job #1288128) | Cod sursa (job #281289)
Cod sursa(job #281289)
#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:
static void Merge(std::vector<T>& vect) {
}
static void Quick(std::vector<T>& vect) {
}
static void Heap(std::vector<T>& vect) {
}
static void Intro(std::vector<T>& vect) {
}
static void Bubble(std::vector<T>& vect) {
bool sorted;
typename std::vector<T>::size_type 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(std::vector<T>& vect) {
}
static void Insertion(std::vector<T>& vect) {
}
public:
typedef void (*FuncPtr)(std::vector<T>& 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>::Bubble;
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;
}