Problem Statement:
http://www.topcoder.com/stat?c=problem_statement&pm=6413&rd=9986
My solution:
#include <iostream>
#include <vector>
using namespace std;
class SortMachine {
public:
SortMachine () {}
public:
int countMoves(vector <int> a) {
int countMove = 0;
while(1) {
int move = -1;
int minInversion = 1001;
for (unsigned int i = 0; i < a.size() - 1; ++i) {
for (unsigned int j = i + 1; j < a.size(); ++j) {
if (a[i] > a[j] && a[i] < minInversion) {
move = i;
minInversion = a[i];
break;
}
}
}
if (move == -1) {
break;
}
int temp = a[move];
for (unsigned int i = move; i < a.size() - 1; ++i) {
a[i] = a[i + 1];
}
a[a.size() - 1] = temp;
++countMove;
}
return countMove;
}
};
My solution:
#include <iostream>
#include <vector>
using namespace std;
class SortMachine {
public:
SortMachine () {}
public:
int countMoves(vector <int> a) {
int countMove = 0;
while(1) {
int move = -1;
int minInversion = 1001;
for (unsigned int i = 0; i < a.size() - 1; ++i) {
for (unsigned int j = i + 1; j < a.size(); ++j) {
if (a[i] > a[j] && a[i] < minInversion) {
move = i;
minInversion = a[i];
break;
}
}
}
if (move == -1) {
break;
}
int temp = a[move];
for (unsigned int i = move; i < a.size() - 1; ++i) {
a[i] = a[i + 1];
}
a[a.size() - 1] = temp;
++countMove;
}
return countMove;
}
};