# Algorithms_in_C++: sorting/dnf_sort.cpp File Reference

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. 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

  T | type of array

Parameters

  a | array to be sorted,
  arr_size | size of array

Returns
  void

{
std::vector<T> arr(in_arr);
uint64_t lo = 0;
uint64_t hi = arr.size() - 1;
uint64_t mid = 0;
// Iterate till all the elements
// are sorted
while (mid <= hi) {
switch (arr[mid]) {
// If the element is 0
case 0:
std::swap(arr[lo++], arr[mid++]);
break;
// If the element is 1 .
case 1:
mid++;
break;
// If the element is 2
case 2:
std::swap(arr[mid], arr[hi--]);
break;
}
}
return arr;
}
std::swap
T swap(T... args)
std::vector
Here is the call graph for this function:

## main()

int main | ( | void |  | ) |

Main function.

Returns
  0 on exit

{
test(); // execute the test
return 0;
}
test
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

{
// 1st test
// [1, 0, 2, 1] return [0, 1, 1, 2]
std::vector<uint64_t> array1 = {0, 1, 1, 2};
std::cout << "Test 1... ";
std::vector<uint64_t> arr1 = sorting::dnf_sort::dnfSort(array1);
assert(std::is_sorted(std::begin(arr1), std::end(arr1)));
std::cout << "passed" << std::endl;
// 2nd test
// [1, 0, 0, 1, 1, 0, 2, 1] return [0, 0, 0, 1, 1, 1, 1, 2]
std::vector<uint64_t> array2 = {1, 0, 0, 1, 1, 0, 2, 1};
std::cout << "Test 2... ";
std::vector<uint64_t> arr2 = sorting::dnf_sort::dnfSort(array2);
assert(std::is_sorted(std::begin(arr2), std::end(arr2)));
std::cout << "passed" << std::endl;
// 3rd test
// [1, 1, 0, 0, 1, 2, 2, 0, 2, 1] return [0, 0, 0, 1, 1, 1, 1, 2, 2, 2]
std::vector<uint64_t> array3 = {1, 1, 0, 0, 1, 2, 2, 0, 2, 1};
std::cout << "Test 3... ";
std::vector<uint64_t> arr3 = sorting::dnf_sort::dnfSort(array3);
assert(std::is_sorted(std::begin(arr3), std::end(arr3)));
std::cout << "passed" << std::endl;
// 4th test
// [2, 2, 2, 0, 0, 1, 1] return [0, 0, 1, 1, 2, 2, 2]
std::vector<uint64_t> array4 = {2, 2, 2, 0, 0, 1, 1};
std::cout << "Test 4... ";
std::vector<uint64_t> arr4 = sorting::dnf_sort::dnfSort(array4);
assert(std::is_sorted(std::begin(arr4), std::end(arr4)));
std::cout << "passed" << std::endl;
}
std::cout
std::begin
T begin(T... args)
sorting::dnf_sort::dnfSort
std::vector< T > dnfSort(const std::vector< T > &in_arr)
The main function implements DNF sort.
Definition: dnf_sort.cpp:38
std::end
T end(T... args)
std::endl
T endl(T... args)
std::is_sorted
T is_sorted(T... args)
Here is the call graph for this function: