Sorting Network Verify and Draw
Check Whether Comparator Networks Sort and Draw Them
Bubblesort.cpp
Go to the documentation of this file.
1
3
4// MIT License
5//
6// Copyright (c) 2022 Ian Parberry
7//
8// Permission is hereby granted, free of charge, to any person obtaining a copy
9// of this software and associated documentation files (the "Software"), to deal
10// in the Software without restriction, including without limitation the rights
11// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12// copies of the Software, and to permit persons to whom the Software is
13// furnished to do so, subject to the following conditions:
14//
15// The above copyright notice and this permission notice shall be included in all
16// copies or substantial portions of the Software.
17//
18// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24// SOFTWARE.
25
26#include "Bubblesort.h"
27#include "Helpers.h"
28
30// CBubbleSortMax functions
31
34
36 if(n < 2)return; //safety
37
38 m_nInputs = n;
39 m_nDepth = 2*n - 3;
40
45} //constructor
46
50
52 for(UINT i=0; i<m_nInputs; i++) //for each level, first half
53 for(UINT j=i&1; j<i+1; j+=2) //min-most channel used on this level
54 InsertComparator(i, j, j + 1);
55
56 for(UINT i=0; i<m_nInputs-2; i++){ //for each level, second half
57 const UINT k = (m_nInputs&1)^(i&1);
58
59 for(UINT j=k; j<m_nInputs-i-2; j+=2){ //min-most channel used on this level
60 InsertComparator(i + m_nInputs, j, j + 1);
61 } //for
62 } //for
63
64 ComputeSize();
65} //CreateComparators
66
70
71const std::wstring CBubbleSortMax::GetName() const{
72 return std::wstring(L"BubblesortMax" + std::to_wstring(m_nInputs));
73} //GetName
74
76// CBubbleSortMin functions
77
80
82 if(n < 2)return; //safety
83
84 m_nInputs = n;
85 m_nDepth = 2*n - 3;
86
91} //constructor
92
96
98 for(UINT i=0; i<m_nInputs; i++) //for each level, first half
99 for(UINT j=i&1; j<i+1; j+=2){ //min-most channel used on this level
100 UINT nMin = m_nInputs - j - 2;
101 InsertComparator(i, nMin, nMin + 1);
102 } //for
103
104 for(UINT i=0; i<m_nInputs-2; i++){ //for each level, second half
105 const UINT k = (m_nInputs&1)^(i&1);
106
107 for(UINT j=k; j<m_nInputs-i-2; j+=2){ //min-most channel used on this level
108 UINT nMin = m_nInputs - j - 2;
109 InsertComparator(i + m_nInputs, nMin, nMin + 1);
110 } //for
111 } //for
112
113 ComputeSize();
114} //CreateComparators
115
119
120const std::wstring CBubbleSortMin::GetName() const{
121 return std::wstring(L"BubblesortMin" + std::to_wstring(m_nInputs));
122} //GetName
123
125// CBubbleSort functions
126
129
131 if(n < 2)return; //safety
132
133 m_nInputs = n;
134 m_nDepth = (n == 2)? 1: n;
135
140} //constructor
141
145
147 for(UINT i=0; i<m_nInputs; i++) //for each level, first half
148 for(UINT j=i&1; j<m_nInputs - 1; j+=2) //min-most channel used on this level
149 InsertComparator(i, j, j + 1);
150
151 ComputeSize();
152} //CreateComparators
153
157
158const std::wstring CBubbleSort::GetName() const{
159 return std::wstring(L"Bubblesort" + std::to_wstring(m_nInputs));
160} //GetName
Interface for the bubblesort sorting network.
Header for useful helper functions.
const std::wstring GetName() const
Get name.
Definition: Bubblesort.cpp:158
void CreateComparators()
Create comparators.
Definition: Bubblesort.cpp:146
CBubbleSort(const UINT)
Constructor.
Definition: Bubblesort.cpp:130
CBubbleSortMax(const UINT)
Constructor.
Definition: Bubblesort.cpp:35
const std::wstring GetName() const
Get name.
Definition: Bubblesort.cpp:71
void CreateComparators()
Create comparators.
Definition: Bubblesort.cpp:51
CBubbleSortMin(const UINT)
Constructor.
Definition: Bubblesort.cpp:81
void CreateComparators()
Create comparators.
Definition: Bubblesort.cpp:97
const std::wstring GetName() const
Get name.
Definition: Bubblesort.cpp:120
void InsertComparator(UINT, UINT, UINT)
Insert comparator.
void ComputeSize()
Compute size.
UINT m_nInputs
Number of inputs.
void CreateMatchArray(UINT, UINT)
Create match array.
void CreateUsageArray()
Make usage array.
void CreateValueArray()
Make value array.