Skip to content

Commit 303bef2

Browse files
committed
add: insertion sort
1 parent 7b8cf2d commit 303bef2

File tree

2 files changed

+121
-2
lines changed

2 files changed

+121
-2
lines changed
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
#pragma once
2+
3+
#include <SortingAlgorithmVisualizer/Sorters/ISorterSpecialized.hpp>
4+
5+
6+
template <typename T>
7+
class InsertionSorter : public ISorterSpecialized <T>
8+
{
9+
size_t mSortedIndex {};
10+
size_t mCurrentIndex {1};
11+
size_t mUnsortedIndex {1};
12+
13+
size_t mSwappedIndices[2] {};
14+
15+
16+
public:
17+
InsertionSorter() = default;
18+
~InsertionSorter() override = default;
19+
20+
21+
inline void reset() override
22+
{
23+
mSortedIndex = 0;
24+
mCurrentIndex = 1;
25+
mUnsortedIndex = 1;
26+
mSwappedIndices[0] = 0;
27+
mSwappedIndices[1] = 0;
28+
resetColors();
29+
}
30+
31+
inline bool step() override
32+
{
33+
using PlotColor = PlotValueColorIndex::PlotValueColorIndex;
34+
35+
if ( this->mValues.size() < 2 )
36+
return true;
37+
38+
39+
auto& values = this->mValues;
40+
auto& colors = this->mColors;
41+
42+
auto& lhsIndex = mSwappedIndices[0];
43+
auto& rhsIndex = mSwappedIndices[1];
44+
45+
colors[lhsIndex] = PlotColor::Sorted;
46+
colors[rhsIndex] = PlotColor::Sorted;
47+
48+
lhsIndex = mSortedIndex;
49+
rhsIndex = mCurrentIndex;
50+
51+
auto& lhs = values[lhsIndex];
52+
auto& rhs = values[rhsIndex];
53+
54+
55+
if ( lhs > rhs )
56+
{
57+
std::swap(lhs, rhs);
58+
59+
colors[lhsIndex] = PlotColor::SwappedLess;
60+
colors[rhsIndex] = PlotColor::SwappedGreater;
61+
62+
if ( mSortedIndex > 0 )
63+
{
64+
--mSortedIndex;
65+
--mCurrentIndex;
66+
}
67+
else
68+
{
69+
mCurrentIndex = ++mUnsortedIndex;
70+
mSortedIndex = mCurrentIndex - 1;
71+
}
72+
}
73+
else
74+
{
75+
if ( mCurrentIndex < mUnsortedIndex )
76+
{
77+
colors[lhsIndex] = PlotColor::Sorted;
78+
colors[rhsIndex] = PlotColor::Sorted;
79+
}
80+
else
81+
{
82+
colors[lhsIndex] = PlotColor::SwappedLess;
83+
colors[rhsIndex] = PlotColor::SwappedGreater;
84+
}
85+
86+
mCurrentIndex = ++mUnsortedIndex;
87+
mSortedIndex = mCurrentIndex - 1;
88+
}
89+
90+
91+
bool isSorted =
92+
mUnsortedIndex == this->mValues.size();
93+
94+
if ( isSorted == true )
95+
{
96+
colors[lhsIndex] = PlotColor::Sorted;
97+
colors[rhsIndex] = PlotColor::Sorted;
98+
}
99+
100+
101+
flushValues();
102+
flushColors();
103+
swapBuffers();
104+
105+
return isSorted;
106+
}
107+
108+
inline size_t instanceSize() const override
109+
{
110+
return sizeof(*this);
111+
}
112+
};

src/main.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <SortingAlgorithmVisualizer/Sorters/MockSorter.hpp>
88
#include <SortingAlgorithmVisualizer/Sorters/BubbleSorter.hpp>
99
#include <SortingAlgorithmVisualizer/Sorters/CocktailSorter.hpp>
10+
#include <SortingAlgorithmVisualizer/Sorters/InsertionSorter.hpp>
1011
#include <SortingAlgorithmVisualizer/Allocators/ArenaAllocator.hpp>
1112

1213

@@ -21,22 +22,25 @@ WinMain(
2122
LPSTR cmdArgs,
2223
int windowShowState )
2324
{
24-
const size_t plotCount = 3;
25+
const size_t plotCount = 4;
2526

2627
const char* plotTitles[] =
2728
{
2829
"Pseudo sort (PLACEHOLDER)",
2930
"Bubble sort",
3031
"Cocktail sort",
32+
"Insertion sort",
3133
};
3234

3335
using Sorter1Type = MockSorter <float>;
3436
using Sorter2Type = BubbleSorter <int>;
3537
using Sorter3Type = CocktailSorter <int>;
38+
using Sorter4Type = InsertionSorter <int>;
3639

3740
const size_t plot1ValueCount = 20;
3841
const size_t plot2ValueCount = 20;
3942
const size_t plot3ValueCount = 20;
43+
const size_t plot4ValueCount = 20;
4044

4145
const size_t callbackStackDepth = 2;
4246

@@ -49,9 +53,11 @@ WinMain(
4953
sizeof(Sorter1Type) +
5054
sizeof(Sorter2Type) +
5155
sizeof(Sorter3Type) +
56+
sizeof(Sorter4Type) +
5257
Sorter1Type::HeapMemoryBudget(plot1ValueCount) +
5358
Sorter2Type::HeapMemoryBudget(plot2ValueCount) +
54-
Sorter3Type::HeapMemoryBudget(plot2ValueCount) +
59+
Sorter3Type::HeapMemoryBudget(plot3ValueCount) +
60+
Sorter4Type::HeapMemoryBudget(plot4ValueCount) +
5561

5662
// allocation bookkeeping
5763
sizeof(IAllocator*) * 9 +
@@ -112,6 +118,7 @@ WinMain(
112118
backend->addSorter <Sorter1Type> (0, plot1ValueCount);
113119
backend->addSorter <Sorter2Type> (1, plot2ValueCount);
114120
backend->addSorter <Sorter3Type> (2, plot3ValueCount);
121+
backend->addSorter <Sorter4Type> (3, plot4ValueCount);
115122

116123

117124
auto frontend = ObjectCreate <Frontend> (arena, arena, *backend);

0 commit comments

Comments
 (0)