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

Implementation of the DNF sort implementation. More...

#include <algorithm>
#include <cassert>
#include <iostream>
#include <vector>
Include dependency graph for dnf_sort.cpp:

Namespaces

namespace  sorting
 for working with vectors
 
namespace  dnf_sort
 Functions for the DNF sort implementation.
 

Functions

template<typename T >
std::vector< T > sorting::dnf_sort::dnfSort (const std::vector< T > &in_arr)
 The main function implements DNF sort.
 
static void test ()
 Self-test implementations.
 
int main ()
 Main function.
 

Detailed Description

Implementation of the DNF sort implementation.

C++ program to sort an array with 0, 1 and 2 in a single pass(DNF sort). Since one traversal of the array is there hence it works in O(n) time complexity.

Author
Sujal Gupta

Function Documentation

◆ dnfSort()

template<typename T >
std::vector< T > sorting::dnf_sort::dnfSort ( const std::vector< T > & in_arr)

The main function implements DNF sort.

Template Parameters
Ttype of array
Parameters
aarray to be sorted,
arr_sizesize of array
Returns
void
38 {
39 std::vector<T> arr(in_arr);
40 uint64_t lo = 0;
41 uint64_t hi = arr.size() - 1;
42 uint64_t mid = 0;
43
44 // Iterate till all the elements
45 // are sorted
46 while (mid <= hi) {
47 switch (arr[mid]) {
48 // If the element is 0
49 case 0:
50 std::swap(arr[lo++], arr[mid++]);
51 break;
52
53 // If the element is 1 .
54 case 1:
55 mid++;
56 break;
57
58 // If the element is 2
59 case 2:
60 std::swap(arr[mid], arr[hi--]);
61 break;
62 }
63 }
64 return arr;
65}
T swap(T... args)
Here is the call graph for this function:

◆ main()

int main ( void )

Main function.

Returns
0 on exit
108 {
109 test(); // execute the test
110 return 0;
111}
static void test()
Self-test implementations.
Definition dnf_sort.cpp:73
Here is the call graph for this function:

◆ test()

static void test ( )
static

Self-test implementations.

Returns
void
73 {
74 // 1st test
75 // [1, 0, 2, 1] return [0, 1, 1, 2]
76 std::vector<uint64_t> array1 = {0, 1, 1, 2};
77 std::cout << "Test 1... ";
79 assert(std::is_sorted(std::begin(arr1), std::end(arr1)));
80 std::cout << "passed" << std::endl;
81 // 2nd test
82 // [1, 0, 0, 1, 1, 0, 2, 1] return [0, 0, 0, 1, 1, 1, 1, 2]
83 std::vector<uint64_t> array2 = {1, 0, 0, 1, 1, 0, 2, 1};
84 std::cout << "Test 2... ";
86 assert(std::is_sorted(std::begin(arr2), std::end(arr2)));
87 std::cout << "passed" << std::endl;
88 // 3rd test
89 // [1, 1, 0, 0, 1, 2, 2, 0, 2, 1] return [0, 0, 0, 1, 1, 1, 1, 2, 2, 2]
90 std::vector<uint64_t> array3 = {1, 1, 0, 0, 1, 2, 2, 0, 2, 1};
91 std::cout << "Test 3... ";
93 assert(std::is_sorted(std::begin(arr3), std::end(arr3)));
94 std::cout << "passed" << std::endl;
95 // 4th test
96 // [2, 2, 2, 0, 0, 1, 1] return [0, 0, 1, 1, 2, 2, 2]
97 std::vector<uint64_t> array4 = {2, 2, 2, 0, 0, 1, 1};
98 std::cout << "Test 4... ";
100 assert(std::is_sorted(std::begin(arr4), std::end(arr4)));
101 std::cout << "passed" << std::endl;
102}
T begin(T... args)
std::vector< T > dnfSort(const std::vector< T > &in_arr)
The main function implements DNF sort.
Definition dnf_sort.cpp:38
T end(T... args)
T endl(T... args)
T is_sorted(T... args)
Here is the call graph for this function: