diff --git a/ALGOR.cpp b/ALGOR.cpp index ed3b849..eef2e3a 100644 --- a/ALGOR.cpp +++ b/ALGOR.cpp @@ -347,6 +347,221 @@ void MersenneTwister::Init0(int seed) #undef MERS_N +template void Exchange_Sorts::BubbleSort::start_sort() +{ + for (unsigned int i = 0; i < this->ARRAY->array_size; i++) + { + for (unsigned int j = 0; j < this->ARRAY->array_size - 1; j++) + { + if (this->ARRAY->array[j] > this->ARRAY->array[j + 1]) + { + swap(this->ARRAY->array[j], this->ARRAY->array[j + 1]); + } + } + } +} + +template void Exchange_Sorts::CocktailShakerSort::start_sort() +{ + int leftMark = 1, rightMark = this->ARRAY->array_size - 1; + while (leftMark <= rightMark) + { + for (int i = rightMark; i >= leftMark; i--) + { + if (this->ARRAY->array[i - 1] > this->ARRAY->array[i]) + { + swap(this->ARRAY->array[i], this->ARRAY->array[i - 1]); + } + } + leftMark++; + for (int i = leftMark; i <= rightMark; i++) + { + if (this->ARRAY->array[i - 1] > this->ARRAY->array[i]) + { + swap(this->ARRAY->array[i], this->ARRAY->array[i - 1]); + } + } + rightMark--; + } +} + +template void Exchange_Sorts::QuickSort::start_sort() +{ + quick_sort(0, this->ARRAY->array_size - 1); +} + +template void Exchange_Sorts::QuickSort::quick_sort(const int &left_limit, const int &right_limit) +{ + type_array middle = this->ARRAY->array[(left_limit + right_limit) / 2]; + int start = left_limit, finish = right_limit; + do + { + while (this->ARRAY->array[start] < middle) + { + start++; + } + while (this->ARRAY->array[finish] > middle) + { + finish--; + } + if (start <= finish) + { + swap(this->ARRAY->array[start], this->ARRAY->array[finish]); + start++; + finish--; + } + } while (start < finish); + if (left_limit < finish) + { + quick_sort(left_limit, finish); + } + if (start < right_limit) + { + quick_sort(start, right_limit); + } +} + +template void Selection_Sorts::HeapSort::start_sort() +{ + for (int right = this->ARRAY->array_size / 2 - 1; right >= 0; right--) + { + heapify(this->ARRAY->array, right, this->ARRAY->array_size); + } + for (int i = this->ARRAY->array_size - 1; i >= 0; i--) + { + swap(this->ARRAY->array[0], this->ARRAY->array[i]); + heapify(this->ARRAY->array, 0, i); + } +} + +template void Selection_Sorts::HeapSort::heapify(type_array *Array, const asize_t &count, const asize_t &array_size) +{ + asize_t left = 2 * count + 1, large = count, right = 2 * count + 2; + if (left < array_size && Array[left] > Array[large]) + { + large = left; + } + if (right < array_size && Array[right] > Array[large]) + { + large = right; + } + if (large != count) + { + swap(Array[count], Array[large]); + heapify(Array, large, array_size); + } +} + +template void Insertion_Sorts::InsertSort::start_sort() +{ + for (unsigned int i = 0; i < this->ARRAY->array_size; i++) + { + for (int j = i; j > 0 && this->ARRAY->array[j - 1] > this->ARRAY->array[j]; j--) + { + swap(this->ARRAY->array[j - 1], this->ARRAY->array[j]); + } + } +} + +template void Merge_Sorts::MergeSort::start_sort() +{ + merge_sort(this->ARRAY->array, 0, this->ARRAY->array_size - 1); +} + +template void Merge_Sorts::MergeSort::merge_sort(type_array *Array, const int &left_limit, const int &right_limit) +{ + if (left_limit < right_limit) + { + int middle = left_limit + (right_limit - left_limit) / 2; + merge_sort(Array, left_limit, middle); + merge_sort(Array, middle + 1, right_limit); + merge(Array, left_limit, middle, right_limit); + } +} + +template void Merge_Sorts::MergeSort::merge(type_array *Array, const int &left_limit, const int &middle_limit, const int &right_limit) +{ + int start = left_limit, finish = middle_limit + 1; + type_array *tempArray = new type_array[right_limit - left_limit + 1]; + for (int i = left_limit; i <= right_limit; i++) + { + if ((start <= middle_limit) && ((finish > right_limit) || (Array[start] < Array[finish]))) + { + tempArray[i - left_limit] = Array[start]; + start++; + } + else + { + tempArray[i - left_limit] = Array[finish]; + finish++; + } + } + for (int i = left_limit; i <= right_limit; i++) + { + Array[i] = tempArray[i - left_limit]; + } + delete[] tempArray; +} + +void Noncomparison_Sort::CountingSort::start_sort() +{ + int min = minimum(ARRAY->array, ARRAY->array_size), + max = maximum(ARRAY->array, ARRAY->array_size); + int *tempArray = new int[max - min + 1]; + for (int i = 0; i < max - min + 1; i++) + { + tempArray[i] = 0; + } + for (unsigned int i = 0; i < ARRAY->array_size; i++) + { + tempArray[ARRAY->array[i] - min] = tempArray[ARRAY->array[i] - min] + 1; + } + for (int i = 0, j = min; j < max + 1; j++) + { + while (tempArray[j - min] != 0) + { + ARRAY->array[i] = j; + tempArray[j - min]--; + i++; + } + } + delete[] tempArray; +} + +void Noncomparison_Sort::RadixSort::start_sort() +{ + int exp = 1, bit = 10, max = maximum(ARRAY->array, ARRAY->array_size); + int *tempArray = new int[ARRAY->array_size], *bucket = new int[bit]; + while (max / exp > 0) + { + for (int i = 0; i < bit; i++) + { + bucket[i] = 0; + } + for (unsigned int i = 0; i < ARRAY->array_size; i++) + { + bucket[(ARRAY->array[i] / exp) % bit]++; + } + for (int i = 1; i < bit; i++) + { + bucket[i] += bucket[i - 1]; + } + for (int i = ARRAY->array_size - 1; i >= 0; i--) + { + int current = (ARRAY->array[i] % (exp * bit)) / exp; + bucket[current]--; + tempArray[bucket[current]] = ARRAY->array[i]; + } + for (unsigned int i = 0; i < ARRAY->array_size; i++) + { + ARRAY->array[i] = tempArray[i]; + } + exp *= bit; + } + delete[] bucket; + delete[] tempArray; +} + template void ARRAYDATA::generatedData(const int &min_limit, const int &max_limit) { //Generating elements for an array @@ -784,221 +999,6 @@ template void ARRAYDATA::operator/(const asize_ remove_struct(temp); } -template void Exchange_Sorts::BubbleSort::start_sort() -{ - for (unsigned int i = 0; i < this->ARRAY->array_size; i++) - { - for (unsigned int j = 0; j < this->ARRAY->array_size - 1; j++) - { - if (this->ARRAY->array[j] > this->ARRAY->array[j + 1]) - { - swap(this->ARRAY->array[j], this->ARRAY->array[j + 1]); - } - } - } -} - -template void Exchange_Sorts::CocktailShakerSort::start_sort() -{ - int leftMark = 1, rightMark = this->ARRAY->array_size - 1; - while (leftMark <= rightMark) - { - for (int i = rightMark; i >= leftMark; i--) - { - if (this->ARRAY->array[i - 1] > this->ARRAY->array[i]) - { - swap(this->ARRAY->array[i], this->ARRAY->array[i - 1]); - } - } - leftMark++; - for (int i = leftMark; i <= rightMark; i++) - { - if (this->ARRAY->array[i - 1] > this->ARRAY->array[i]) - { - swap(this->ARRAY->array[i], this->ARRAY->array[i - 1]); - } - } - rightMark--; - } -} - -template void Exchange_Sorts::QuickSort::start_sort() -{ - quick_sort(0, this->ARRAY->array_size - 1); -} - -template void Exchange_Sorts::QuickSort::quick_sort(const int &left_limit, const int &right_limit) -{ - type_array middle = this->ARRAY->array[(left_limit + right_limit) / 2]; - int start = left_limit, finish = right_limit; - do - { - while (this->ARRAY->array[start] < middle) - { - start++; - } - while (this->ARRAY->array[finish] > middle) - { - finish--; - } - if (start <= finish) - { - swap(this->ARRAY->array[start], this->ARRAY->array[finish]); - start++; - finish--; - } - } while (start < finish); - if (left_limit < finish) - { - quick_sort(left_limit, finish); - } - if (start < right_limit) - { - quick_sort(start, right_limit); - } -} - -template void Selection_Sorts::HeapSort::start_sort() -{ - for (int right = this->ARRAY->array_size / 2 - 1; right >= 0; right--) - { - heapify(this->ARRAY->array, right, this->ARRAY->array_size); - } - for (int i = this->ARRAY->array_size - 1; i >= 0; i--) - { - swap(this->ARRAY->array[0], this->ARRAY->array[i]); - heapify(this->ARRAY->array, 0, i); - } -} - -template void Selection_Sorts::HeapSort::heapify(type_array *Array, const asize_t &count, const asize_t &array_size) -{ - asize_t left = 2 * count + 1, large = count, right = 2 * count + 2; - if (left < array_size && Array[left] > Array[large]) - { - large = left; - } - if (right < array_size && Array[right] > Array[large]) - { - large = right; - } - if (large != count) - { - swap(Array[count], Array[large]); - heapify(Array, large, array_size); - } -} - -template void Insertion_Sorts::InsertSort::start_sort() -{ - for (unsigned int i = 0; i < this->ARRAY->array_size; i++) - { - for (int j = i; j > 0 && this->ARRAY->array[j - 1] > this->ARRAY->array[j]; j--) - { - swap(this->ARRAY->array[j - 1], this->ARRAY->array[j]); - } - } -} - -template void Merge_Sorts::MergeSort::start_sort() -{ - merge_sort(this->ARRAY->array, 0, this->ARRAY->array_size - 1); -} - -template void Merge_Sorts::MergeSort::merge_sort(type_array *Array, const int &left_limit, const int &right_limit) -{ - if (left_limit < right_limit) - { - int middle = left_limit + (right_limit - left_limit) / 2; - merge_sort(Array, left_limit, middle); - merge_sort(Array, middle + 1, right_limit); - merge(Array, left_limit, middle, right_limit); - } -} - -template void Merge_Sorts::MergeSort::merge(type_array *Array, const int &left_limit, const int &middle_limit, const int &right_limit) -{ - int start = left_limit, finish = middle_limit + 1; - type_array *tempArray = new type_array[right_limit - left_limit + 1]; - for (int i = left_limit; i <= right_limit; i++) - { - if ((start <= middle_limit) && ((finish > right_limit) || (Array[start] < Array[finish]))) - { - tempArray[i - left_limit] = Array[start]; - start++; - } - else - { - tempArray[i - left_limit] = Array[finish]; - finish++; - } - } - for (int i = left_limit; i <= right_limit; i++) - { - Array[i] = tempArray[i - left_limit]; - } - delete[] tempArray; -} - -void Noncomparison_Sort::CountingSort::start_sort() -{ - int min = minimum(ARRAY->array, ARRAY->array_size), - max = maximum(ARRAY->array, ARRAY->array_size); - int *tempArray = new int[max - min + 1]; - for (int i = 0; i < max - min + 1; i++) - { - tempArray[i] = 0; - } - for (unsigned int i = 0; i < ARRAY->array_size; i++) - { - tempArray[ARRAY->array[i] - min] = tempArray[ARRAY->array[i] - min] + 1; - } - for (int i = 0, j = min; j < max + 1; j++) - { - while (tempArray[j - min] != 0) - { - ARRAY->array[i] = j; - tempArray[j - min]--; - i++; - } - } - delete[] tempArray; -} - -void Noncomparison_Sort::RadixSort::start_sort() -{ - int exp = 1, bit = 10, max = maximum(ARRAY->array, ARRAY->array_size); - int *tempArray = new int[ARRAY->array_size], *bucket = new int[bit]; - while (max / exp > 0) - { - for (int i = 0; i < bit; i++) - { - bucket[i] = 0; - } - for (unsigned int i = 0; i < ARRAY->array_size; i++) - { - bucket[(ARRAY->array[i] / exp) % bit]++; - } - for (int i = 1; i < bit; i++) - { - bucket[i] += bucket[i - 1]; - } - for (int i = ARRAY->array_size - 1; i >= 0; i--) - { - int current = (ARRAY->array[i] % (exp * bit)) / exp; - bucket[current]--; - tempArray[bucket[current]] = ARRAY->array[i]; - } - for (unsigned int i = 0; i < ARRAY->array_size; i++) - { - ARRAY->array[i] = tempArray[i]; - } - exp *= bit; - } - delete[] bucket; - delete[] tempArray; -} - template void swap(int &, int &); template void swap(float &, float &); template void swap(char &, char &); @@ -1035,6 +1035,46 @@ template void remove_struct(Array *&); template void remove_struct(Array *&); template void remove_struct(Array *&); +template void Exchange_Sorts::BubbleSort::start_sort(); +template void Exchange_Sorts::BubbleSort::start_sort(); +template void Exchange_Sorts::BubbleSort::start_sort(); + +template void Exchange_Sorts::CocktailShakerSort::start_sort(); +template void Exchange_Sorts::CocktailShakerSort::start_sort(); +template void Exchange_Sorts::CocktailShakerSort::start_sort(); + +template void Exchange_Sorts::QuickSort::start_sort(); +template void Exchange_Sorts::QuickSort::start_sort(); +template void Exchange_Sorts::QuickSort::start_sort(); + +template void Exchange_Sorts::QuickSort::quick_sort(const int &, const int &); +template void Exchange_Sorts::QuickSort::quick_sort(const int &, const int &); +template void Exchange_Sorts::QuickSort::quick_sort(const int &, const int &); + +template void Selection_Sorts::HeapSort::start_sort(); +template void Selection_Sorts::HeapSort::start_sort(); +template void Selection_Sorts::HeapSort::start_sort(); + +template void Selection_Sorts::HeapSort::heapify(int *, const asize_t &, const asize_t &); +template void Selection_Sorts::HeapSort::heapify(float *, const asize_t &, const asize_t &); +template void Selection_Sorts::HeapSort::heapify(char *, const asize_t &, const asize_t &); + +template void Insertion_Sorts::InsertSort::start_sort(); +template void Insertion_Sorts::InsertSort::start_sort(); +template void Insertion_Sorts::InsertSort::start_sort(); + +template void Merge_Sorts::MergeSort::start_sort(); +template void Merge_Sorts::MergeSort::start_sort(); +template void Merge_Sorts::MergeSort::start_sort(); + +template void Merge_Sorts::MergeSort::merge_sort(int *, const int &, const int &); +template void Merge_Sorts::MergeSort::merge_sort(float *, const int &, const int &); +template void Merge_Sorts::MergeSort::merge_sort(char *, const int &, const int &); + +template void Merge_Sorts::MergeSort::merge(int *, const int &, const int &, const int &); +template void Merge_Sorts::MergeSort::merge(float *, const int &, const int &, const int &); +template void Merge_Sorts::MergeSort::merge(char *, const int &, const int &, const int &); + template void ARRAYDATA::generatedData(const int &, const int &); template void ARRAYDATA::generatedData(const int &, const int &); template void ARRAYDATA::generatedData(const int &, const int &); @@ -1162,43 +1202,3 @@ template void ARRAYDATA::operator*(const asize_t &); template void ARRAYDATA::operator/(const asize_t &); template void ARRAYDATA::operator/(const asize_t &); template void ARRAYDATA::operator/(const asize_t &); - -template void Exchange_Sorts::BubbleSort::start_sort(); -template void Exchange_Sorts::BubbleSort::start_sort(); -template void Exchange_Sorts::BubbleSort::start_sort(); - -template void Exchange_Sorts::CocktailShakerSort::start_sort(); -template void Exchange_Sorts::CocktailShakerSort::start_sort(); -template void Exchange_Sorts::CocktailShakerSort::start_sort(); - -template void Exchange_Sorts::QuickSort::start_sort(); -template void Exchange_Sorts::QuickSort::start_sort(); -template void Exchange_Sorts::QuickSort::start_sort(); - -template void Exchange_Sorts::QuickSort::quick_sort(const int &, const int &); -template void Exchange_Sorts::QuickSort::quick_sort(const int &, const int &); -template void Exchange_Sorts::QuickSort::quick_sort(const int &, const int &); - -template void Selection_Sorts::HeapSort::start_sort(); -template void Selection_Sorts::HeapSort::start_sort(); -template void Selection_Sorts::HeapSort::start_sort(); - -template void Selection_Sorts::HeapSort::heapify(int *, const asize_t &, const asize_t &); -template void Selection_Sorts::HeapSort::heapify(float *, const asize_t &, const asize_t &); -template void Selection_Sorts::HeapSort::heapify(char *, const asize_t &, const asize_t &); - -template void Insertion_Sorts::InsertSort::start_sort(); -template void Insertion_Sorts::InsertSort::start_sort(); -template void Insertion_Sorts::InsertSort::start_sort(); - -template void Merge_Sorts::MergeSort::start_sort(); -template void Merge_Sorts::MergeSort::start_sort(); -template void Merge_Sorts::MergeSort::start_sort(); - -template void Merge_Sorts::MergeSort::merge_sort(int *, const int &, const int &); -template void Merge_Sorts::MergeSort::merge_sort(float *, const int &, const int &); -template void Merge_Sorts::MergeSort::merge_sort(char *, const int &, const int &); - -template void Merge_Sorts::MergeSort::merge(int *, const int &, const int &, const int &); -template void Merge_Sorts::MergeSort::merge(float *, const int &, const int &, const int &); -template void Merge_Sorts::MergeSort::merge(char *, const int &, const int &, const int &); diff --git a/ALGOR.hpp b/ALGOR.hpp index 5ff3c38..7225759 100644 --- a/ALGOR.hpp +++ b/ALGOR.hpp @@ -18,7 +18,7 @@ * ---------------------------------------------------------------------------- * * **************************************************************************** * * ---------------------------------------------------------------------------- * - * Version: 2.0.0-dev * + * Version: 2.0.0 * * ---------------------------------------------------------------------------- * * **************************************************************************** * * ---------------------------------------------------------------------------- * @@ -27,10 +27,11 @@ * 1. ALGOR_CORE - basic structures and functions of the library * * 2. ALGOR_EXCEPTION - class for working with exceptions * * 3. ALGOR_RANDOM - own random number generators * - * 4. AlGOR_ARRAY - class for working with arrays * - * 5. AlGOR_SORTING - set of sorting methods * - * 6. AlGOR_HEAP - class for working with trees * - * 7. AlGOR_LIST - class for working with lists * + * 4. AlGOR_SORTING - set of sorting methods * + * 5. AlGOR_ARRAY - class for working with arrays * + * 6. ALGOR_MATRIX - class for working with matrices * + * 7. AlGOR_HEAP - class for working with trees * + * 8. AlGOR_LIST - class for working with lists * * * * ---------------------------------------------------------------------------- * * **************************************************************************** */ @@ -76,8 +77,6 @@ template class ArrayBase Array *ARRAY; //Pointer to a structure storing an array }; -//TODO Come up with your own alias for unsigned int, which will be used to indicate the size of the array - //ALGOR_EXCEPTION //TODO Excep class will be implemented in version 2.1.0 @@ -105,7 +104,7 @@ class MersenneTwister MersenneTwister(int seed); void RandomInit(int seed); void RandomInitByArray(int const seeds[], int NumSeeds); - int IRandom (int min, int max); + int IRandom(int min, int max); int IRandomX(int min, int max); double Random(); uint32_t BRandom(); @@ -117,59 +116,6 @@ class MersenneTwister uint32_t RejectionLimit; //Rejection limit used by IRandomX }; -//ALGOR_ARRAY - -enum ArrayStatus { SORTED, UNSORTED }; -enum ArrayType { NUMBER, STRING }; - -template class ARRAYDATA : public ArrayBase -{ -public: - ARRAYDATA(Array *&Array) : ArrayBase(Array) {} - ARRAYDATA(const asize_t &SIZE) : ArrayBase(SIZE) {} - ARRAYDATA() : ArrayBase() {} - - void generatedData(const int &min_limit, const int &max_limit); - void setNewData(Array *&Array); - void setData(Array *&Array); - void cloneData(Array *&CloningArray); - void cloneData(ARRAYDATA *&CloningObject); - void getData(Array *&DATA); - Array *getData(); - - void reset(); - void resize(const asize_t &NEW_SIZE, const type_array &setElement); - void replace(const unsigned int &position, const type_array &value); - void reverse(); - void remove(); - void respawn(); - - type_array getMin(ArrayStatus ArrStat = UNSORTED); - type_array getMax(ArrayStatus ArrStat = UNSORTED); - - Array *lenear_searcher(const type_array &required_element); - int binary_searcher(const type_array &required_element); - Array *searcherOccurrencesOfSubstring(Array *&SUBARRAY, ArrayType ArrType = NUMBER); - - type_array average(); - type_array mediana(); - type_array moda(int &highest_frequency); - Array *modas(int &highest_frequency); - - void operator&& (const type_array &value); - void operator! (); - void operator|| (const type_array &value); - void operator<< (ARRAYDATA *&appendingArray); - void operator>> (ARRAYDATA *&appendingArray); - void operator+ (const asize_t &addSize); - void operator- (const asize_t &subtractSize); - void operator* (const asize_t &multiplySize); - void operator/ (const asize_t ÷Size); - -private: - void binary_searcher(const type_array &required_element, int &number_point, int left_limit, int right_limit); -}; - //ALGOR_SORTING namespace Exchange_Sorts @@ -320,6 +266,70 @@ namespace Unknown_Sorts //class SampleSort{}; } +//ALGOR_ARRAY + +enum ArrayStatus +{ + SORTED, + UNSORTED +}; +enum ArrayType +{ + NUMBER, + STRING +}; + +template +class ARRAYDATA : public ArrayBase +{ +public: + ARRAYDATA(Array *&Array) : ArrayBase(Array) {} + ARRAYDATA(const asize_t &SIZE) : ArrayBase(SIZE) {} + ARRAYDATA() : ArrayBase() {} + + void generatedData(const int &min_limit, const int &max_limit); + void setNewData(Array *&Array); + void setData(Array *&Array); + void cloneData(Array *&CloningArray); + void cloneData(ARRAYDATA *&CloningObject); + void getData(Array *&DATA); + Array *getData(); + + void reset(); + void resize(const asize_t &NEW_SIZE, const type_array &setElement); + void replace(const unsigned int &position, const type_array &value); + void reverse(); + void remove(); + void respawn(); + + type_array getMin(ArrayStatus ArrStat = UNSORTED); + type_array getMax(ArrayStatus ArrStat = UNSORTED); + + Array *lenear_searcher(const type_array &required_element); + int binary_searcher(const type_array &required_element); + Array *searcherOccurrencesOfSubstring(Array *&SUBARRAY, ArrayType ArrType = NUMBER); + + type_array average(); + type_array mediana(); + type_array moda(int &highest_frequency); + Array *modas(int &highest_frequency); + + void operator&&(const type_array &value); + void operator!(); + void operator||(const type_array &value); + void operator<<(ARRAYDATA *&appendingArray); + void operator>>(ARRAYDATA *&appendingArray); + void operator+(const asize_t &addSize); + void operator-(const asize_t &subtractSize); + void operator*(const asize_t &multiplySize); + void operator/(const asize_t ÷Size); + +private: + void binary_searcher(const type_array &required_element, int &number_point, int left_limit, int right_limit); +}; + +//ALGOR_MATRIX + //ALGOR_HEAP //ALGOR_LIST diff --git a/CHANGELOG.md b/CHANGELOG.md index 5cd3c83..28298d5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,6 +19,77 @@ --- --> + +## v2.0.0 (23.12.2021) + +#### Enhancements: +- This is the largest update compared to previous versions. I finally implemented what I had planned from the very beginning, but for which I then two months ago did not have enough experience. I finally decided to split the original header into several. Of course, before that I decided what I want to implement in my library in the future. These were six sections: _Core_ (or _Base_) - the base on which everything else will be based; _Randomizer_ - own implementation of the randomizer (needed to get rid of any plug-in library); _Arrays_ - functions for working with arrays; _Sorting_ - sorting algorithms; _Trees_; _Lists_. As it is already clear, one of the goals was set: "To get rid of any plug-in library - my development should be unique and written only in pure C++ (in the future, assembly language inserts may appear)." True, later, in order to optimize and get rid of errors, another class was planned for working with exceptions, it was planned to implement matrices along with trees and lists, and in general the order changed slightly. And, finally, only at this stage did I understand how it is still possible to implement what I wanted to do two months ago. I rewrote the functions from the namespace for working with arrays into a class and began to extend its methods. A base class has been created, from which all sorting methods and the array processing class are inherited. Yes, sorting algorithms are now also described in the class. In the future, I will rebuild the architecture of the sorting classes, but this does not apply to this update. With the gigantic classification, problems appeared in the form of architecture. The library continues to support the use of not only classes but also structures. To do this, I also implemented basic functions that can be used when working with a structure, and on which most of the methods from the class are built, and some are even used in sorting classes. I ended up with a new library architecture. Further, based on the accumulated knowledge, I was able to separate the implementation from the declaration. I began to separate the implementation into separate .cpp files. Later, all the headers were combined into one common header, and all the implementation files were combined into one file with the implementation. So I was finally able to separate the implementation from the announcement - something that I had planned two months ago, but could not do then. +- I have implemented what I had planned for this update: completely rebuild the architecture and syntax of the library. Now it's even easier to use. In addition to the new architecture, I have implemented aliases for some types. +- Which functions have been rewritten, renamed, and which are truly new, it's hard to say. Therefore, I will describe this update like this: + - Removed: + 1. The _ArrayProcessing_ class with all its methods, but the functionality and algorithms are not thrown away, but rewritten into new methods and functions + - New project structure: + 1. _ALGOR_CORE_ ✔ + 2. _ALGOR_EXCEPTION_ ⧖ (will be released in version 2.1.0) + 3. _ALGOR_RANDOM_ ✔ + 4. _AlGOR_SORTING_ ✔ (The functionality will be expanded from versions 2.1.1 to 2.1.X) + 5. _AlGOR_ARRAY_ ✔ + 6. _ALGOR_MATRIX_ ⧖ (will be released in version 2.2.0) + 7. _AlGOR_HEAP_ ⧖ (will be released in version 3.0.0) + 8. _AlGOR_LIST_ ⧖ (will be released in version 4.0.0) + - New classes: + 1. _RC4_ - a simple cryptographic randomizer (its functionality will be further expanded) + 2. _MersenneTwister_ - an advanced randomizer (its functionality will be further expanded) + 3. _ARRAYDATA_ - a class for working with an array + - Everyone knows how to work with randomizers. Sorting hasn't changed much. I will describe the ARRAYDATA class: + 1. **Constructors** that accept either structure, array size, or void + 2. **generatedData()** - generates array elements + 3. **setNewData()** - removes the old array and stores the pointer to the new one + 4. **setData()** - saves a pointer to a new array without deleting the old one + 5. **cloneData()** - copies an array + 6. **getData()** - sets/returns a pointer to an array + 7. **reset()** - deletes the old array, creates a new one with the same size and fills it in the same range + 8. **resize()** - resizes an array + 9. **replace()** - changes the element at the specified position by the specified value + 10. **reverse()** - reverses the array + 11. **remove()** - removes an array + 12. **respawn()** - analogue of **reset()**, only at the end it does not fill the array with elements - it leaves it empty + 13. **getMin()** - returns the minimum value of the array (optimized method) + 14. **getMax()** - returns the maximum value of the array (optimized method) + 15. **lenear_searcher()** - Returns all occurrences of the element you are looking for + 16. **binary_searcher()** - returns the occurrence of the desired element in a sorted array + 17. **searcherOccurrencesOfSubstring()** - Returns all occurrences of a substring + 18. **average()** - returns the arithmetic average of all array elements + 19. **mediana()** - Returns the median of an array + 20. **moda()** - Returns the first occurrence of a mod + 21. **modas()** - Returns all occurrences of a mod + 22. **operator&&** - adds an element to the end of the array + 23. **operator!** - removes an element from the end of the array + 24. **operator||** - removes all elements from the array that match the specified value + 25. **operator<<** - merges two arrays in the current object + 26. **operator>>** - merges two arrays in the specified object + 27. **operator+** - increases the size of the array by the specified size + 28. **operator-** - decreases the size of the array by the specified size + 29. **operator*** - increases the size of the array several times by the specified size + 30. **operator/** - decreases the size of the array several times by the specified size + - New basic features: + 1. **swap()** - swaps two elements + 2. **minimum()** - returns the minimum value of an array by iteration + 3. **maximum()** - returns the maximum value of an array by iteration + 4. **addElement()** - adds the specified value to the specified position + 5. **subtractElement()** - removes the element at the specified position + 6. **subtractValue()** - removes all elements that match the specified value + 7. **copy()** - copies elements from one array to another + 8. **create_struct()** - creates a structure and returns a pointer to it + 9. **remove_struct()** - removes structure + - New types: + 1. **int8_t, int16_t, int32_t, int64_t** (taken from stdint) + 2. **uint8_t, uint16_t, uint32_t, uint64_t** (taken from stdint) + 3. **asize_t** - "array size type" - native type for specifying the type "array size" +- I got a lot of new experiences working on this update. + +--- + ## v1.1.0 (05.11.2021) #### Bug Fixes: diff --git a/README.md b/README.md index 157cec8..e34d677 100644 --- a/README.md +++ b/README.md @@ -1,12 +1,12 @@ [![template](https://img.shields.io/badge/Repository-template-darkred)](https://github.com/Nakama3942/template_rep) -[![GitHub license](https://img.shields.io/github/license/Nakama3942/SortingAlgorithms?color=gold&style=flat-square)](https://github.com/Nakama3942/SortingAlgorithms/blob/main/LICENSE) +[![GitHub license](https://img.shields.io/github/license/Nakama3942/ALGOR?color=gold&style=flat-square)](https://github.com/Nakama3942/ALGOR/blob/main/LICENSE) -[![CHANGELOG](https://img.shields.io/badge/here-CHANGELOG-yellow)](https://github.com/Nakama3942/SortingAlgorithms/blob/main/CHANGELOG.md) -[![CONTRIBUTING](https://img.shields.io/badge/here-CONTRIBUTING-indigo)](https://github.com/Nakama3942/SortingAlgorithms/blob/main/CONTRIBUTING.md) -[![CODE_OF_CONDUCT](https://img.shields.io/badge/here-CODE_OF_CONDUCT-darkgreen)](https://github.com/Nakama3942/SortingAlgorithms/blob/main/CODE_OF_CONDUCT.md) -[![PULL_REQUEST_TEMPLATE](https://img.shields.io/badge/here-PULL_REQUEST_TEMPLATE-orange)](https://github.com/Nakama3942/SortingAlgorithms/blob/main/.github/PULL_REQUEST_TEMPLATE.md) +[![CHANGELOG](https://img.shields.io/badge/here-CHANGELOG-yellow)](https://github.com/Nakama3942/ALGOR/blob/main/CHANGELOG.md) +[![CONTRIBUTING](https://img.shields.io/badge/here-CONTRIBUTING-indigo)](https://github.com/Nakama3942/ALGOR/blob/main/CONTRIBUTING.md) +[![CODE_OF_CONDUCT](https://img.shields.io/badge/here-CODE_OF_CONDUCT-darkgreen)](https://github.com/Nakama3942/ALGOR/blob/main/CODE_OF_CONDUCT.md) +[![PULL_REQUEST_TEMPLATE](https://img.shields.io/badge/here-PULL_REQUEST_TEMPLATE-orange)](https://github.com/Nakama3942/ALGOR/blob/main/.github/PULL_REQUEST_TEMPLATE.md) -# SortingAlgorithms +# ALGOR ## Overview Library for processing and sorting arrays. Processing means outputting to the array console, finding the maximum and minimum elements, etc. @@ -17,25 +17,37 @@ In search of sorting algorithms and explanations in the form of a code, I often --> ## Usage + +The library is designed for compilation and dynamic linking. However, you can put the files ALGOR.hpp and ALGOR.cpp into your project and just connect the header: +```cpp +#include "ALGOR.hpp" +``` +If you follow the idea, then after compiling the library, in the settings of your project you need to specify the path to the .a-file, and in the source you just connect the header, as shown above. The library itself (.dll or .so) must be placed next to the executable file for it to work. ## Building + +The library is designed for dynamic linking. You must first compile the library before using it. Development was transferred to Qt, however this project remains independent and is written in pure C++. You just need to clone the repository, open the project in Qt and compile the library. You can use another IDE or compile it yourself in the terminal. ## Troubleshooting -All algorithms have been tested by me, but if you have problems using the library, the code does not work, there are suggestions for optimization or advice on improving the style of the code and names - I invite you [here](https://github.com/Nakama3942/SortingAlgorithms/blob/main/CONTRIBUTING.md). +All algorithms have been tested by me, but if you have problems using the library, the code does not work, there are suggestions for optimization or advice on improving the style of the code and names - I invite you [here](https://github.com/Nakama3942/ALGOR/blob/main/CONTRIBUTING.md). ## Authors - - + + + +

