#include <vector>
using namespace std;
void Swap(int& a, int& b)
{
int temp = a;
a = b;
b = temp;
}
int BSearchUpper(vector<int>& array, int low, int high, int target)
{
if (low > high || array[high] < target) return -1;
int mid = (low + high) / 2;
while(low < high)
{
if (array[mid] <= target)
low = mid + 1;
else
high = mid;
mid = (low + high) / 2;
}
return mid;
}
void ReverseArray(vector<int>& array, int low, int high)
{
while(low < high)
{
Swap(array[low], array[high]);
++ low;
-- high;
}
}
void NextPermutation(vector<int> &array)
{
int length = array.size()-1;
int i = array.size();
while(--i > 0 && array[i-1] >= array [i]);
ReverseArray(array,i,length);
if (i >= 0)
{
int upper = BSearchUpper(array,i,length, array[i-1]);
Swap(array[i-1], array[upper]);
}
}
#include <iostream>
using namespace std;
template<typename T>
void PrintVector(T v, size_t size)
{
for (size_t i = 0; i < size; ++i)
cout << v[i]<<", ";
cout << endl;
}
int main(int argc, char** argv)
{
vector<int> v;
v.push_back(3);
v.push_back(4);
v.push_back(1);
v.push_back(2);
PrintVector(v,4);
NextPermutation(v);
PrintVector(v,4);
return 0;
}