Pagini recente » Cod sursa (job #1099778) | Cod sursa (job #2777924) | Cod sursa (job #1248953) | Cod sursa (job #2731710) | Cod sursa (job #2512093)
#include <algorithm>
#include <iostream>
#include <iterator>
#include <vector>
#include <fstream>
using namespace std;
template <typename BidIt, typename Pred>
void pancake_sort(BidIt first, BidIt last, Pred order)
{
if (distance(first, last) < 2) return; // no sort needed
for (; first != last; --last)
{
BidIt mid = max_element(first, last, order);
if (mid == last - 1)
{
continue; // no flips needed
}
if (first != mid)
{
reverse(first, mid + 1); // flip element to front
}
reverse(first, last); // flip front to final position
}
}
// pancake sort template (ascending order)
template <typename BidIt>
void pancake_sort(BidIt first, BidIt last)
{
pancake_sort(first, last,less<typename std::iterator_traits<BidIt>::value_type>());
}
int main()
{
ifstream fin("algsort.in");
ofstream fout("algsort.out");
vector<int> data;
int n,i,j;
fin>>n;
for(i=1;i<=n;i++){
fin>>j;
data.push_back(j);
}
pancake_sort(data.begin(), data.end()); // ascending pancake sort
copy(data.begin(), data.end(), ostream_iterator<int>(fout, " "));
}