|
| 1 | +/** |
| 2 | + * @file |
| 3 | + * @brief Implementation of the [DNF |
| 4 | + * sort](https://www.geeksforgeeks.org/sort-an-array-of-0s-1s-and-2s/) implementation |
| 5 | + * @details |
| 6 | + * C++ program to sort an array with 0, 1 and 2 in a single pass(DNF sort). |
| 7 | + * Since one traversal of the array is there hence it works in O(n) time |
| 8 | + * complexity. |
| 9 | + * @author [Sujal Gupta](https://github.com/heysujal) |
| 10 | + */ |
| 11 | + |
| 12 | +#include <algorithm> /// for std::is_sorted |
| 13 | +#include <cassert> /// for assert |
| 14 | +#include <iostream> /// for std::swap and io operations |
| 15 | +#include <vector> /// for std::vector |
| 16 | + |
| 17 | +/** |
| 18 | + * @namespace sorting |
| 19 | + * @breif Sorting algorithms |
| 20 | + */ |
| 21 | +namespace sorting { |
| 22 | +/** |
| 23 | + * @namespace dnf_sort |
| 24 | + * @brief Functions for the [DNF |
| 25 | + * sort](https://en.wikipedia.org/wiki/Dutch_national_flag_problem) implementation |
| 26 | + */ |
| 27 | +namespace dnf_sort { |
| 28 | +/** |
| 29 | + * @brief The main function implements DNF sort |
| 30 | + * @tparam T type of array |
| 31 | + * @param a array to be sorted, |
| 32 | + * @param arr_size size of array |
| 33 | + * @returns void |
| 34 | + */ |
| 35 | +template <typename T> |
| 36 | +std::vector<T> dnfSort(const std::vector<T> &in_arr) { |
| 37 | + std::vector<T> arr(in_arr); |
| 38 | + uint64_t lo = 0; |
| 39 | + uint64_t hi = arr.size() - 1; |
| 40 | + uint64_t mid = 0; |
| 41 | + |
| 42 | + // Iterate till all the elements |
| 43 | + // are sorted |
| 44 | + while (mid <= hi) { |
| 45 | + switch (arr[mid]) { |
| 46 | + // If the element is 0 |
| 47 | + case 0: |
| 48 | + std::swap(arr[lo++], arr[mid++]); |
| 49 | + break; |
| 50 | + |
| 51 | + // If the element is 1 . |
| 52 | + case 1: |
| 53 | + mid++; |
| 54 | + break; |
| 55 | + |
| 56 | + // If the element is 2 |
| 57 | + case 2: |
| 58 | + std::swap(arr[mid], arr[hi--]); |
| 59 | + break; |
| 60 | + } |
| 61 | + } |
| 62 | + return arr; |
| 63 | +} |
| 64 | +} // namespace dnf_sort |
| 65 | +} // namespace sorting |
| 66 | + |
| 67 | +/** |
| 68 | + * @brief Self-test implementations |
| 69 | + * @returns void |
| 70 | + */ |
| 71 | +static void test() { |
| 72 | + // 1st test |
| 73 | + // [1, 0, 2, 1] return [0, 1, 1, 2] |
| 74 | + std::vector<uint64_t> array1 = {0, 1, 1, 2}; |
| 75 | + std::cout << "Test 1... "; |
| 76 | + std::vector<uint64_t> arr1 = sorting::dnf_sort::dnfSort(array1); |
| 77 | + assert(std::is_sorted(std::begin(arr1), std::end(arr1))); |
| 78 | + std::cout << "passed" << std::endl; |
| 79 | + // 2nd test |
| 80 | + // [1, 0, 0, 1, 1, 0, 2, 1] return [0, 0, 0, 1, 1, 1, 1, 2] |
| 81 | + std::vector<uint64_t> array2 = {1, 0, 0, 1, 1, 0, 2, 1}; |
| 82 | + std::cout << "Test 2... "; |
| 83 | + std::vector<uint64_t> arr2 = sorting::dnf_sort::dnfSort(array2); |
| 84 | + assert(std::is_sorted(std::begin(arr2), std::end(arr2))); |
| 85 | + std::cout << "passed" << std::endl; |
| 86 | + // 3rd test |
| 87 | + // [1, 1, 0, 0, 1, 2, 2, 0, 2, 1] return [0, 0, 0, 1, 1, 1, 1, 2, 2, 2] |
| 88 | + std::vector<uint64_t> array3 = {1, 1, 0, 0, 1, 2, 2, 0, 2, 1}; |
| 89 | + std::cout << "Test 3... "; |
| 90 | + std::vector<uint64_t> arr3 = sorting::dnf_sort::dnfSort(array3); |
| 91 | + assert(std::is_sorted(std::begin(arr3), std::end(arr3))); |
| 92 | + std::cout << "passed" << std::endl; |
| 93 | + // 4th test |
| 94 | + // [2, 2, 2, 0, 0, 1, 1] return [0, 0, 1, 1, 2, 2, 2] |
| 95 | + std::vector<uint64_t> array4 = {2, 2, 2, 0, 0, 1, 1}; |
| 96 | + std::cout << "Test 4... "; |
| 97 | + std::vector<uint64_t> arr4 = sorting::dnf_sort::dnfSort(array4); |
| 98 | + assert(std::is_sorted(std::begin(arr4), std::end(arr4))); |
| 99 | + std::cout << "passed" << std::endl; |
| 100 | +} |
| 101 | + |
| 102 | +/** |
| 103 | + * @brief Main function |
| 104 | + * @returns 0 on exit |
| 105 | + */ |
| 106 | +int main() { |
| 107 | + test(); // execute the test |
| 108 | + return 0; |
| 109 | +} |
0 commit comments