Algorithms_in_C++  1.0.0
Set of algorithms implemented in C++.
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

 sorting
 Sorting algorithms.
 
 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. More...
 
static void test ()
 Self-test implementations. More...
 
int main ()
 Main function. More...
 

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
36  {
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 }
T swap(T... args)
Here is the call graph for this function:

◆ main()

int main ( void  )

Main function.

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

◆ test()

static void test ( )
static

Self-test implementations.

Returns
void
71  {
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... ";
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... ";
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... ";
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... ";
98  assert(std::is_sorted(std::begin(arr4), std::end(arr4)));
99  std::cout << "passed" << std::endl;
100 }
T begin(T... args)
std::vector< T > dnfSort(const std::vector< T > &in_arr)
The main function implements DNF sort.
Definition: dnf_sort.cpp:36
T end(T... args)
T endl(T... args)
T is_sorted(T... args)
Here is the call graph for this function: