TheAlgorithms/C++ 1.0.0
All the 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 <cstdint>
#include <iostream>
#include <vector>
Include dependency graph for dnf_sort.cpp:

Go to the source code of this file.

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

Definition in file dnf_sort.cpp.

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

Definition at line 39 of file dnf_sort.cpp.

39 {
40 std::vector<T> arr(in_arr);
41 uint64_t lo = 0;
42 uint64_t hi = arr.size() - 1;
43 uint64_t mid = 0;
44
45 // Iterate till all the elements
46 // are sorted
47 while (mid <= hi) {
48 switch (arr[mid]) {
49 // If the element is 0
50 case 0:
51 std::swap(arr[lo++], arr[mid++]);
52 break;
53
54 // If the element is 1 .
55 case 1:
56 mid++;
57 break;
58
59 // If the element is 2
60 case 2:
61 std::swap(arr[mid], arr[hi--]);
62 break;
63 }
64 }
65 return arr;
66}

◆ main()

int main ( void )

Main function.

Returns
0 on exit

Definition at line 109 of file dnf_sort.cpp.

109 {
110 test(); // execute the test
111 return 0;
112}
static void test()
Self-test implementations.
Definition dnf_sort.cpp:74

◆ test()

static void test ( )
static

Self-test implementations.

Returns
void

Definition at line 74 of file dnf_sort.cpp.

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