Skip to content

Commit ef223cf

Browse files
committed
add: selection sort
1 parent 303bef2 commit ef223cf

File tree

2 files changed

+96
-1
lines changed

2 files changed

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

src/main.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <SortingAlgorithmVisualizer/Sorters/BubbleSorter.hpp>
99
#include <SortingAlgorithmVisualizer/Sorters/CocktailSorter.hpp>
1010
#include <SortingAlgorithmVisualizer/Sorters/InsertionSorter.hpp>
11+
#include <SortingAlgorithmVisualizer/Sorters/SelectionSorter.hpp>
1112
#include <SortingAlgorithmVisualizer/Allocators/ArenaAllocator.hpp>
1213

1314

@@ -22,25 +23,28 @@ WinMain(
2223
LPSTR cmdArgs,
2324
int windowShowState )
2425
{
25-
const size_t plotCount = 4;
26+
const size_t plotCount = 5;
2627

2728
const char* plotTitles[] =
2829
{
2930
"Pseudo sort (PLACEHOLDER)",
3031
"Bubble sort",
3132
"Cocktail sort",
3233
"Insertion sort",
34+
"Selection sort",
3335
};
3436

3537
using Sorter1Type = MockSorter <float>;
3638
using Sorter2Type = BubbleSorter <int>;
3739
using Sorter3Type = CocktailSorter <int>;
3840
using Sorter4Type = InsertionSorter <int>;
41+
using Sorter5Type = SelectionSorter <int>;
3942

4043
const size_t plot1ValueCount = 20;
4144
const size_t plot2ValueCount = 20;
4245
const size_t plot3ValueCount = 20;
4346
const size_t plot4ValueCount = 20;
47+
const size_t plot5ValueCount = 20;
4448

4549
const size_t callbackStackDepth = 2;
4650

@@ -54,10 +58,12 @@ WinMain(
5458
sizeof(Sorter2Type) +
5559
sizeof(Sorter3Type) +
5660
sizeof(Sorter4Type) +
61+
sizeof(Sorter5Type) +
5762
Sorter1Type::HeapMemoryBudget(plot1ValueCount) +
5863
Sorter2Type::HeapMemoryBudget(plot2ValueCount) +
5964
Sorter3Type::HeapMemoryBudget(plot3ValueCount) +
6065
Sorter4Type::HeapMemoryBudget(plot4ValueCount) +
66+
Sorter5Type::HeapMemoryBudget(plot5ValueCount) +
6167

6268
// allocation bookkeeping
6369
sizeof(IAllocator*) * 9 +
@@ -119,6 +125,7 @@ WinMain(
119125
backend->addSorter <Sorter2Type> (1, plot2ValueCount);
120126
backend->addSorter <Sorter3Type> (2, plot3ValueCount);
121127
backend->addSorter <Sorter4Type> (3, plot4ValueCount);
128+
backend->addSorter <Sorter5Type> (4, plot5ValueCount);
122129

123130

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

0 commit comments

Comments
 (0)