From 43cb4ad4eb009ac5ac0624f35fe5a0b76bdf9d43 Mon Sep 17 00:00:00 2001 From: Riot Date: Tue, 5 Nov 2019 01:22:27 +0100 Subject: [PATCH 01/20] playfair --- Others/PlayfairCipher.cpp | 83 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 Others/PlayfairCipher.cpp diff --git a/Others/PlayfairCipher.cpp b/Others/PlayfairCipher.cpp new file mode 100644 index 000000000..fdb327cfc --- /dev/null +++ b/Others/PlayfairCipher.cpp @@ -0,0 +1,83 @@ +#include +#include +#include +#define ENGLISH_ABC "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + +using namespace std; + +inline pair getCoordinate(char*code_table,const char character) { + for (uint8_t x(0); x < 5;x++) { + for (uint8_t y(0); y < 5;y++) { + if (*(code_table + (x * 5) + y) == character) + return make_pair(x, y); + } + } +} +string playfair(string str, string keyword) { + + char code_table[5][5]; + int32_t keyword_index(-1); + + string::iterator str_iter = str.begin(), keywor_iter = keyword.begin(); + + while (str_iter != str.end() || keywor_iter != keyword.end()) + { + if (str_iter != str.end()) { + if ((*str_iter = toupper(*str_iter)) >= 'A' && *str_iter <= 'Z') + str_iter++; + else str.erase(str_iter); + } + if (keywor_iter != keyword.end()) { + if ((*keywor_iter = toupper(*keywor_iter)) >= 'A' && *keywor_iter <= 'Z') + keywor_iter++; + else keyword.erase(keywor_iter); + } + } + if (str.length() == 0)return str; + + keyword.append(ENGLISH_ABC); + for (uint8_t x(0); x < 5;x++) { + for (uint8_t y(0); y < 5;y++) { + while (true) + { + if (keyword.find_first_of(keyword.at(++keyword_index)) == keyword_index){ + code_table[x][y] = keyword.at(keyword_index); + break; + } + } + } + } + + for (string::iterator iter = str.begin() + 1; iter != str.end(); iter+= iter == str.end() - 1 ? 1 : 2) { + if (*(iter - 1) == *iter) + str.insert(iter, 'x'); + } + if (str.length() % 2 != 0) str.append("x"); + + for (string::iterator iter = str.begin(); iter != str.end(); iter += 2) { + pair, pair> character_pair_coordinate; + character_pair_coordinate.first = getCoordinate(&code_table[0][0], *iter); + character_pair_coordinate.second = getCoordinate(&code_table[0][0], *(iter + 1)); + if (character_pair_coordinate.first.first == character_pair_coordinate.second.first) // x1 == x2 + { + *iter = (code_table[character_pair_coordinate.first.first][(character_pair_coordinate.first.second + 1) % 5]); + *(iter + 1) = (code_table[character_pair_coordinate.second.first][(character_pair_coordinate.second.second + 1) % 5]); + } + else if (character_pair_coordinate.first.second == character_pair_coordinate.second.second) //y1 == y2 + { + *iter = (code_table[(character_pair_coordinate.first.first + 1) % 5][character_pair_coordinate.first.second]); + *(iter + 1) = (code_table[(character_pair_coordinate.second.first + 1) % 5][character_pair_coordinate.second.second]); + } + else + { + *iter = (code_table[character_pair_coordinate.first.first ][character_pair_coordinate.second.second]); + *(iter + 1) = (code_table[character_pair_coordinate.second.first][character_pair_coordinate.first.second]); + } + } + + return str; +} +int main(){ + playfair("Welcome from Hungary!","playfair"); //return the encrypted text + return 0; +} From d36e3f49ba6e37e4f262389fd260ae61d4f22665 Mon Sep 17 00:00:00 2001 From: Riot Date: Fri, 15 Nov 2019 20:27:37 +0100 Subject: [PATCH 02/20] matrix rotation --- .../matrix_layer_rotation.cpp | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 Operations on Datastructures/matrix_layer_rotation.cpp diff --git a/Operations on Datastructures/matrix_layer_rotation.cpp b/Operations on Datastructures/matrix_layer_rotation.cpp new file mode 100644 index 000000000..1056b2523 --- /dev/null +++ b/Operations on Datastructures/matrix_layer_rotation.cpp @@ -0,0 +1,52 @@ +#include +#include +using namespace std; +template inline void do_rotate(Type* rotated_matrix, const pair& position, const unsigned &row, const unsigned &columm, const uint8_t inside_row, const uint8_t inside_columm, const uint8_t rotation_position, const Type* const element) { + int*rotated_matrix_walker = (rotated_matrix + position.first * columm + (position.second)); + int offset = 1, element_index = -1; + while (true) + { + if (++element_index == rotation_position) { + *rotated_matrix_walker = *element; + return; + } + rotated_matrix_walker += offset; + if (rotated_matrix + ((position.first * columm) + position.second + inside_columm - 1) == rotated_matrix_walker) offset = columm; + else if (rotated_matrix + ((position.first * columm) + position.second + ((inside_row - 1) * columm) + inside_columm - 1) == rotated_matrix_walker) offset = -1; + else if (rotated_matrix + ((position.first * columm) + position.second + ((inside_row - 1) * columm)) == rotated_matrix_walker) offset = -1 * columm; + } +} +template Type* matrix_rotation(Type * const matrix, const unsigned row, const unsigned column, const unsigned rotation) { + pair position = make_pair(0, 0); + Type *rotated_matrix = new Type[row * column]; + int offset = 1; + int *matrix_walker = matrix; + uint8_t inside_row = row, inside_column = column; + int32_t array_lenght(0), element_index(0); + while (inside_row > 0 && inside_column > 0) + { + array_lenght = (2 * inside_column + ((inside_row - 2) * 2)); + do + { + do_rotate(rotated_matrix, position, row, column, inside_row, inside_column, (rotation + element_index) % array_lenght, matrix_walker); + matrix_walker += offset; + element_index++; + if (matrix + ((position.first * column) + position.second + inside_column - 1) == matrix_walker) offset = column; + else if (matrix + ((position.first * column) + position.second + ((inside_row - 1) * column) + inside_column - 1) == matrix_walker) offset = -1; + else if (matrix + ((position.first * column) + position.second + ((inside_row - 1) * column)) == matrix_walker) offset = -1 * column; + } while ((matrix + (position.first * column + position.second)) != matrix_walker); + position.first++; + position.second++; + inside_row -= 2; + inside_column -= 2; + offset = 1; + matrix_walker = matrix + (position.first * column) + (position.second); + } + return rotated_matrix; +} +int main() +{ + int *matrix = new int[16]{7,20,31,90,9,10,40,8,50,2,70,10,10,20,25,49}; + int*rotated_matrix = matrix_rotation(matrix,4,4,1); + return 0; +} From e7a7ac32fce0166286a8ae6f9be07d09cce1eef3 Mon Sep 17 00:00:00 2001 From: Riot Date: Fri, 29 Nov 2019 01:25:28 +0100 Subject: [PATCH 03/20] struzik --- Search/exponential_search.cpp | 56 +++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 Search/exponential_search.cpp diff --git a/Search/exponential_search.cpp b/Search/exponential_search.cpp new file mode 100644 index 000000000..b0f1d5aa3 --- /dev/null +++ b/Search/exponential_search.cpp @@ -0,0 +1,56 @@ +#include +#include +#include +using namespaces std; + +//-----------------Binary Search Algorithm(use by struziki algorithm//----------------- +//Time Complexity O(log n) where 'n' is the number of elements +//Worst Time Complexity O(log n) +//Best Time Complexity Ω(1) +//Space Complexity O(1) +//Auxiliary Space Complexity O(1) + +template inline Type* binary_search(Type *array, size_t size, Type key) {//Parameter List:Pointer to and array|size of array|key what you search + int32_t lower_index(0), upper_index(size - 1),middle_index; //lower_index => start of search range | upper_index => end of search range | middle_index => middle of search range + while (lower_index <= upper_index) + { + middle_index = floor((lower_index + upper_index) / 2); + if (*(array + middle_index) < key) lower_index = (middle_index + 1); //if the key is smaller than the middle of search range, we narrow the search range from up + else if (*(array + middle_index) > key) upper_index = (middle_index - 1);//if the key is bigger than the middle of search range, we narrow the search range from down + else return (array + middle_index); //the key has been found + } + return nullptr; +} + +//-----------------Struzik Search Algorithm(Exponential//----------------- +//Time Complexity O(log i)where i is the position of the search key in the list +//Worst Time Complexity O(log i) +//Best Time Complexity Ω(1) +//Space Complexity O(1) +//Auxiliary Space Complexity O(1) + +template Type* Struzik_Search(Type* array,size_t size,Type key) { //Parameter List: Pointer to an array(sorted)!You can use complex objectum, but in that case you have to overload '<>' operators!|size of array|key what you search + uint32_t block_front(0),block_size = size == 0 ? 0 : 1; //the start and end of the first block where the algorithm starts seach !if the size of array 0 than return null pointer + while (block_front != block_size) //when the start of block(block_front) and end of block(block_size) equal it means the key bigger than the last element of array and it return null pointer + { + if (*(array + block_size - 1) < key) {//if the key is bigger than the end of block we define a new block what is twice bigger than the previous + block_front = block_size; + (block_size * 2 - 1 < size) ? (block_size *= 2) : block_size = size;//if the end of new block bigger than size of array it takes the end of array + continue; + } + //when the algorithm delimit the block where the key shold be we do a binary search there + return binary_search(array + block_front, (block_size - block_front), key); + } + return nullptr; +} +int main(){ + + //----------------TEST CASES---------------- + int *sorted_array = new int[7]{ 7,10,15,23,70,105,203 }; + assert(Struzik_Search(sorted_array, 7, 0) == nullptr); //Key smaller than the first element of array + assert(Struzik_Search(sorted_array, 7, 1000) == nullptr); //Key bigger than the last element of array + assert(Struzik_Search(sorted_array, 7, 50) == nullptr); //Key between the elemenets of array + assert(Struzik_Search(sorted_array, 7, 7) == sorted_array);//Key is in the array !FOUND! + //----------------TEST CASES---------------- + return EXIT_SUCCESS; +} From 0c41877301e915eef04f945b5c1589896526cb57 Mon Sep 17 00:00:00 2001 From: Divide-et-impera-11 <54957167+Divide-et-impera-11@users.noreply.github.com> Date: Fri, 29 Nov 2019 02:48:14 +0100 Subject: [PATCH 04/20] Delete PlayfairCipher.cpp --- Others/PlayfairCipher.cpp | 83 --------------------------------------- 1 file changed, 83 deletions(-) delete mode 100644 Others/PlayfairCipher.cpp diff --git a/Others/PlayfairCipher.cpp b/Others/PlayfairCipher.cpp deleted file mode 100644 index fdb327cfc..000000000 --- a/Others/PlayfairCipher.cpp +++ /dev/null @@ -1,83 +0,0 @@ -#include -#include -#include -#define ENGLISH_ABC "ABCDEFGHIJKLMNOPQRSTUVWXYZ" - -using namespace std; - -inline pair getCoordinate(char*code_table,const char character) { - for (uint8_t x(0); x < 5;x++) { - for (uint8_t y(0); y < 5;y++) { - if (*(code_table + (x * 5) + y) == character) - return make_pair(x, y); - } - } -} -string playfair(string str, string keyword) { - - char code_table[5][5]; - int32_t keyword_index(-1); - - string::iterator str_iter = str.begin(), keywor_iter = keyword.begin(); - - while (str_iter != str.end() || keywor_iter != keyword.end()) - { - if (str_iter != str.end()) { - if ((*str_iter = toupper(*str_iter)) >= 'A' && *str_iter <= 'Z') - str_iter++; - else str.erase(str_iter); - } - if (keywor_iter != keyword.end()) { - if ((*keywor_iter = toupper(*keywor_iter)) >= 'A' && *keywor_iter <= 'Z') - keywor_iter++; - else keyword.erase(keywor_iter); - } - } - if (str.length() == 0)return str; - - keyword.append(ENGLISH_ABC); - for (uint8_t x(0); x < 5;x++) { - for (uint8_t y(0); y < 5;y++) { - while (true) - { - if (keyword.find_first_of(keyword.at(++keyword_index)) == keyword_index){ - code_table[x][y] = keyword.at(keyword_index); - break; - } - } - } - } - - for (string::iterator iter = str.begin() + 1; iter != str.end(); iter+= iter == str.end() - 1 ? 1 : 2) { - if (*(iter - 1) == *iter) - str.insert(iter, 'x'); - } - if (str.length() % 2 != 0) str.append("x"); - - for (string::iterator iter = str.begin(); iter != str.end(); iter += 2) { - pair, pair> character_pair_coordinate; - character_pair_coordinate.first = getCoordinate(&code_table[0][0], *iter); - character_pair_coordinate.second = getCoordinate(&code_table[0][0], *(iter + 1)); - if (character_pair_coordinate.first.first == character_pair_coordinate.second.first) // x1 == x2 - { - *iter = (code_table[character_pair_coordinate.first.first][(character_pair_coordinate.first.second + 1) % 5]); - *(iter + 1) = (code_table[character_pair_coordinate.second.first][(character_pair_coordinate.second.second + 1) % 5]); - } - else if (character_pair_coordinate.first.second == character_pair_coordinate.second.second) //y1 == y2 - { - *iter = (code_table[(character_pair_coordinate.first.first + 1) % 5][character_pair_coordinate.first.second]); - *(iter + 1) = (code_table[(character_pair_coordinate.second.first + 1) % 5][character_pair_coordinate.second.second]); - } - else - { - *iter = (code_table[character_pair_coordinate.first.first ][character_pair_coordinate.second.second]); - *(iter + 1) = (code_table[character_pair_coordinate.second.first][character_pair_coordinate.first.second]); - } - } - - return str; -} -int main(){ - playfair("Welcome from Hungary!","playfair"); //return the encrypted text - return 0; -} From 969652b422a3f2c13438118b056f4a579373330f Mon Sep 17 00:00:00 2001 From: Divide-et-impera-11 <54957167+Divide-et-impera-11@users.noreply.github.com> Date: Fri, 29 Nov 2019 02:48:37 +0100 Subject: [PATCH 05/20] Delete matrix_layer_rotation.cpp --- .../matrix_layer_rotation.cpp | 52 ------------------- 1 file changed, 52 deletions(-) delete mode 100644 Operations on Datastructures/matrix_layer_rotation.cpp diff --git a/Operations on Datastructures/matrix_layer_rotation.cpp b/Operations on Datastructures/matrix_layer_rotation.cpp deleted file mode 100644 index 1056b2523..000000000 --- a/Operations on Datastructures/matrix_layer_rotation.cpp +++ /dev/null @@ -1,52 +0,0 @@ -#include -#include -using namespace std; -template inline void do_rotate(Type* rotated_matrix, const pair& position, const unsigned &row, const unsigned &columm, const uint8_t inside_row, const uint8_t inside_columm, const uint8_t rotation_position, const Type* const element) { - int*rotated_matrix_walker = (rotated_matrix + position.first * columm + (position.second)); - int offset = 1, element_index = -1; - while (true) - { - if (++element_index == rotation_position) { - *rotated_matrix_walker = *element; - return; - } - rotated_matrix_walker += offset; - if (rotated_matrix + ((position.first * columm) + position.second + inside_columm - 1) == rotated_matrix_walker) offset = columm; - else if (rotated_matrix + ((position.first * columm) + position.second + ((inside_row - 1) * columm) + inside_columm - 1) == rotated_matrix_walker) offset = -1; - else if (rotated_matrix + ((position.first * columm) + position.second + ((inside_row - 1) * columm)) == rotated_matrix_walker) offset = -1 * columm; - } -} -template Type* matrix_rotation(Type * const matrix, const unsigned row, const unsigned column, const unsigned rotation) { - pair position = make_pair(0, 0); - Type *rotated_matrix = new Type[row * column]; - int offset = 1; - int *matrix_walker = matrix; - uint8_t inside_row = row, inside_column = column; - int32_t array_lenght(0), element_index(0); - while (inside_row > 0 && inside_column > 0) - { - array_lenght = (2 * inside_column + ((inside_row - 2) * 2)); - do - { - do_rotate(rotated_matrix, position, row, column, inside_row, inside_column, (rotation + element_index) % array_lenght, matrix_walker); - matrix_walker += offset; - element_index++; - if (matrix + ((position.first * column) + position.second + inside_column - 1) == matrix_walker) offset = column; - else if (matrix + ((position.first * column) + position.second + ((inside_row - 1) * column) + inside_column - 1) == matrix_walker) offset = -1; - else if (matrix + ((position.first * column) + position.second + ((inside_row - 1) * column)) == matrix_walker) offset = -1 * column; - } while ((matrix + (position.first * column + position.second)) != matrix_walker); - position.first++; - position.second++; - inside_row -= 2; - inside_column -= 2; - offset = 1; - matrix_walker = matrix + (position.first * column) + (position.second); - } - return rotated_matrix; -} -int main() -{ - int *matrix = new int[16]{7,20,31,90,9,10,40,8,50,2,70,10,10,20,25,49}; - int*rotated_matrix = matrix_rotation(matrix,4,4,1); - return 0; -} From 895010d8a9f568789a19f32036c61e17702a4165 Mon Sep 17 00:00:00 2001 From: Divide-et-impera-11 <54957167+Divide-et-impera-11@users.noreply.github.com> Date: Fri, 29 Nov 2019 03:02:08 +0100 Subject: [PATCH 06/20] Update exponential_search.cpp --- Search/exponential_search.cpp | 60 ++++++++++++++++------------------- 1 file changed, 27 insertions(+), 33 deletions(-) diff --git a/Search/exponential_search.cpp b/Search/exponential_search.cpp index b0f1d5aa3..7a0349917 100644 --- a/Search/exponential_search.cpp +++ b/Search/exponential_search.cpp @@ -2,55 +2,49 @@ #include #include using namespaces std; - -//-----------------Binary Search Algorithm(use by struziki algorithm//----------------- -//Time Complexity O(log n) where 'n' is the number of elements -//Worst Time Complexity O(log n) -//Best Time Complexity Ω(1) -//Space Complexity O(1) -//Auxiliary Space Complexity O(1) - -template inline Type* binary_search(Type *array, size_t size, Type key) {//Parameter List:Pointer to and array|size of array|key what you search - int32_t lower_index(0), upper_index(size - 1),middle_index; //lower_index => start of search range | upper_index => end of search range | middle_index => middle of search range +//-----------------Binary Search Algorithm(use by Struzik algorithm)----------------- +// Time Complexity O(log n) where 'n' is the number of elements +// Worst Time Complexity O(log n) +// Best Time Complexity O(1) +// Space Complexity O(1) +// Auxiliary Space Complexity O(1) +template inline Type* binary_search(Type *array, size_t size, Type key) {//Parameter List:Pointer to an array|size of array|key what you search + int32_t lower_index(0), upper_index(size - 1),middle_index; //lower_index => start of search range|upper_index => end of search range while (lower_index <= upper_index) { middle_index = floor((lower_index + upper_index) / 2); - if (*(array + middle_index) < key) lower_index = (middle_index + 1); //if the key is smaller than the middle of search range, we narrow the search range from up - else if (*(array + middle_index) > key) upper_index = (middle_index - 1);//if the key is bigger than the middle of search range, we narrow the search range from down - else return (array + middle_index); //the key has been found + if (*(array + middle_index) < key) lower_index = (middle_index + 1); //narrow the search range from up + else if (*(array + middle_index) > key) upper_index = (middle_index - 1);//narrow the search range from down + else return (array + middle_index); //key has been found } return nullptr; } - -//-----------------Struzik Search Algorithm(Exponential//----------------- -//Time Complexity O(log i)where i is the position of the search key in the list -//Worst Time Complexity O(log i) -//Best Time Complexity Ω(1) -//Space Complexity O(1) -//Auxiliary Space Complexity O(1) - -template Type* Struzik_Search(Type* array,size_t size,Type key) { //Parameter List: Pointer to an array(sorted)!You can use complex objectum, but in that case you have to overload '<>' operators!|size of array|key what you search - uint32_t block_front(0),block_size = size == 0 ? 0 : 1; //the start and end of the first block where the algorithm starts seach !if the size of array 0 than return null pointer - while (block_front != block_size) //when the start of block(block_front) and end of block(block_size) equal it means the key bigger than the last element of array and it return null pointer +//-----------------Struzik Search Algorithm(Exponential)----------------- +// Time Complexity O(log i)where i is the position of the search key in the list +// Worst Time Complexity O(log i) +// Best Time Complexity O(1) +// Space Complexity O(1) +// Auxiliary Space Complexity O(1) +template Type* Struzik_Search(Type* array,size_t size,Type key) { // Parameter List:Pointer to an array|size of array|key what you search + uint32_t block_front(0),block_size = size == 0 ? 0 : 1; //block_front => start of search range|block_size => end of search range + while (block_front != block_size) //if key bigger than last element itt will be equal and return nullptr { if (*(array + block_size - 1) < key) {//if the key is bigger than the end of block we define a new block what is twice bigger than the previous block_front = block_size; (block_size * 2 - 1 < size) ? (block_size *= 2) : block_size = size;//if the end of new block bigger than size of array it takes the end of array continue; } - //when the algorithm delimit the block where the key shold be we do a binary search there - return binary_search(array + block_front, (block_size - block_front), key); + return binary_search(array + block_front, (block_size - block_front), key);//if delimit the block where the key shold be,do binary search } return nullptr; } int main(){ - - //----------------TEST CASES---------------- + // ----------------TEST CASES---------------- int *sorted_array = new int[7]{ 7,10,15,23,70,105,203 }; - assert(Struzik_Search(sorted_array, 7, 0) == nullptr); //Key smaller than the first element of array - assert(Struzik_Search(sorted_array, 7, 1000) == nullptr); //Key bigger than the last element of array - assert(Struzik_Search(sorted_array, 7, 50) == nullptr); //Key between the elemenets of array - assert(Struzik_Search(sorted_array, 7, 7) == sorted_array);//Key is in the array !FOUND! - //----------------TEST CASES---------------- + assert(Struzik_Search(sorted_array, 7, 0) == nullptr);// Key smaller than the first element of array + assert(Struzik_Search(sorted_array, 7, 1000) == nullptr);// Key bigger than the last element of array + assert(Struzik_Search(sorted_array, 7, 50) == nullptr);// Key between the elemenets of array + assert(Struzik_Search(sorted_array, 7, 7) == sorted_array);// Key is in the array !FOUND! + // ----------------TEST CASES---------------- return EXIT_SUCCESS; } From 51c26b85fe1f75e3ca1cd2e756dadf1fc0bf01a6 Mon Sep 17 00:00:00 2001 From: Divide-et-impera-11 <54957167+Divide-et-impera-11@users.noreply.github.com> Date: Sat, 30 Nov 2019 19:58:22 +0100 Subject: [PATCH 07/20] Update exponential_search.cpp --- Search/exponential_search.cpp | 45 +++++++++++++---------------------- 1 file changed, 16 insertions(+), 29 deletions(-) diff --git a/Search/exponential_search.cpp b/Search/exponential_search.cpp index 7a0349917..243c28c8b 100644 --- a/Search/exponential_search.cpp +++ b/Search/exponential_search.cpp @@ -1,50 +1,37 @@ +// Copyright 2020 Divide-et-impera-11 #include #include #include using namespaces std; -//-----------------Binary Search Algorithm(use by Struzik algorithm)----------------- -// Time Complexity O(log n) where 'n' is the number of elements -// Worst Time Complexity O(log n) -// Best Time Complexity O(1) -// Space Complexity O(1) -// Auxiliary Space Complexity O(1) -template inline Type* binary_search(Type *array, size_t size, Type key) {//Parameter List:Pointer to an array|size of array|key what you search - int32_t lower_index(0), upper_index(size - 1),middle_index; //lower_index => start of search range|upper_index => end of search range +template inline Type* binary_search(Type *array, size_t size, Type key){ + int32_t lower_index(0), upper_index(size - 1),middle_index; while (lower_index <= upper_index) { middle_index = floor((lower_index + upper_index) / 2); - if (*(array + middle_index) < key) lower_index = (middle_index + 1); //narrow the search range from up - else if (*(array + middle_index) > key) upper_index = (middle_index - 1);//narrow the search range from down - else return (array + middle_index); //key has been found + if (*(array + middle_index) < key) lower_index = (middle_index + 1); + else if (*(array + middle_index) > key) upper_index = (middle_index - 1); + else return (array + middle_index); } return nullptr; } -//-----------------Struzik Search Algorithm(Exponential)----------------- -// Time Complexity O(log i)where i is the position of the search key in the list -// Worst Time Complexity O(log i) -// Best Time Complexity O(1) -// Space Complexity O(1) -// Auxiliary Space Complexity O(1) -template Type* Struzik_Search(Type* array,size_t size,Type key) { // Parameter List:Pointer to an array|size of array|key what you search - uint32_t block_front(0),block_size = size == 0 ? 0 : 1; //block_front => start of search range|block_size => end of search range - while (block_front != block_size) //if key bigger than last element itt will be equal and return nullptr +template Type* Struzik_Search(Type* array,size_t size,Type key) { + uint32_t block_front(0),block_size = size == 0 ? 0 : 1; + while (block_front != block_size) { - if (*(array + block_size - 1) < key) {//if the key is bigger than the end of block we define a new block what is twice bigger than the previous + if (*(array + block_size - 1) < key) { block_front = block_size; - (block_size * 2 - 1 < size) ? (block_size *= 2) : block_size = size;//if the end of new block bigger than size of array it takes the end of array + (block_size * 2 - 1 < size) ? (block_size *= 2) : block_size = size; continue; } - return binary_search(array + block_front, (block_size - block_front), key);//if delimit the block where the key shold be,do binary search + return binary_search(array + block_front, (block_size - block_front), key); } return nullptr; } int main(){ - // ----------------TEST CASES---------------- int *sorted_array = new int[7]{ 7,10,15,23,70,105,203 }; - assert(Struzik_Search(sorted_array, 7, 0) == nullptr);// Key smaller than the first element of array - assert(Struzik_Search(sorted_array, 7, 1000) == nullptr);// Key bigger than the last element of array - assert(Struzik_Search(sorted_array, 7, 50) == nullptr);// Key between the elemenets of array - assert(Struzik_Search(sorted_array, 7, 7) == sorted_array);// Key is in the array !FOUND! - // ----------------TEST CASES---------------- + assert(Struzik_Search(sorted_array, 7, 0) == nullptr); + assert(Struzik_Search(sorted_array, 7, 1000) == nullptr); + assert(Struzik_Search(sorted_array, 7, 50) == nullptr); + assert(Struzik_Search(sorted_array, 7, 7) == sorted_array); return EXIT_SUCCESS; } From 36a3999f3edb82f6c62ab5f295fb27625b2a9b39 Mon Sep 17 00:00:00 2001 From: Divide-et-impera-11 <54957167+Divide-et-impera-11@users.noreply.github.com> Date: Sat, 30 Nov 2019 21:01:52 +0100 Subject: [PATCH 08/20] Update exponential_search.cpp --- Search/exponential_search.cpp | 44 +++++++++++++++++------------------ 1 file changed, 21 insertions(+), 23 deletions(-) diff --git a/Search/exponential_search.cpp b/Search/exponential_search.cpp index 243c28c8b..06f9ebf51 100644 --- a/Search/exponential_search.cpp +++ b/Search/exponential_search.cpp @@ -3,32 +3,30 @@ #include #include using namespaces std; -template inline Type* binary_search(Type *array, size_t size, Type key){ - int32_t lower_index(0), upper_index(size - 1),middle_index; - while (lower_index <= upper_index) - { - middle_index = floor((lower_index + upper_index) / 2); - if (*(array + middle_index) < key) lower_index = (middle_index + 1); - else if (*(array + middle_index) > key) upper_index = (middle_index - 1); - else return (array + middle_index); - } - return nullptr; +template inline Type* binary_s(Type *array, size_t size, Type key) { + int32_t lower_index(0), upper_index(size - 1), middle_index; + while (lower_index <= upper_index) { + middle_index = floor((lower_index + upper_index) / 2); + if (*(array + middle_index) < key) lower_index = (middle_index + 1); + else if (*(array + middle_index) > key)upper_index = (middle_index - 1); + else return (array + middle_index); + } + return nullptr; } -template Type* Struzik_Search(Type* array,size_t size,Type key) { - uint32_t block_front(0),block_size = size == 0 ? 0 : 1; - while (block_front != block_size) - { - if (*(array + block_size - 1) < key) { - block_front = block_size; - (block_size * 2 - 1 < size) ? (block_size *= 2) : block_size = size; - continue; - } - return binary_search(array + block_front, (block_size - block_front), key); +template Type* Struzik_Search(Type* array, size_t size, Type key) { + uint32_t block_front(0), block_size = size == 0 ? 0 : 1; + while (block_front != block_size) { + if (*(array + block_size - 1) < key) { + block_front = block_size; + (block_size * 2 - 1 < size) ? (block_size *= 2) : block_size = size; + continue; } - return nullptr; + return binary_s(array + block_front, (block_size - block_front), key); + } + return nullptr; } -int main(){ - int *sorted_array = new int[7]{ 7,10,15,23,70,105,203 }; +int main() { + int *sorted_array = new int[7]{7, 10, 15, 23, 70, 105, 203}; assert(Struzik_Search(sorted_array, 7, 0) == nullptr); assert(Struzik_Search(sorted_array, 7, 1000) == nullptr); assert(Struzik_Search(sorted_array, 7, 50) == nullptr); From 38fb290b88c0a6454237af6523b1a2d1bc6fe9e8 Mon Sep 17 00:00:00 2001 From: Divide-et-impera-11 <54957167+Divide-et-impera-11@users.noreply.github.com> Date: Sat, 30 Nov 2019 21:13:17 +0100 Subject: [PATCH 09/20] Update exponential_search.cpp --- Search/exponential_search.cpp | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/Search/exponential_search.cpp b/Search/exponential_search.cpp index 06f9ebf51..13ed28d6f 100644 --- a/Search/exponential_search.cpp +++ b/Search/exponential_search.cpp @@ -1,17 +1,17 @@ // Copyright 2020 Divide-et-impera-11 +#include #include #include -#include using namespaces std; template inline Type* binary_s(Type *array, size_t size, Type key) { - int32_t lower_index(0), upper_index(size - 1), middle_index; - while (lower_index <= upper_index) { - middle_index = floor((lower_index + upper_index) / 2); - if (*(array + middle_index) < key) lower_index = (middle_index + 1); - else if (*(array + middle_index) > key)upper_index = (middle_index - 1); - else return (array + middle_index); - } - return nullptr; +int32_t lower_index(0), upper_index(size - 1), middle_index; +while (lower_index <= upper_index) { + middle_index = floor((lower_index + upper_index) / 2); + if (*(array + middle_index) < key) lower_index = (middle_index + 1); + else if (*(array + middle_index) > key)upper_index = (middle_index - 1); + else return (array + middle_index); + } +return nullptr; } template Type* Struzik_Search(Type* array, size_t size, Type key) { uint32_t block_front(0), block_size = size == 0 ? 0 : 1; @@ -23,13 +23,13 @@ template Type* Struzik_Search(Type* array, size_t size, Type key) { } return binary_s(array + block_front, (block_size - block_front), key); } - return nullptr; +return nullptr; } int main() { - int *sorted_array = new int[7]{7, 10, 15, 23, 70, 105, 203}; - assert(Struzik_Search(sorted_array, 7, 0) == nullptr); - assert(Struzik_Search(sorted_array, 7, 1000) == nullptr); - assert(Struzik_Search(sorted_array, 7, 50) == nullptr); - assert(Struzik_Search(sorted_array, 7, 7) == sorted_array); - return EXIT_SUCCESS; + int *sorted_array = new int[7]{7, 10, 15, 23, 70, 105, 203}; + assert(Struzik_Search(sorted_array, 7, 0) == nullptr); + assert(Struzik_Search(sorted_array, 7, 1000) == nullptr); + assert(Struzik_Search(sorted_array, 7, 50) == nullptr); + assert(Struzik_Search(sorted_array, 7, 7) == sorted_array); +return EXIT_SUCCESS; } From f197fa38e3bcde5782291c7210b403cba84b11fc Mon Sep 17 00:00:00 2001 From: Divide-et-impera-11 <54957167+Divide-et-impera-11@users.noreply.github.com> Date: Sat, 30 Nov 2019 21:15:03 +0100 Subject: [PATCH 10/20] Update exponential_search.cpp --- Search/exponential_search.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Search/exponential_search.cpp b/Search/exponential_search.cpp index 13ed28d6f..90ce27409 100644 --- a/Search/exponential_search.cpp +++ b/Search/exponential_search.cpp @@ -26,10 +26,10 @@ template Type* Struzik_Search(Type* array, size_t size, Type key) { return nullptr; } int main() { - int *sorted_array = new int[7]{7, 10, 15, 23, 70, 105, 203}; - assert(Struzik_Search(sorted_array, 7, 0) == nullptr); - assert(Struzik_Search(sorted_array, 7, 1000) == nullptr); - assert(Struzik_Search(sorted_array, 7, 50) == nullptr); - assert(Struzik_Search(sorted_array, 7, 7) == sorted_array); +int *sorted_array = new int[7]{7, 10, 15, 23, 70, 105, 203}; +assert(Struzik_Search(sorted_array, 7, 0) == nullptr); +assert(Struzik_Search(sorted_array, 7, 1000) == nullptr); +assert(Struzik_Search(sorted_array, 7, 50) == nullptr); +assert(Struzik_Search(sorted_array, 7, 7) == sorted_array); return EXIT_SUCCESS; } From c1629a93909943794d77db3536a2c1ca2e3efa90 Mon Sep 17 00:00:00 2001 From: Divide-et-impera-11 <54957167+Divide-et-impera-11@users.noreply.github.com> Date: Sat, 30 Nov 2019 21:20:54 +0100 Subject: [PATCH 11/20] Update exponential_search.cpp --- Search/exponential_search.cpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/Search/exponential_search.cpp b/Search/exponential_search.cpp index 90ce27409..1c12351a2 100644 --- a/Search/exponential_search.cpp +++ b/Search/exponential_search.cpp @@ -13,7 +13,7 @@ while (lower_index <= upper_index) { } return nullptr; } -template Type* Struzik_Search(Type* array, size_t size, Type key) { +template Type* struzik_search(Type* array, size_t size, Type key) { uint32_t block_front(0), block_size = size == 0 ? 0 : 1; while (block_front != block_size) { if (*(array + block_size - 1) < key) { @@ -27,9 +27,9 @@ return nullptr; } int main() { int *sorted_array = new int[7]{7, 10, 15, 23, 70, 105, 203}; -assert(Struzik_Search(sorted_array, 7, 0) == nullptr); -assert(Struzik_Search(sorted_array, 7, 1000) == nullptr); -assert(Struzik_Search(sorted_array, 7, 50) == nullptr); -assert(Struzik_Search(sorted_array, 7, 7) == sorted_array); -return EXIT_SUCCESS; +assert(struzik_search(sorted_array, 7, 0) == nullptr); +assert(struzik_search(sorted_array, 7, 1000) == nullptr); +assert(struzik_search(sorted_array, 7, 50) == nullptr); +assert(struzik_search(sorted_array, 7, 7) == sorted_array); +return 0; } From 51da734a423689f25a4488d9a68338a868d67672 Mon Sep 17 00:00:00 2001 From: Divide-et-impera-11 <54957167+Divide-et-impera-11@users.noreply.github.com> Date: Sat, 30 Nov 2019 21:27:08 +0100 Subject: [PATCH 12/20] Update exponential_search.cpp --- Search/exponential_search.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Search/exponential_search.cpp b/Search/exponential_search.cpp index 1c12351a2..f5302eee0 100644 --- a/Search/exponential_search.cpp +++ b/Search/exponential_search.cpp @@ -1,9 +1,9 @@ -// Copyright 2020 Divide-et-impera-11 +// copyright 2020 divide-et-impera-11 #include #include #include using namespaces std; -template inline Type* binary_s(Type *array, size_t size, Type key) { +template inline type* binary_s(type *array, size_t size, type key) { int32_t lower_index(0), upper_index(size - 1), middle_index; while (lower_index <= upper_index) { middle_index = floor((lower_index + upper_index) / 2); @@ -13,7 +13,7 @@ while (lower_index <= upper_index) { } return nullptr; } -template Type* struzik_search(Type* array, size_t size, Type key) { +template type* struzik_search(type* array, size_t size, type key) { uint32_t block_front(0), block_size = size == 0 ? 0 : 1; while (block_front != block_size) { if (*(array + block_size - 1) < key) { @@ -21,7 +21,7 @@ template Type* struzik_search(Type* array, size_t size, Type key) { (block_size * 2 - 1 < size) ? (block_size *= 2) : block_size = size; continue; } - return binary_s(array + block_front, (block_size - block_front), key); + return binary_s(array + block_front, (block_size - block_front), key); } return nullptr; } From 16007b980831ec4cd9221e4bcfdeed2d903197e5 Mon Sep 17 00:00:00 2001 From: Divide-et-impera-11 <54957167+Divide-et-impera-11@users.noreply.github.com> Date: Sat, 30 Nov 2019 21:32:32 +0100 Subject: [PATCH 13/20] Update exponential_search.cpp --- Search/exponential_search.cpp | 29 ----------------------------- 1 file changed, 29 deletions(-) diff --git a/Search/exponential_search.cpp b/Search/exponential_search.cpp index f5302eee0..0684bc602 100644 --- a/Search/exponential_search.cpp +++ b/Search/exponential_search.cpp @@ -1,35 +1,6 @@ // copyright 2020 divide-et-impera-11 -#include #include #include -using namespaces std; -template inline type* binary_s(type *array, size_t size, type key) { -int32_t lower_index(0), upper_index(size - 1), middle_index; -while (lower_index <= upper_index) { - middle_index = floor((lower_index + upper_index) / 2); - if (*(array + middle_index) < key) lower_index = (middle_index + 1); - else if (*(array + middle_index) > key)upper_index = (middle_index - 1); - else return (array + middle_index); - } -return nullptr; -} -template type* struzik_search(type* array, size_t size, type key) { - uint32_t block_front(0), block_size = size == 0 ? 0 : 1; - while (block_front != block_size) { - if (*(array + block_size - 1) < key) { - block_front = block_size; - (block_size * 2 - 1 < size) ? (block_size *= 2) : block_size = size; - continue; - } - return binary_s(array + block_front, (block_size - block_front), key); - } -return nullptr; -} int main() { -int *sorted_array = new int[7]{7, 10, 15, 23, 70, 105, 203}; -assert(struzik_search(sorted_array, 7, 0) == nullptr); -assert(struzik_search(sorted_array, 7, 1000) == nullptr); -assert(struzik_search(sorted_array, 7, 50) == nullptr); -assert(struzik_search(sorted_array, 7, 7) == sorted_array); return 0; } From 63f9fd2dc52f2ca721050a52057f86aa06f73ca7 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Sat, 30 Nov 2019 21:52:06 +0100 Subject: [PATCH 14/20] Search -> search --- {Search => search}/exponential_search.cpp | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename {Search => search}/exponential_search.cpp (100%) diff --git a/Search/exponential_search.cpp b/search/exponential_search.cpp similarity index 100% rename from Search/exponential_search.cpp rename to search/exponential_search.cpp From eeb7d5caa505129de9bcdcba442ee95f7a03dbfb Mon Sep 17 00:00:00 2001 From: Divide-et-impera-11 <54957167+Divide-et-impera-11@users.noreply.github.com> Date: Sat, 30 Nov 2019 22:41:18 +0100 Subject: [PATCH 15/20] Update exponential_search.cpp --- search/exponential_search.cpp | 43 ++++++++++++++++++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) diff --git a/search/exponential_search.cpp b/search/exponential_search.cpp index 0684bc602..e140d9df6 100644 --- a/search/exponential_search.cpp +++ b/search/exponential_search.cpp @@ -1,6 +1,47 @@ -// copyright 2020 divide-et-impera-11 +// Copyright 2020 Divide-et-Impera-11 +#include #include #include +using namespaces std; +// Binary Search Algorithm(use by struziki algorithm) +// Time Complexity O(log n) where 'n' is the number of elements +// Worst Time Complexity O(log n) +// Best Time Complexity Ω(1) +// Space Complexity O(1) +// Auxiliary Space Complexity O(1) +template inline Type* binary_s(Type *array, size_t size, Type key) { +int32_t lower_index(0), upper_index(size - 1), middle_index; +while (lower_index <= upper_index) { + middle_index = floor((lower_index + upper_index) / 2); + if (*(array + middle_index) < key) lower_index = (middle_index + 1); + else if (*(array + middle_index) > key)upper_index = (middle_index - 1); + else return (array + middle_index); + } +return nullptr; +} +// Struzik Search Algorithm(Exponential) +// Time Complexity O(log i)where i is the position of search key in the list +// Worst Time Complexity O(log i) +// Best Time Complexity Ω(1) +// Space Complexity O(1) +// Auxiliary Space Complexity O(1) +template Type* struzik_search(Type* array, size_t size, Type key) { + uint32_t block_front(0), block_size = size == 0 ? 0 : 1; + while (block_front != block_size) { + if (*(array + block_size - 1) < key) { + block_front = block_size; + (block_size * 2 - 1 < size) ? (block_size *= 2) : block_size = size; + continue; + } + return binary_s(array + block_front, (block_size - block_front), key); + } +return nullptr; +} int main() { +int *sorted_array = new int[7]{7, 10, 15, 23, 70, 105, 203}; +assert(struzik_search(sorted_array, 7, 0) == nullptr); +assert(struzik_search(sorted_array, 7, 1000) == nullptr); +assert(struzik_search(sorted_array, 7, 50) == nullptr); +assert(struzik_search(sorted_array, 7, 7) == sorted_array); return 0; } From 67fdd7b7356f2b9c56f5fa325e8d172f81e61785 Mon Sep 17 00:00:00 2001 From: Divide-et-impera-11 <54957167+Divide-et-impera-11@users.noreply.github.com> Date: Sat, 30 Nov 2019 22:56:10 +0100 Subject: [PATCH 16/20] Update exponential_search.cpp --- search/exponential_search.cpp | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/search/exponential_search.cpp b/search/exponential_search.cpp index e140d9df6..d217fce64 100644 --- a/search/exponential_search.cpp +++ b/search/exponential_search.cpp @@ -1,9 +1,9 @@ -// Copyright 2020 Divide-et-Impera-11 +// Copyright 2020 Divide-et-impera-11 #include #include #include using namespaces std; -// Binary Search Algorithm(use by struziki algorithm) +// Binary Search Algorithm(use by struzik algorithm) // Time Complexity O(log n) where 'n' is the number of elements // Worst Time Complexity O(log n) // Best Time Complexity Ω(1) @@ -25,6 +25,13 @@ return nullptr; // Best Time Complexity Ω(1) // Space Complexity O(1) // Auxiliary Space Complexity O(1) +/* Tha algorithm try to search the range where the key should be. +If it has been found we do a binary search there. +The range of the search grows by exponential every time. +If the key is larger than the last element of array, +the start of block(block_front) will be equal to the end of block(block_size) +and the algorithm return null ponter, +every other cases the algoritm return fom the loop. */ template Type* struzik_search(Type* array, size_t size, Type key) { uint32_t block_front(0), block_size = size == 0 ? 0 : 1; while (block_front != block_size) { @@ -38,10 +45,12 @@ template Type* struzik_search(Type* array, size_t size, Type key) { return nullptr; } int main() { +// TEST CASES int *sorted_array = new int[7]{7, 10, 15, 23, 70, 105, 203}; assert(struzik_search(sorted_array, 7, 0) == nullptr); assert(struzik_search(sorted_array, 7, 1000) == nullptr); assert(struzik_search(sorted_array, 7, 50) == nullptr); assert(struzik_search(sorted_array, 7, 7) == sorted_array); +// TEST CASES return 0; } From dbb23219b2e5622016f03dd1326e0ef0fca84c17 Mon Sep 17 00:00:00 2001 From: Divide-et-impera-11 <54957167+Divide-et-impera-11@users.noreply.github.com> Date: Sat, 30 Nov 2019 22:58:04 +0100 Subject: [PATCH 17/20] Update exponential_search.cpp --- search/exponential_search.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/search/exponential_search.cpp b/search/exponential_search.cpp index d217fce64..b8343fa02 100644 --- a/search/exponential_search.cpp +++ b/search/exponential_search.cpp @@ -45,12 +45,12 @@ template Type* struzik_search(Type* array, size_t size, Type key) { return nullptr; } int main() { -// TEST CASES +// TEST CASES int *sorted_array = new int[7]{7, 10, 15, 23, 70, 105, 203}; assert(struzik_search(sorted_array, 7, 0) == nullptr); assert(struzik_search(sorted_array, 7, 1000) == nullptr); assert(struzik_search(sorted_array, 7, 50) == nullptr); assert(struzik_search(sorted_array, 7, 7) == sorted_array); -// TEST CASES +// TEST CASES return 0; } From 5931f3d23436cc635d5ccaa41c462cc6603bcfe9 Mon Sep 17 00:00:00 2001 From: Divide-et-impera-11 <54957167+Divide-et-impera-11@users.noreply.github.com> Date: Sat, 30 Nov 2019 22:58:27 +0100 Subject: [PATCH 18/20] Update exponential_search.cpp --- search/exponential_search.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/search/exponential_search.cpp b/search/exponential_search.cpp index b8343fa02..55e376f51 100644 --- a/search/exponential_search.cpp +++ b/search/exponential_search.cpp @@ -45,12 +45,12 @@ template Type* struzik_search(Type* array, size_t size, Type key) { return nullptr; } int main() { -// TEST CASES +//TEST CASES int *sorted_array = new int[7]{7, 10, 15, 23, 70, 105, 203}; assert(struzik_search(sorted_array, 7, 0) == nullptr); assert(struzik_search(sorted_array, 7, 1000) == nullptr); assert(struzik_search(sorted_array, 7, 50) == nullptr); assert(struzik_search(sorted_array, 7, 7) == sorted_array); -// TEST CASES +//TEST CASES return 0; } From 3b6e05e68fa5cab8fd2e542d061fca0aa000b529 Mon Sep 17 00:00:00 2001 From: Divide-et-impera-11 <54957167+Divide-et-impera-11@users.noreply.github.com> Date: Sat, 30 Nov 2019 22:59:52 +0100 Subject: [PATCH 19/20] Update exponential_search.cpp --- search/exponential_search.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/search/exponential_search.cpp b/search/exponential_search.cpp index 55e376f51..b8343fa02 100644 --- a/search/exponential_search.cpp +++ b/search/exponential_search.cpp @@ -45,12 +45,12 @@ template Type* struzik_search(Type* array, size_t size, Type key) { return nullptr; } int main() { -//TEST CASES +// TEST CASES int *sorted_array = new int[7]{7, 10, 15, 23, 70, 105, 203}; assert(struzik_search(sorted_array, 7, 0) == nullptr); assert(struzik_search(sorted_array, 7, 1000) == nullptr); assert(struzik_search(sorted_array, 7, 50) == nullptr); assert(struzik_search(sorted_array, 7, 7) == sorted_array); -//TEST CASES +// TEST CASES return 0; } From 332d18a13f4909dca5e3c9e243165ad5639b3099 Mon Sep 17 00:00:00 2001 From: Divide-et-impera-11 <54957167+Divide-et-impera-11@users.noreply.github.com> Date: Sat, 30 Nov 2019 23:00:07 +0100 Subject: [PATCH 20/20] Update exponential_search.cpp