Kalynovsky Valentin

Kalynovsky Valentin

"Ideological inspirer and Author"
\ No newline at end of file diff --git a/example.cpp b/example.cpp index 1c55de0..5b7e5c6 100644 --- a/example.cpp +++ b/example.cpp @@ -6,7 +6,7 @@ using std::cout; template void printer(Array *&ARRAY) { - for (int i = 0; i < ARRAY->array_size; i++) + for (unsigned int i = 0; i < ARRAY->array_size; i++) { cout << " " << ARRAY->array[i]; } @@ -20,7 +20,7 @@ int main() //Генерация (заполнение) массива ARRAYDATA *array = new ARRAYDATA(12); - array->generatedData(1, 10); + array->generatedData(1, 100); //Вывод на экран неотсортированного массива ArrayStruct = array->getData(); @@ -28,7 +28,7 @@ int main() //Копирование массива ARRAYDATA *copy_array = new ARRAYDATA(12); - copy(copy_array->getData()->array, array->getData()->array, copy_array->getData()->array_size); + copy_array->cloneData(array); //Сортировка массива ArrayStruct = copy_array->getData(); @@ -52,16 +52,15 @@ int main() printer(ArrayStruct); //Нахождение максимального и минимального элемента - //int min = minimum(array->getData()->array, array->getData()->array_size), max = maximum(copy_array->getData()->array, copy_array->getData()->array_size); - int min = array->getMin(), max = copy_array->getMax(ArrayStatus::SORTED); - cout << "Минимальный элемент: " << min << "; Максимальный: " << max << "\n"; + //cout << "Минимальный элемент: " << minimum(array->getData()->array, array->getData()->array_size) << "; Максимальный: " << maximum(copy_array->getData()->array, copy_array->getData()->array_size) << "\n"; + cout << "Минимальный элемент: " << array->getMin() << "; Максимальный: " << copy_array->getMax(ArrayStatus::SORTED) << "\n"; //Нахождение определённого элемента в неотсортированном массиве try { Array *NumberPoints = array->lenear_searcher(5); cout << "Элемент \"5\" встречается на местах: \n"; - for (int i = 0; i < NumberPoints->array_size; i++) + for (unsigned int i = 0; i < NumberPoints->array_size; i++) { cout << NumberPoints->array[i] + 1 << " "; } @@ -91,7 +90,7 @@ int main() { Array *Occurrence = copy_array->searcherOccurrencesOfSubstring(Sequence); cout << "Последовательность \"5, 6\" встречается на местах: \n"; - for (int i = 0; i < Occurrence->array_size; i++) + for (unsigned int i = 0; i < Occurrence->array_size; i++) { cout << Occurrence->array[i] + 1 << " "; } @@ -118,13 +117,25 @@ int main() int modas_count; Array *Modas = copy_array->modas(modas_count); cout << "Чаще всего встречаются элементы: "; - for (int i = 0; i < Modas->array_size; i++) + for (unsigned int i = 0; i < Modas->array_size; i++) { cout << Modas->array[i] << " "; } cout << ", а именно " << modas_count << " раз.\n"; remove_struct(Modas); + //Тест операторов + *array + 10; + Array *temp = array->getData(); + for (unsigned int i = 7; i < temp->array_size; i++) + { + temp->array[i] = i; + } + printer(temp); + *array && 2; + temp = array->getData(); + printer(temp); + //Освобождение памяти copy_array->remove(); delete (copy_array);