From 168acd0e7705c285ec432240962f658a33170927 Mon Sep 17 00:00:00 2001 From: Alvin Philips Date: Sun, 24 Oct 2021 02:48:44 +0530 Subject: [PATCH] Rewrote array_left_rotation --- .../array_left_rotation.cpp | 120 +++++++++++++----- 1 file changed, 91 insertions(+), 29 deletions(-) diff --git a/operations_on_datastructures/array_left_rotation.cpp b/operations_on_datastructures/array_left_rotation.cpp index 7b8f7f279..43b3baae3 100644 --- a/operations_on_datastructures/array_left_rotation.cpp +++ b/operations_on_datastructures/array_left_rotation.cpp @@ -1,31 +1,93 @@ -#include -using namespace std; -int main() { - int n, k; - cout << "Enter size of array=\t"; - cin >> n; - cout << "Enter Number of indeces u want to rotate the array to left=\t"; - cin >> k; - int a[n]; - cout << "Enter elements of array=\t"; - for (int i = 0; i < n; i++) { - cin >> a[i]; +/** + * @file + * @brief Implementation for the [Union of two sorted + * Arrays](https://en.wikipedia.org/wiki/Union_(set_theory)) + * algorithm. + * @details The Union of two arrays is the collection of all the unique elements + * in the first array, combined with all of the unique elements of a second + * array. This implementation uses ordered arrays, and an algorithm to correctly + * order them and return the result as a new array (vector). + * @author [Alvin](https://github.com/polarvoid) + */ + +#include /// for assert +#include /// for IO operations +#include /// for std::vector + +/** + * @namespace operations_on_datastructures + * @brief Operations on Data Structures + */ +namespace operations_on_datastructures { + +/** + * @brief Prints the values of a vector sequentially, ending with a newline + * character. + * @param array Reference to the array to be printed + * @returns void + */ +void print(const std::vector &array) { + for (int32_t i : array) { + std::cout << i << " "; /// Print each value in the array } - int temp = 0; - for (int i = 0; i < k; i++) { - temp = a[0]; - for (int j = 0; j < n; j++) { - if (j == n - 1) { - a[n - 1] = temp; - } else { - a[j] = a[j + 1]; - } - } - } - cout << "Your rotated array is=\t"; - for (int j = 0; j < n; j++) { - cout << a[j] << " "; - } - getchar(); - return 0; + std::cout << "\n"; /// Print newline } + +/** + * @brief Shifts the given vector to the left by the shift amount and returns a + * new vector with the result. The original vector is not mutated. + * @details Shifts the values of the vector, by creating a new vector and adding + * values from the shift index to the end, then appending the rest of the + * elements from the start of the vector. + * @param array A reference to the input std::vector + * @param shift The amount to be shifted to the left + * @returns A std::vector with the shiftedd values + */ +std::vector shift_left(const std::vector &array, + size_t shift) { + if (shift >= array.size()) { + return {}; ///< We got an invalid shift, return empty array + } + std::vector res(array.size()); ///< Result array + for (int i = shift; i < array.size(); i++) { + res.push_back(array[i]); ///< Add values after the shift index + } + for (int i = 0; i < shift; i++) { + res.push_back(array[i]); ///< Add the values from the start + } + return res; +} + +} // namespace operations_on_datastructures + +/** + * @namespace tests + * @brief Testcases to check Union of Two Arrays. + */ +namespace tests { +using operations_on_datastructures::print; +using operations_on_datastructures::shift_left; +/** + * @brief A Test to check an edge case (two empty arrays) + * @returns void + */ +void test1() { + std::vector arr = {1, 2, 3, 4, 5}; + print(shift_left(arr, 2)); +} +} // namespace tests + +/** + * @brief Function to test the correctness of get_union() function + * @returns void + */ +static void test() { tests::test1(); } + +/** + * @brief main function + * @returns 0 on exit + */ +int main() { + test(); // run self-test implementations + return 0; +} \ No newline at end of file