Pagini recente » Cod sursa (job #1056643) | Cod sursa (job #494060) | Cod sursa (job #2461657) | Cod sursa (job #172442) | Cod sursa (job #281291)
Cod sursa(job #281291)
#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::difference_type Index;
typedef typename Vector::size_type Size;
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;
Size size = vect.size();
do {
sorted = true;
for(Index 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) {
Size size = vect.size();
Index 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) {
Size size = vect.size();
for(Index i = 1; i < size; i++) {
T data = vect[i];
Index j = i-1;
while(j >= 0 && vect[j] > data) {
vect[j+1] = vect[j];
j--;
}
vect[j+1] = data;
}
}
};
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>::Insertion;
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;
}