未验证 提交 66dcc4c3 编写于 作者: L Lownish Rai Sookha 提交者: GitHub

feat: add Pigeonhole algorithm (#1028)

* feat: add Pigeonhole algorithm

* Executed clang-format

* Used pointers and vector

* Corrected clang-tidy issues

* Modified code structure

* Apply suggestions from code review

Suggested changes applied
Co-authored-by: NDavid Leal <halfpacho@gmail.com>

* Added missing parameter and documentation

* Added delete function

* Update pigeonhole_sort.cpp

* Corrected delete function
Co-authored-by: NDavid Leal <halfpacho@gmail.com>

* Apply suggestions from code review
Co-authored-by: NDavid Leal <halfpacho@gmail.com>

* Apply suggestions from code review
Co-authored-by: NDavid Leal <halfpacho@gmail.com>

* Changed documentation regarding array size

* clang-tidy

* Apply suggestions from code review
Co-authored-by: NDavid Leal <halfpacho@gmail.com>

* assert moved to test function

* Update pigeonhole_sort.cpp

* Update sorting/pigeonhole_sort.cpp
Co-authored-by: NDavid Leal <halfpacho@gmail.com>

* Added test function and inbuilt min function

* min and max to const variables

* const int* to auto

* const int* to auto

* Apply suggestions from code review
Co-authored-by: NKrishna Vedala <7001608+kvedala@users.noreply.github.com>

* Modified pigeonSort documentation

* Corrected test functions
Co-authored-by: Nlsurface <lownish@hotmail.com>
Co-authored-by: NDavid Leal <halfpacho@gmail.com>
Co-authored-by: NKrishna Vedala <7001608+kvedala@users.noreply.github.com>
上级 2882b7be
/**
* @file
* @brief Implementation of [Pigeonhole Sort algorithm]
* (https://en.wikipedia.org/wiki/Pigeonhole_sort)
* @author [Lownish](https://github.com/Lownish)
* @details
* Pigeonhole sorting is a sorting algorithm that is suitable for sorting lists
* of elements where the number of elements and the number of possible key
* values are approximately the same. It requires O(n + Range) time where n is
* number of elements in input array and ‘Range’ is number of possible values in
* array.
*
* The time Complexity of the algorithm is \f$O(n+N)\f$.
*/
#include <algorithm> //for std::is_sorted
#include <array> //for std::array
#include <cassert> //for assert
#include <iostream> //for io operations
/**
* @namespace sorting
* @brief Sorting algorithms
*/
namespace sorting {
/**
* Pigeonhole sorting of array of size n
* The function will sort the array through Pigeonhole algorithm and print
* @param arr unsorted array of elements
* @returns sorted array of elements
*/
template <std::size_t N>
std::array<int, N> pigeonSort(std::array<int, N> arr) {
// Finding min and max*
auto min = std::min_element(std::begin(arr), std::end(arr));
auto max = std::max_element(std::begin(arr), std::end(arr));
// Range refers to the number of holes required
int range = *max - *min + 1;
int *hole = new int[range]();
// Copying all array values to pigeonhole
for (int i = 0; i < N; i++) {
hole[arr[i] - *min] = arr[i];
}
// Deleting elements from list and storing to original array
int count = 0;
for (int i = 0; i < range; i++) {
while (hole[i] != '\0') {
arr[count] = hole[i];
hole[i] = {};
count++;
}
}
delete[] hole;
return arr;
}
} // namespace sorting
/**
* Test function 1 with unsorted array
* {8, 3, 2, 7, 4, 6, 8}
* @returns none
*/
static void test_1() {
const int n = 7;
std::array<int, n> test_array = {8, 3, 2, 7, 4, 6, 8};
test_array = sorting::pigeonSort<n>(test_array);
assert(std::is_sorted(std::begin(test_array), std::end(test_array)));
// Printing sorted array
for (int i = 0; i < n; i++) {
std::cout << test_array.at(i) << " ";
}
std::cout << "\nPassed\n";
}
/**
* Test function 2 with unsorted array
* {802, 630, 20, 745, 52, 300, 612, 932, 78, 187}
* @returns none
*/
static void test_2() {
const int n = 10;
std::array<int, n> test_array = {802, 630, 20, 745, 52,
300, 612, 932, 78, 187};
test_array = sorting::pigeonSort<n>(test_array);
assert(std::is_sorted(std::begin(test_array), std::end(test_array)));
// Printing sorted array
for (int i = 0; i < n; i++) {
std::cout << test_array.at(i) << " ";
}
std::cout << "\nPassed\n";
}
/**
* Test function 1 with unsorted array
* {11,13,12,14}
* @returns none
*/
static void test_3() {
const int n = 4;
std::array<int, n> test_array = {11, 13, 12, 14};
test_array = sorting::pigeonSort<n>(test_array);
assert(std::is_sorted(std::begin(test_array), std::end(test_array)));
// Printing sorted array
for (int i = 0; i < n; i++) {
std::cout << test_array.at(i) << " ";
}
std::cout << "\nPassed\n";
}
/**
* Main function
*/
int main() {
test_1();
test_2();
test_3();
return 0;
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册