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+ }
0 commit comments