Pagini recente » Cod sursa (job #1059970) | Cod sursa (job #1198680) | Cod sursa (job #1415699) | Cod sursa (job #1194235) | Cod sursa (job #1401267)
#include <fstream>
#include <utility>
#include <queue>
using namespace std;
ifstream in("kbubblesort.in");
ofstream out("kbubblesort.out");
unsigned v[1000000];
int main() {
unsigned n,k;
in >> n >> k;
for(unsigned i = 0; i < n; ++i) {
in >> v[i];
}
queue<unsigned> to_swap;
for(unsigned i = 1; i < n; ++i) {
if(v[i-1] > v[i]) {
to_swap.push(i);
}
}
unsigned swaps = 0;
while(swaps < k) {
unsigned c = to_swap.front();
to_swap.pop();
to_swap.push(c);
while(swaps < k && c < n && v[c-1] > v[c]) {
swap(v[c-1],v[c]);
c++;
swaps++;
}
}
for(unsigned i = 0; i < n; ++i) {
out << v[i] << ' ';
}
out << '\n';
}