Created
March 10, 2015 11:06
-
-
Save MORTAL2000/e337d5e28e37578b2072 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <vector> | |
#include <algorithm> | |
void printVect(const std::vector<int> path) { | |
for( std::vector<int>::const_iterator i = path.begin(); i != path.end(); ++i) { | |
std::cout << *i << ' '; | |
} | |
std::cout << "\n"; | |
} | |
int moveTo(const int index, const int right) { | |
const int shift = index >> 1; | |
return ((index & 1) == 0) ? shift : (right - shift); | |
} | |
template<class T> | |
int rotate(std::vector<T>& nums, const int start, std::vector<bool>& seen) { | |
const int right = nums.size() - 1; | |
int index = start; | |
T buffer = nums[index]; | |
int count = 0; | |
do { | |
const int npos = moveTo(index, right); | |
const T tmp = nums[npos]; | |
nums[npos] = buffer; | |
buffer = tmp; | |
index = npos; | |
count++; | |
seen[npos] = true; | |
} while (index != start); | |
return count; | |
} | |
template<class T> | |
void shuffleEvenOdd(std::vector<T>& nums) { | |
const int sz = nums.size(); | |
if (sz < 3) { | |
return; | |
} | |
std::vector<bool> seen = std::vector<bool>(sz, false); | |
int count = 0; | |
for (int i = 0; i < sz && count < sz; i++) { | |
if (!seen[i]) { | |
count += rotate(nums, i, seen); | |
} | |
} | |
} | |
void prepareEntries(std::vector<int>& nums) { | |
std::sort(nums.begin(), nums.end()); | |
} | |
void test( std::vector<int>& input, const std::vector<int>& expected, const std::string & testName) | |
{ | |
std::cout << "Test case : " << testName << "\n"; | |
std::cout << "Input : "; | |
printVect(input); | |
prepareEntries(input); | |
std::cout << "Prepd : "; | |
printVect(input); | |
shuffleEvenOdd(input); | |
std::cout << "Output: "; | |
printVect(input); | |
std::cout << "Expect: "; | |
printVect(expected); | |
std::cout << (expected == input ? " correct\n" : " !!!FAILED!!!\n"); | |
std::cout<<"\n\n"; | |
} | |
void test1() | |
{ | |
std::vector<int>tmp= {2,2,2}; | |
const std::vector<int>expected= {2,2,2}; | |
const std::string tstName = " Test 1 "; | |
test(tmp,expected, tstName); | |
} | |
void test2() | |
{ | |
std::vector<int>tmp= {2}; | |
const std::vector<int>expected= {2}; | |
const std::string tstName = " Test 2 "; | |
test(tmp,expected, tstName); | |
} | |
void test3() | |
{ | |
std::vector<int>tmp= {3,3}; | |
const std::vector<int>expected= {3,3}; | |
const std::string tstName = " Test 3 "; | |
test(tmp,expected, tstName); | |
} | |
void test4() | |
{ | |
std::vector<int>tmp= {1,-8,2,0,5}; | |
const std::vector<int>expected= {-8,1,5,2,0}; | |
const std::string tstName = " Test 4 "; | |
test(tmp,expected, tstName); | |
} | |
void test5() | |
{ | |
std::vector<int>tmp= {-9,1,-8,2,0,5}; | |
const std::vector<int>expected= {-9,0,2,5,1,-8}; | |
const std::string tstName = " Test 5 "; | |
test(tmp,expected, tstName); | |
} | |
void test6() | |
{ | |
std::vector<int> tmp = std::vector<int>(100); | |
std::vector<int> expect = std::vector<int>(100); | |
for (int i = 0; i < 100; i++) { | |
tmp[i] = i; | |
int os = i >> 1; | |
expect[((i & 1) == 1) ? (99 - os) : os] = i; | |
} | |
const std::string name = " Sequentials 5"; | |
test(tmp, expect, name); | |
} | |
int main() | |
{ | |
test1(); | |
test2(); | |
test3(); | |
test4(); | |
test5(); | |
test6(); | |
return 0; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment