Algorithms_in_C++ 1.0.0
Set of algorithms implemented in C++.
Loading...
Searching...
No Matches
pigeonhole_sort.cpp File Reference

Implementation of [Pigeonhole Sort algorithm] (https://en.wikipedia.org/wiki/Pigeonhole_sort) More...

#include <algorithm>
#include <array>
#include <cassert>
#include <iostream>
Include dependency graph for pigeonhole_sort.cpp:

Namespaces

namespace  sorting
 for working with vectors
 

Functions

template<std::size_t N>
std::array< int, N > sorting::pigeonSort (std::array< int, N > arr)
 
static void test_1 ()
 
static void test_2 ()
 
static void test_3 ()
 
int main ()
 

Detailed Description

Implementation of [Pigeonhole Sort algorithm] (https://en.wikipedia.org/wiki/Pigeonhole_sort)

Author
Lownish

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 \(O(n+N)\).

Function Documentation

◆ main()

int main ( void )

Main function

127 {
128 test_1();
129 test_2();
130 test_3();
131
132 return 0;
133}
static void test_1()
Definition pigeonhole_sort.cpp:68
static void test_2()
Definition pigeonhole_sort.cpp:88
static void test_3()
Definition pigeonhole_sort.cpp:109
Here is the call graph for this function:

◆ test_1()

static void test_1 ( )
static

Test function 1 with unsorted array {8, 3, 2, 7, 4, 6, 8}

Returns
none
68 {
69 const int n = 7;
70 std::array<int, n> test_array = {8, 3, 2, 7, 4, 6, 8};
71
72 test_array = sorting::pigeonSort<n>(test_array);
73
74 assert(std::is_sorted(std::begin(test_array), std::end(test_array)));
75
76 // Printing sorted array
77 for (int i = 0; i < n; i++) {
78 std::cout << test_array.at(i) << " ";
79 }
80 std::cout << "\nPassed\n";
81}
T at(T... args)
T begin(T... args)
T end(T... args)
T is_sorted(T... args)
Here is the call graph for this function:

◆ test_2()

static void test_2 ( )
static

Test function 2 with unsorted array {802, 630, 20, 745, 52, 300, 612, 932, 78, 187}

Returns
none
88 {
89 const int n = 10;
90 std::array<int, n> test_array = {802, 630, 20, 745, 52,
91 300, 612, 932, 78, 187};
92
93 test_array = sorting::pigeonSort<n>(test_array);
94
95 assert(std::is_sorted(std::begin(test_array), std::end(test_array)));
96
97 // Printing sorted array
98 for (int i = 0; i < n; i++) {
99 std::cout << test_array.at(i) << " ";
100 }
101 std::cout << "\nPassed\n";
102}
Here is the call graph for this function:

◆ test_3()

static void test_3 ( )
static

Test function 1 with unsorted array {11,13,12,14}

Returns
none
109 {
110 const int n = 4;
111 std::array<int, n> test_array = {11, 13, 12, 14};
112
113 test_array = sorting::pigeonSort<n>(test_array);
114
115 assert(std::is_sorted(std::begin(test_array), std::end(test_array)));
116
117 // Printing sorted array
118 for (int i = 0; i < n; i++) {
119 std::cout << test_array.at(i) << " ";
120 }
121 std::cout << "\nPassed\n";
122}
Here is the call graph for this function: