From 02d7fa727a5f0bf87523090376677c55e4707e12 Mon Sep 17 00:00:00 2001 From: AnupamaTK <43145867+AnupamaTK@users.noreply.github.com> Date: Wed, 12 May 2021 14:41:52 +0530 Subject: [PATCH] Create Blocksort.cpp --- Cpp/Blocksort.cpp | 48 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 Cpp/Blocksort.cpp diff --git a/Cpp/Blocksort.cpp b/Cpp/Blocksort.cpp new file mode 100644 index 0000000..75bb4a4 --- /dev/null +++ b/Cpp/Blocksort.cpp @@ -0,0 +1,48 @@ +#include +#include +#include + +class Blocksort +{ +public: + static std::string encode(const std::string &input,int &top); + static std::string decode(const std::string &input,int top); +}; + + +std::string Blocksort::encode(const std::string &input, int &top) +{ + std::string buff,result; + buff = input + input; + std::vector vectemp; + + for(int i=0; i < buff.size()/2;++i){ + std::string temp=buff.substr(i,input.size()); + vectemp.push_back(temp); + } + + std::stable_sort(vectemp.begin(),vectemp.end()); + + for(int i=0; i < vectemp.size();++i){ + if( input == vectemp[i] ) top = i; + result += *(vectemp[i].end()-1); + } + return result; +} + +std::string Blocksort::decode(const std::string &input,int top) +{ + std::string result,inputs=input; + std::vector idx(input.size()); + for (int i = 0; i != idx.size(); ++i) idx[i] = i; + std::stable_sort(idx.begin(),idx.end(), + [inputs](int i1,int i2) {return inputs[i1] < inputs[i2];}); + + int p = top; + for(int i=0;i