Skip to content

Commit e48dfc1

Browse files
committed
Push sourcecodes before release
1 parent 04fec5f commit e48dfc1

31 files changed

+1230
-20
lines changed

Algorithms/BubbleSort.hpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#ifndef BUBBLE_SORT_N_HEADER
2+
#define BUBBLE_SORT_N_HEADER
3+
#include "..\header\ArrayManager.hpp"
4+
#include "..\header\BarManager.hpp"
5+
#include <SFML/Graphics/Color.hpp>
6+
#include <thread>
7+
#include <chrono>
8+
9+
void BubbleSort(ArrayManager& arrayManager, BarManager& barManager, int delay);
10+
11+
#endif // !BUBBLE_SORT_N_HEADER

Algorithms/FisherYatesShuffle.hpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#ifndef FISHER_YATES_N_HEADER
2+
#define FISHER_YATES_N_HEADER
3+
#include <random>
4+
#include <SFML/Graphics/Color.hpp>
5+
#include <thread>
6+
#include <chrono>
7+
#include "..\header\ArrayManager.hpp"
8+
#include "..\header\BarManager.hpp"
9+
10+
void FisherYates(ArrayManager& arrayManager, BarManager& barManager, int delay);
11+
12+
#endif // !FISHER_YATES_N_HEADER

Algorithms/HoareQuickSort.hpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#ifndef HOARE_QUICK_SORT_N_HEADER
2+
#define HOARE_QUICK_SORT_N_HEADER
3+
4+
#include "..\header\ArrayManager.hpp"
5+
#include "..\header\BarManager.hpp"
6+
#include <SFML/Graphics/Color.hpp>
7+
#include <thread>
8+
#include <chrono>
9+
inline void Swap(int& a, int& b)
10+
{
11+
int tmp = a;
12+
a = b;
13+
b = tmp;
14+
}
15+
16+
void HoareQuickSortFunc(int* array, int left, int right, BarManager& barManager, int delay);
17+
18+
void HoareQuickSort(ArrayManager& arrayManager, BarManager& barManager, int delay);
19+
20+
#endif // !HOARE_QUICK_SORT_N_HEADER

Algorithms/Implemetnt/BubbleSort.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#include "..\BubbleSort.hpp"
2+
3+
void BubbleSort(ArrayManager& arrayManager, BarManager& barManager, int delay)
4+
{
5+
int* array = arrayManager.getArray();
6+
for (int i = 0; i < arrayManager.getArrayCount(); i++)
7+
{
8+
for (int k = 0; k < arrayManager.getArrayCount() - 1; k++)
9+
{
10+
barManager.SetColor(k, sf::Color::Red);
11+
barManager.SetColor(k + 1, sf::Color::Red);
12+
13+
if (array[k] > array[k + 1])
14+
{
15+
int tmp = array[k];
16+
array[k] = array[k + 1];
17+
array[k + 1] = tmp;
18+
barManager.SwapBar(k, k + 1);
19+
}
20+
21+
22+
23+
std::this_thread::sleep_for(std::chrono::nanoseconds(delay));
24+
barManager.SetColor(k, sf::Color::White);
25+
barManager.SetColor(k + 1, sf::Color::White);
26+
}
27+
}
28+
}

Algorithms/Implemetnt/FisherYates.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#include "..\FisherYatesShuffle.hpp"
2+
3+
void FisherYates(ArrayManager& arrayManager, BarManager& barManager, int delay)
4+
{
5+
std::random_device rd;
6+
std::mt19937 mt(rd());
7+
std::uniform_int_distribution<int> dist(0, arrayManager.getArrayCount() - 1);
8+
9+
for (int i = 0; i < arrayManager.getArrayCount(); i++)
10+
{
11+
int randNum = dist(mt);
12+
barManager.SetColor(i, sf::Color::Red);
13+
barManager.SetColor(randNum, sf::Color::Red);
14+
int tmp = arrayManager.getArray()[randNum];
15+
arrayManager.getArray()[randNum] = arrayManager.getArray()[i];
16+
arrayManager.getArray()[i] = tmp;
17+
barManager.SwapBar(i, randNum);
18+
barManager.SetColor(i, sf::Color::White);
19+
barManager.SetColor(randNum, sf::Color::White);
20+
if (delay > 0)
21+
{
22+
std::this_thread::sleep_for(std::chrono::nanoseconds(delay));
23+
}
24+
}
25+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
#include "..\HoareQuickSort.hpp"
2+
3+
4+
5+
void HoareQuickSortFunc(int* array, int left, int right, BarManager& barManager, int delay)
6+
{
7+
if (right <= left)
8+
{
9+
return;
10+
}
11+
12+
13+
14+
int pivotIndex = (left+right)/2;//for visualization
15+
int pivotValue = array[pivotIndex];
16+
int low = left, high = right;
17+
18+
while (true/*low <= high*/)
19+
{
20+
barManager.SetColor(low, sf::Color::Blue);
21+
barManager.SetColor(high, sf::Color::Red);
22+
23+
barManager.SetColor(pivotIndex, sf::Color::Green);
24+
25+
while (array[low] < pivotValue)
26+
{
27+
std::this_thread::sleep_for(std::chrono::nanoseconds(delay));
28+
barManager.SetColor(low, sf::Color::White);
29+
barManager.SetColor(pivotIndex, sf::Color::Green);
30+
low++;
31+
barManager.SetColor(low, sf::Color::Blue);
32+
}
33+
34+
while (array[high] > pivotValue)
35+
{
36+
std::this_thread::sleep_for(std::chrono::nanoseconds(delay));
37+
barManager.SetColor(high, sf::Color::White);
38+
barManager.SetColor(pivotIndex, sf::Color::Green);
39+
high--;
40+
barManager.SetColor(high, sf::Color::Red);
41+
}
42+
43+
44+
std::this_thread::sleep_for(std::chrono::nanoseconds(delay));
45+
barManager.SetColor(low, sf::Color::White);
46+
barManager.SetColor(high, sf::Color::White);
47+
barManager.SetColor(pivotIndex, sf::Color::White);
48+
if (low < high)
49+
{
50+
51+
barManager.SwapBar(low, high);
52+
Swap(array[low], array[high]);
53+
54+
if (low == pivotIndex)
55+
{
56+
pivotIndex = high;
57+
}
58+
else if (high == pivotIndex)
59+
{
60+
pivotIndex = low;
61+
}
62+
63+
low++, high--;
64+
}
65+
else
66+
{
67+
break;
68+
}
69+
70+
71+
}
72+
73+
HoareQuickSortFunc(array, left, high,barManager,delay);
74+
HoareQuickSortFunc(array, high + 1, right,barManager,delay);
75+
76+
}
77+
78+
79+
void HoareQuickSort(ArrayManager& arrayManager, BarManager& barManager, int delay)
80+
{
81+
82+
HoareQuickSortFunc(arrayManager.getArray(), 0 , arrayManager.getArrayCount() - 1,barManager,delay);
83+
84+
85+
86+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include "..\InsertionSort.hpp"
2+
3+
void InsertionSort(ArrayManager& arrayManager, BarManager& barManager, int delay)
4+
{
5+
int* array = arrayManager.getArray();
6+
for (int i =1; i < arrayManager.getArrayCount(); i++)
7+
{
8+
for (int k = i; k > 0; k--)
9+
{
10+
barManager.SetColor(k - 1, sf::Color::Red);
11+
barManager.SetColor(k, sf::Color::Red);
12+
if (array[k-1] < array[k])
13+
{
14+
barManager.SetColor(k - 1, sf::Color::White);
15+
barManager.SetColor(k, sf::Color::White);
16+
break;
17+
}
18+
19+
std::this_thread::sleep_for(std::chrono::nanoseconds(delay));
20+
21+
barManager.SetColor(k - 1, sf::Color::White);
22+
barManager.SetColor(k, sf::Color::White);
23+
barManager.SwapBar(k, k - 1);
24+
int tmp = array[k];
25+
array[k] = array[k - 1];
26+
array[k - 1] = tmp;
27+
}
28+
}
29+
}

Algorithms/Implemetnt/MergeSort.cpp

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#include "..\MergeSort.hpp"
2+
3+
void MergeSortFunc(int* array, int size, BarManager& barManager, int delay, int startIndex)
4+
{
5+
if (size <= 1)
6+
{
7+
return;
8+
}
9+
10+
//ºÐÇÒ
11+
int middle = size / 2;
12+
int leftSize = middle, rightSize = size - middle;
13+
14+
int* left = new int[leftSize];
15+
int* right = new int[rightSize];
16+
17+
for (int i = 0; i < leftSize; i++)
18+
{
19+
left[i] = array[i];
20+
}
21+
22+
for (int i = 0; i < rightSize; i++)
23+
{
24+
right[i] = array[i + middle];
25+
}
26+
27+
28+
29+
MergeSortFunc(left, leftSize, barManager, delay, startIndex);
30+
MergeSortFunc(right, rightSize, barManager, delay, startIndex + leftSize);
31+
32+
//ÇÕÄ¡±â
33+
for (int i = 0, leftIndex = 0, rightIndex = 0; i < size; i++)
34+
{
35+
std::this_thread::sleep_for(std::chrono::nanoseconds(delay));
36+
if (leftIndex < leftSize && rightIndex < rightSize)
37+
{
38+
if (left[leftIndex] <= right[rightIndex])
39+
{
40+
array[i] = left[leftIndex];
41+
leftIndex++;
42+
}
43+
else
44+
{
45+
array[i] = right[rightIndex];
46+
rightIndex++;
47+
}
48+
49+
}
50+
else if (leftIndex < leftSize)
51+
{
52+
array[i] = left[leftIndex];
53+
leftIndex++;
54+
}
55+
else if (rightIndex < rightSize)
56+
{
57+
array[i] = right[rightIndex];
58+
rightIndex++;
59+
}
60+
barManager.SetBarPos(startIndex + i, array[i]);
61+
}
62+
63+
64+
delete[] left;
65+
delete[] right;
66+
}
67+
68+
void MergeSort(ArrayManager& arrayManager, BarManager& barManager, int delay)
69+
{
70+
MergeSortFunc(arrayManager.getArray(), arrayManager.getArrayCount(), barManager, delay,0);
71+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#include "..\SelectionSort.hpp"
2+
3+
void SelectionSort(ArrayManager& arrayManager, BarManager& barManager, int delay)
4+
{
5+
int* array = arrayManager.getArray();
6+
7+
for (int i = 0; i < arrayManager.getArrayCount() - 1; i++)
8+
{
9+
int lowestIndex = i;
10+
11+
12+
for (int k = lowestIndex; k < arrayManager.getArrayCount(); k++)
13+
{
14+
if (k != i)
15+
{
16+
barManager.SetColor(k, sf::Color::Red);
17+
}
18+
19+
if (array[k] < array[lowestIndex])
20+
{
21+
22+
if (lowestIndex != i)
23+
{
24+
barManager.SetColor(lowestIndex,sf::Color::White);
25+
}
26+
27+
lowestIndex = k;
28+
}
29+
30+
barManager.SetColor(lowestIndex, sf::Color::Green);
31+
barManager.SetColor(i, sf::Color::Blue);
32+
std::this_thread::sleep_for(std::chrono::nanoseconds(delay));
33+
if (k != i)
34+
{
35+
barManager.SetColor(k, sf::Color::White);
36+
}
37+
38+
39+
}
40+
barManager.SetColor(lowestIndex, sf::Color::White);
41+
barManager.SetColor(i, sf::Color::White);
42+
barManager.SwapBar(i, lowestIndex);
43+
int tmp = array[lowestIndex];
44+
array[lowestIndex] = array[i];
45+
array[i] = tmp;
46+
47+
48+
}
49+
50+
}

Algorithms/InsertionSort.hpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#ifndef INSERTION_SORT_N_HEADER
2+
#define INSERTION_SORT_N_HEADER
3+
4+
#include "..\header\ArrayManager.hpp"
5+
#include "..\header\BarManager.hpp"
6+
#include <SFML/Graphics/Color.hpp>
7+
#include <thread>
8+
#include <chrono>
9+
10+
void InsertionSort(ArrayManager& arrayManager, BarManager& barManager, int delay);
11+
12+
#endif // !INSERTION_SORT_N_HEADER

Algorithms/MergeSort.hpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#ifndef MERGE_SORT_N_HEADER
2+
#define MERGE_SORT_N_HEADER
3+
4+
#include "..\header\ArrayManager.hpp"
5+
#include "..\header\BarManager.hpp"
6+
#include <SFML/Graphics/Color.hpp>
7+
#include <thread>
8+
#include <chrono>
9+
10+
11+
void MergeSortFunc(int* array, int size, BarManager& barManager, int delay, int startIndex);
12+
13+
void MergeSort(ArrayManager& arrayManager, BarManager& barManager, int delay);
14+
15+
#endif // !MERGE_SORT_N_HEADER

Algorithms/SelectionSort.hpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#ifndef SELECTION_SORT_N_HEADER
2+
#define SELECTION_SORT_N_HEADER
3+
#include "..\header\ArrayManager.hpp"
4+
#include "..\header\BarManager.hpp"
5+
#include <SFML/Graphics/Color.hpp>
6+
#include <thread>
7+
#include <chrono>
8+
9+
void SelectionSort(ArrayManager& arrayManager, BarManager& barManager, int delay);
10+
11+
#endif // !SELECTION_SORT_N_HEADER

0 commit comments

Comments
 (0)