From 171b57ce61ae5fc64f7db52f44d7ac9ce6b34eb2 Mon Sep 17 00:00:00 2001 From: Tarun Date: Sun, 29 Jun 2025 19:46:43 +0530 Subject: [PATCH 1/7] Solution for Bubble Sort Branch --- .../Collection/StickCollectionController.h | 7 ++ .../Collection/StickCollectionModel.h | 8 +- .../Collection/StickCollectionController.cpp | 94 ++++++++++++++++++- .../UI/MainMenu/MainMenuUIController.cpp | 7 +- 4 files changed, 109 insertions(+), 7 deletions(-) diff --git a/Sorting-Stick/include/Gameplay/Collection/StickCollectionController.h b/Sorting-Stick/include/Gameplay/Collection/StickCollectionController.h index 02ba30c..e15c844 100644 --- a/Sorting-Stick/include/Gameplay/Collection/StickCollectionController.h +++ b/Sorting-Stick/include/Gameplay/Collection/StickCollectionController.h @@ -11,6 +11,7 @@ namespace Gameplay class StickCollectionModel; struct Stick; enum class SortType; + enum class SortingState; class StickCollectionController { @@ -20,6 +21,7 @@ namespace Gameplay std::vector sticks; SortType sort_type; + SortingState sort_State; std::thread sort_thread; @@ -46,6 +48,8 @@ namespace Gameplay bool isCollectionSorted(); void destroy(); + int color_delay; + public: StickCollectionController(); ~StickCollectionController(); @@ -64,6 +68,9 @@ namespace Gameplay int getNumberOfSticks(); int getDelayMilliseconds(); sf::String getTimeComplexity(); + void processBubbleSort(); + + void completedColour(); }; } } \ No newline at end of file diff --git a/Sorting-Stick/include/Gameplay/Collection/StickCollectionModel.h b/Sorting-Stick/include/Gameplay/Collection/StickCollectionModel.h index dbd01c8..89a827c 100644 --- a/Sorting-Stick/include/Gameplay/Collection/StickCollectionModel.h +++ b/Sorting-Stick/include/Gameplay/Collection/StickCollectionModel.h @@ -14,7 +14,10 @@ namespace Gameplay QUICK_SORT, RADIX_SORT, }; - + enum class SortingState { + Sorting, + NotSorting, + }; class StickCollectionModel { public: @@ -29,7 +32,8 @@ namespace Gameplay const sf::Color placement_position_element_color = sf::Color::Green; const sf::Color selected_element_color = sf::Color::Blue; - const long operation_delay = 150; + const long operation_delay = 10; + const int colorDelay = 10; StickCollectionModel(); ~StickCollectionModel(); diff --git a/Sorting-Stick/source/Gameplay/Collection/StickCollectionController.cpp b/Sorting-Stick/source/Gameplay/Collection/StickCollectionController.cpp index 13293eb..e9d210a 100644 --- a/Sorting-Stick/source/Gameplay/Collection/StickCollectionController.cpp +++ b/Sorting-Stick/source/Gameplay/Collection/StickCollectionController.cpp @@ -5,6 +5,7 @@ #include "Global/ServiceLocator.h" #include "Gameplay/Collection/Stick.h" #include +#include"../../include/Sound/SoundService.h" namespace Gameplay { @@ -13,6 +14,7 @@ namespace Gameplay using namespace UI::UIElement; using namespace Global; using namespace Graphics; + using namespace Sound; StickCollectionController::StickCollectionController() { @@ -29,7 +31,9 @@ namespace Gameplay void StickCollectionController::initialize() { + sort_State = SortingState::NotSorting; collection_view->initialize(this); + initializeSticks(); reset(); } @@ -114,7 +118,10 @@ namespace Gameplay void StickCollectionController::processSortThreadState() { - if (sort_thread.joinable() && isCollectionSorted()) sort_thread.join(); + if (sort_thread.joinable() && isCollectionSorted()) { + sort_thread.join(); + sort_State = SortingState::NotSorting; + } } @@ -132,6 +139,8 @@ namespace Gameplay void StickCollectionController::reset() { current_operation_delay = 0; + sort_State = SortingState::NotSorting; + color_delay = 0; if (sort_thread.joinable()) sort_thread.join(); shuffleSticks(); @@ -143,13 +152,15 @@ namespace Gameplay { current_operation_delay = collection_model->operation_delay; this->sort_type = sort_type; - - /*switch (sort_type) + color_delay = collection_model->colorDelay; + sort_State = SortingState::Sorting; + switch (sort_type) { case Gameplay::Collection::SortType::BUBBLE_SORT: + time_complexity = "O(n^2)"; sort_thread = std::thread(&StickCollectionController::processBubbleSort, this); break; - }*/ + } } bool StickCollectionController::isCollectionSorted() @@ -181,6 +192,81 @@ namespace Gameplay int StickCollectionController::getDelayMilliseconds() { return current_operation_delay; } sf::String StickCollectionController::getTimeComplexity() { return time_complexity; } + void StickCollectionController::processBubbleSort() + { + + SoundService* sound = Global::ServiceLocator::getInstance()->getSoundService(); + + for (int j = 0; j < sticks.size(); j++) { // Loop through the sticks array + if (sort_State == SortingState::NotSorting) { break; } // Check if sorting has stopped or been interrupted + + bool swapped = false; // To track if a swap was made + + for (int i = 1; i < sticks.size() - j; i++) { // Loop through the array, reducing the range each pass + if (sort_State == SortingState::NotSorting) { break; } // Check if sorting has stopped or been interrupted + + // Increment the number of array accesses and comparisons + number_of_array_access += 2; + number_of_comparisons++; + + sound->playSound(SoundType::COMPARE_SFX); // Play the compare sound effect + + // Set the current sticks to the processing color + sticks[i - 1]->stick_view->setFillColor(collection_model->processing_element_color); + sticks[i]->stick_view->setFillColor(collection_model->processing_element_color); + + if (sticks[i - 1]->data > sticks[i]->data) { // Check if the current stick is greater than the next stick + // Swap the sticks if necessary + std::swap(sticks[i - 1], sticks[i]); + swapped = true; // Set swapped to true if there was a swap + } + + std::this_thread::sleep_for(std::chrono::milliseconds(current_operation_delay)); + + // Reset the stick colors + sticks[i - 1]->stick_view->setFillColor(collection_model->element_color); + sticks[i]->stick_view->setFillColor(collection_model->element_color); + + updateStickPosition(); + } + + if (sticks.size() - j - 1 >= 0) { // Set the last sorted stick to the placement position color + sticks[sticks.size() - j - 1]->stick_view->setFillColor(collection_model->placement_position_element_color); + } + + if (!swapped) { break; } // If no swaps were made, the array is already sorted + } + + completedColour(); + } + void StickCollectionController::completedColour() + { + + for (int i = 0; i < sticks.size(); i++) + { + if (sort_State == SortingState::NotSorting) { + break; + } + + sticks[i]->stick_view->setFillColor(collection_model->element_color); // Reset to default color if not completed + + + + for (int j = 0; j < sticks.size(); j++) { + + if (sort_State == SortingState::NotSorting) { + + break; + } + + ServiceLocator::getInstance()->getSoundService()->playSound(Sound::SoundType::COMPARE_SFX); + sticks[j]->stick_view->setFillColor(collection_model->placement_position_element_color); + std::this_thread::sleep_for(std::chrono::milliseconds(color_delay)); + + } + + } + } } } diff --git a/Sorting-Stick/source/UI/MainMenu/MainMenuUIController.cpp b/Sorting-Stick/source/UI/MainMenu/MainMenuUIController.cpp index 12338d0..10296e2 100644 --- a/Sorting-Stick/source/UI/MainMenu/MainMenuUIController.cpp +++ b/Sorting-Stick/source/UI/MainMenu/MainMenuUIController.cpp @@ -5,6 +5,7 @@ #include "Event/EventService.h" #include "Global/Config.h" #include "Global/ServiceLocator.h" +#include"../../include/Main/GameService.h" namespace UI { @@ -15,6 +16,7 @@ namespace UI using namespace UIElement; using namespace Sound; using namespace Graphics; + MainMenuUIController::MainMenuUIController() { @@ -73,7 +75,7 @@ namespace UI void MainMenuUIController::registerButtonCallback() { - //bubble_sort_button->registerCallbackFuntion(std::bind(&MainMenuUIController::bubbleSortButtonCallback, this)); + bubble_sort_button->registerCallbackFuntion(std::bind(&MainMenuUIController::bubbleSortButtonCallback, this)); //insertion_sort_button->registerCallbackFuntion(std::bind(&MainMenuUIController::insertionSortButtonCallback, this)); //selection_sort_button->registerCallbackFuntion(std::bind(&MainMenuUIController::selectionSortButtonCallback, this)); //merge_sort_button->registerCallbackFuntion(std::bind(&MainMenuUIController::mergeSortButtonCallback, this)); @@ -85,6 +87,9 @@ namespace UI void MainMenuUIController::bubbleSortButtonCallback() { ServiceLocator::getInstance()->getSoundService()->playSound(SoundType::BUTTON_CLICK); + GameService::setGameState(GameState::GAMEPLAY); + ServiceLocator::getInstance()->getGameplayService()->sortElement(Gameplay::Collection::SortType::BUBBLE_SORT); + } void MainMenuUIController::insertionSortButtonCallback() From a389133f89ff24711e8d25c29750dcfea446fec1 Mon Sep 17 00:00:00 2001 From: Tarun Date: Thu, 3 Jul 2025 16:23:57 +0530 Subject: [PATCH 2/7] Implement Insertion Sort --- .../Collection/StickCollectionController.h | 1 + .../Collection/StickCollectionController.cpp | 46 ++++++++++++++++++- .../UI/MainMenu/MainMenuUIController.cpp | 4 +- 3 files changed, 49 insertions(+), 2 deletions(-) diff --git a/Sorting-Stick/include/Gameplay/Collection/StickCollectionController.h b/Sorting-Stick/include/Gameplay/Collection/StickCollectionController.h index e15c844..aada4ec 100644 --- a/Sorting-Stick/include/Gameplay/Collection/StickCollectionController.h +++ b/Sorting-Stick/include/Gameplay/Collection/StickCollectionController.h @@ -69,6 +69,7 @@ namespace Gameplay int getDelayMilliseconds(); sf::String getTimeComplexity(); void processBubbleSort(); + void processInsertionSort(); void completedColour(); }; diff --git a/Sorting-Stick/source/Gameplay/Collection/StickCollectionController.cpp b/Sorting-Stick/source/Gameplay/Collection/StickCollectionController.cpp index e9d210a..002f69a 100644 --- a/Sorting-Stick/source/Gameplay/Collection/StickCollectionController.cpp +++ b/Sorting-Stick/source/Gameplay/Collection/StickCollectionController.cpp @@ -160,7 +160,13 @@ namespace Gameplay time_complexity = "O(n^2)"; sort_thread = std::thread(&StickCollectionController::processBubbleSort, this); break; + + case Gameplay::Collection::SortType::INSERTION_SORT: + + sort_thread = std::thread(&StickCollectionController::processInsertionSort, this); + break; } + } bool StickCollectionController::isCollectionSorted() @@ -239,6 +245,44 @@ namespace Gameplay completedColour(); } + void StickCollectionController::processInsertionSort() + { + /*ServiceLocator::getInstance()->getSoundService()->playSound(Sound::SoundType::COMPARE_SFX);*/ + + ServiceLocator::getInstance()->getSoundService()->playSound(Sound::SoundType::COMPARE_SFX); + + for (int i = 1; i < sticks.size(); i++) { + if (sort_State == SortingState::NotSorting) { break; } // Check if sorting has stopped or been interrupted + + int j = i - 1; + Stick* key = sticks[i]; + number_of_array_access++; + sticks[i]->stick_view->setFillColor(collection_model->processing_element_color); // Set the key stick to processing color + std::this_thread::sleep_for(std::chrono::milliseconds(current_operation_delay)); + + while (j >= 0 && sticks[j]->data > key->data) { + + if (sort_State == SortingState::NotSorting) { break; } + number_of_array_access++; + number_of_comparisons++; + sticks[j + 1] = sticks[j]; + sticks[j + 1]->stick_view->setFillColor(collection_model->processing_element_color); // Set the current stick to processing color + j--; + updateStickPosition(); + std::this_thread::sleep_for(std::chrono::milliseconds(current_operation_delay)); + + + } + + sticks[j + 1] = key; // Insert the key stick at the correct position + number_of_array_access++; + sticks[j + 1]->stick_view->setFillColor(collection_model->processing_element_color); // Set the inserted stick to placement position color + updateStickPosition(); + std::this_thread::sleep_for(std::chrono::milliseconds(current_operation_delay)); + sticks[j + 1]->stick_view->setFillColor(collection_model->selected_element_color); // Set the inserted stick to placement position color + } + completedColour(); + } void StickCollectionController::completedColour() { @@ -259,7 +303,7 @@ namespace Gameplay break; } - ServiceLocator::getInstance()->getSoundService()->playSound(Sound::SoundType::COMPARE_SFX); + /*ServiceLocator::getInstance()->getSoundService()->playSound(Sound::SoundType::COMPARE_SFX);*/ sticks[j]->stick_view->setFillColor(collection_model->placement_position_element_color); std::this_thread::sleep_for(std::chrono::milliseconds(color_delay)); diff --git a/Sorting-Stick/source/UI/MainMenu/MainMenuUIController.cpp b/Sorting-Stick/source/UI/MainMenu/MainMenuUIController.cpp index 10296e2..78ee012 100644 --- a/Sorting-Stick/source/UI/MainMenu/MainMenuUIController.cpp +++ b/Sorting-Stick/source/UI/MainMenu/MainMenuUIController.cpp @@ -76,7 +76,7 @@ namespace UI void MainMenuUIController::registerButtonCallback() { bubble_sort_button->registerCallbackFuntion(std::bind(&MainMenuUIController::bubbleSortButtonCallback, this)); - //insertion_sort_button->registerCallbackFuntion(std::bind(&MainMenuUIController::insertionSortButtonCallback, this)); + insertion_sort_button->registerCallbackFuntion(std::bind(&MainMenuUIController::insertionSortButtonCallback, this)); //selection_sort_button->registerCallbackFuntion(std::bind(&MainMenuUIController::selectionSortButtonCallback, this)); //merge_sort_button->registerCallbackFuntion(std::bind(&MainMenuUIController::mergeSortButtonCallback, this)); //quick_sort_button->registerCallbackFuntion(std::bind(&MainMenuUIController::quickSortButtonCallback, this)); @@ -95,6 +95,8 @@ namespace UI void MainMenuUIController::insertionSortButtonCallback() { ServiceLocator::getInstance()->getSoundService()->playSound(SoundType::BUTTON_CLICK); + GameService::setGameState(GameState::GAMEPLAY); + ServiceLocator::getInstance()->getGameplayService()->sortElement(Gameplay::Collection::SortType::INSERTION_SORT); } void MainMenuUIController::selectionSortButtonCallback() From 49a1ed5dd2cb139785306baec287bb4ab9d3ab1a Mon Sep 17 00:00:00 2001 From: Tarun Date: Thu, 3 Jul 2025 19:47:53 +0530 Subject: [PATCH 3/7] Implement Selection Sort --- .../Collection/StickCollectionController.h | 1 + .../Collection/StickCollectionController.cpp | 54 +++++++++++++++++++ .../UI/MainMenu/MainMenuUIController.cpp | 4 +- 3 files changed, 58 insertions(+), 1 deletion(-) diff --git a/Sorting-Stick/include/Gameplay/Collection/StickCollectionController.h b/Sorting-Stick/include/Gameplay/Collection/StickCollectionController.h index aada4ec..3a84540 100644 --- a/Sorting-Stick/include/Gameplay/Collection/StickCollectionController.h +++ b/Sorting-Stick/include/Gameplay/Collection/StickCollectionController.h @@ -70,6 +70,7 @@ namespace Gameplay sf::String getTimeComplexity(); void processBubbleSort(); void processInsertionSort(); + void processSelectionSort(); void completedColour(); }; diff --git a/Sorting-Stick/source/Gameplay/Collection/StickCollectionController.cpp b/Sorting-Stick/source/Gameplay/Collection/StickCollectionController.cpp index 002f69a..1f92b68 100644 --- a/Sorting-Stick/source/Gameplay/Collection/StickCollectionController.cpp +++ b/Sorting-Stick/source/Gameplay/Collection/StickCollectionController.cpp @@ -165,7 +165,13 @@ namespace Gameplay sort_thread = std::thread(&StickCollectionController::processInsertionSort, this); break; + + case Gameplay::Collection::SortType::SELECTION_SORT: + sort_thread = std::thread(&StickCollectionController::processSelectionSort, this); + break; } + + } @@ -283,6 +289,54 @@ namespace Gameplay } completedColour(); } + void StickCollectionController::processSelectionSort() + { + for (int i = 0; i < sticks.size() - 1; i++) { + + if (sort_State == SortingState::NotSorting) { break; } // Check if sorting has stopped or been interrupted + + int min = i; + sticks[i]->stick_view->setFillColor(collection_model->selected_element_color); + number_of_array_access++; + + for (int j = i; j < sticks.size(); j++) { + if (sort_State == SortingState::NotSorting) { break; } + number_of_array_access++; + number_of_comparisons++; + sticks[j]->stick_view->setFillColor(collection_model->processing_element_color); + std::this_thread::sleep_for(std::chrono::milliseconds(current_operation_delay)); + if (sticks[j]->data < sticks[min]->data) { + sticks[min]->stick_view->setFillColor(collection_model->element_color); + min = j; + sticks[min]->stick_view->setFillColor(collection_model->processing_element_color); + + } + else { + sticks[j]->stick_view->setFillColor(collection_model->element_color); + } + + number_of_array_access += 3; + + + sticks[i]->stick_view->setFillColor(collection_model->placement_position_element_color); // Mark as sorted + updateStickPosition(); + + } + + number_of_array_access += 3; + std::swap(sticks[min], sticks[i]); // Place the found minimum at its final position + + sticks[i]->stick_view->setFillColor(collection_model->placement_position_element_color); // Mark as sorted + updateStickPosition(); + + } + + + // Ensure the last stick is also marked as sorted + sticks[sticks.size() - 1]->stick_view->setFillColor(collection_model->placement_position_element_color); + completedColour(); + + } void StickCollectionController::completedColour() { diff --git a/Sorting-Stick/source/UI/MainMenu/MainMenuUIController.cpp b/Sorting-Stick/source/UI/MainMenu/MainMenuUIController.cpp index 78ee012..968697c 100644 --- a/Sorting-Stick/source/UI/MainMenu/MainMenuUIController.cpp +++ b/Sorting-Stick/source/UI/MainMenu/MainMenuUIController.cpp @@ -77,7 +77,7 @@ namespace UI { bubble_sort_button->registerCallbackFuntion(std::bind(&MainMenuUIController::bubbleSortButtonCallback, this)); insertion_sort_button->registerCallbackFuntion(std::bind(&MainMenuUIController::insertionSortButtonCallback, this)); - //selection_sort_button->registerCallbackFuntion(std::bind(&MainMenuUIController::selectionSortButtonCallback, this)); + selection_sort_button->registerCallbackFuntion(std::bind(&MainMenuUIController::selectionSortButtonCallback, this)); //merge_sort_button->registerCallbackFuntion(std::bind(&MainMenuUIController::mergeSortButtonCallback, this)); //quick_sort_button->registerCallbackFuntion(std::bind(&MainMenuUIController::quickSortButtonCallback, this)); //radix_sort_button->registerCallbackFuntion(std::bind(&MainMenuUIController::radixSortButtonCallback, this)); @@ -102,6 +102,8 @@ namespace UI void MainMenuUIController::selectionSortButtonCallback() { ServiceLocator::getInstance()->getSoundService()->playSound(SoundType::BUTTON_CLICK); + GameService::setGameState(GameState::GAMEPLAY); + ServiceLocator::getInstance()->getGameplayService()->sortElement(Gameplay::Collection::SortType::SELECTION_SORT); } void MainMenuUIController::mergeSortButtonCallback() From 073e3d9a4981ecee466f2fecdabff3f6e1be4700 Mon Sep 17 00:00:00 2001 From: Tarun Date: Sun, 6 Jul 2025 16:26:23 +0530 Subject: [PATCH 4/7] inplace merge --- .../Collection/StickCollectionController.h | 7 +- .../Collection/StickCollectionController.cpp | 70 +++++++++++++++++++ .../UI/MainMenu/MainMenuUIController.cpp | 4 +- 3 files changed, 79 insertions(+), 2 deletions(-) diff --git a/Sorting-Stick/include/Gameplay/Collection/StickCollectionController.h b/Sorting-Stick/include/Gameplay/Collection/StickCollectionController.h index 3a84540..0917bc6 100644 --- a/Sorting-Stick/include/Gameplay/Collection/StickCollectionController.h +++ b/Sorting-Stick/include/Gameplay/Collection/StickCollectionController.h @@ -71,7 +71,12 @@ namespace Gameplay void processBubbleSort(); void processInsertionSort(); void processSelectionSort(); - + void processMergeSort(); + void InPlaceMerge(int left, int mid, int right); + void MergeSort(int left, int right); + + + void completedColour(); }; } diff --git a/Sorting-Stick/source/Gameplay/Collection/StickCollectionController.cpp b/Sorting-Stick/source/Gameplay/Collection/StickCollectionController.cpp index 1f92b68..1e0a4fe 100644 --- a/Sorting-Stick/source/Gameplay/Collection/StickCollectionController.cpp +++ b/Sorting-Stick/source/Gameplay/Collection/StickCollectionController.cpp @@ -169,6 +169,10 @@ namespace Gameplay case Gameplay::Collection::SortType::SELECTION_SORT: sort_thread = std::thread(&StickCollectionController::processSelectionSort, this); break; + + case Gameplay::Collection::SortType::MERGE_SORT: + sort_thread = std::thread(&StickCollectionController::processMergeSort, this); + break; } @@ -337,6 +341,72 @@ namespace Gameplay completedColour(); } + + void StickCollectionController::processMergeSort() + { + MergeSort(0, sticks.size() - 1); + completedColour(); + + } + + void StickCollectionController::InPlaceMerge(int left, int mid, int right) + { + int i = left; + int j = mid + 1; + + while (i <= mid && j <= right) { + number_of_comparisons++; + number_of_array_access += 2; + + + + if (sticks[i]->data <= sticks[j]->data) { + + i++; + } + else { + // Save the smaller value + Stick* value = sticks[j]; + int index = j; + + // Shift elements rightward + while (index!=i) { + sticks[index] = sticks[index - 1]; + index--; + number_of_array_access += 2; + } + + // Put value at correct place + sticks[i] = value; + number_of_array_access += 2; + + i++; + mid++; + j++; + updateStickPosition(); + } + + sticks[i- 1]->stick_view->setFillColor(collection_model->processing_element_color); + std::this_thread::sleep_for(std::chrono::milliseconds(current_operation_delay)); + sticks[i - 1]->stick_view->setFillColor(collection_model->element_color); + } + } + + void StickCollectionController::MergeSort(int left, int right) + { + if (left < right) { + int mid = left + (right - left) / 2; + + + MergeSort(left, mid); + MergeSort(mid + 1, right); + + + InPlaceMerge(left, mid, right); + } + } + + void StickCollectionController::completedColour() { diff --git a/Sorting-Stick/source/UI/MainMenu/MainMenuUIController.cpp b/Sorting-Stick/source/UI/MainMenu/MainMenuUIController.cpp index 968697c..3a00b4a 100644 --- a/Sorting-Stick/source/UI/MainMenu/MainMenuUIController.cpp +++ b/Sorting-Stick/source/UI/MainMenu/MainMenuUIController.cpp @@ -78,7 +78,7 @@ namespace UI bubble_sort_button->registerCallbackFuntion(std::bind(&MainMenuUIController::bubbleSortButtonCallback, this)); insertion_sort_button->registerCallbackFuntion(std::bind(&MainMenuUIController::insertionSortButtonCallback, this)); selection_sort_button->registerCallbackFuntion(std::bind(&MainMenuUIController::selectionSortButtonCallback, this)); - //merge_sort_button->registerCallbackFuntion(std::bind(&MainMenuUIController::mergeSortButtonCallback, this)); + merge_sort_button->registerCallbackFuntion(std::bind(&MainMenuUIController::mergeSortButtonCallback, this)); //quick_sort_button->registerCallbackFuntion(std::bind(&MainMenuUIController::quickSortButtonCallback, this)); //radix_sort_button->registerCallbackFuntion(std::bind(&MainMenuUIController::radixSortButtonCallback, this)); quit_button->registerCallbackFuntion(std::bind(&MainMenuUIController::quitButtonCallback, this)); @@ -109,6 +109,8 @@ namespace UI void MainMenuUIController::mergeSortButtonCallback() { ServiceLocator::getInstance()->getSoundService()->playSound(SoundType::BUTTON_CLICK); + GameService::setGameState(GameState::GAMEPLAY); + ServiceLocator::getInstance()->getGameplayService()->sortElement(Gameplay::Collection::SortType::MERGE_SORT); } void MainMenuUIController::quickSortButtonCallback() From b0136f740a09ddc93b66ca23be771c6b7d9fe18a Mon Sep 17 00:00:00 2001 From: Tarun Date: Tue, 8 Jul 2025 19:27:46 +0530 Subject: [PATCH 5/7] added quick sort --- .../Collection/StickCollectionController.h | 5 ++- .../Collection/StickCollectionController.cpp | 43 +++++++++++++++++++ .../UI/MainMenu/MainMenuUIController.cpp | 4 +- 3 files changed, 49 insertions(+), 3 deletions(-) diff --git a/Sorting-Stick/include/Gameplay/Collection/StickCollectionController.h b/Sorting-Stick/include/Gameplay/Collection/StickCollectionController.h index 0917bc6..2e675e4 100644 --- a/Sorting-Stick/include/Gameplay/Collection/StickCollectionController.h +++ b/Sorting-Stick/include/Gameplay/Collection/StickCollectionController.h @@ -74,8 +74,9 @@ namespace Gameplay void processMergeSort(); void InPlaceMerge(int left, int mid, int right); void MergeSort(int left, int right); - - + void processQuickSort(); + int partition(int low, int high); + void quickSort(int low, int high); void completedColour(); }; diff --git a/Sorting-Stick/source/Gameplay/Collection/StickCollectionController.cpp b/Sorting-Stick/source/Gameplay/Collection/StickCollectionController.cpp index 1e0a4fe..14f86e0 100644 --- a/Sorting-Stick/source/Gameplay/Collection/StickCollectionController.cpp +++ b/Sorting-Stick/source/Gameplay/Collection/StickCollectionController.cpp @@ -173,6 +173,9 @@ namespace Gameplay case Gameplay::Collection::SortType::MERGE_SORT: sort_thread = std::thread(&StickCollectionController::processMergeSort, this); break; + + case Gameplay::Collection::SortType::QUICK_SORT: + sort_thread = std::thread(&StickCollectionController::processQuickSort, this); // Placeholder for quick sort implementation } @@ -405,6 +408,46 @@ namespace Gameplay InPlaceMerge(left, mid, right); } } + + void StickCollectionController::processQuickSort() + { + quickSort(0, sticks.size() - 1); + completedColour(); + } + + int StickCollectionController::partition(int low, int high) + { + sticks[high]->stick_view->setFillColor(collection_model->selected_element_color); + int i = low- 1; + + for (int j = low; j < high; j++) { + sticks[j]->stick_view->setFillColor(collection_model->processing_element_color); + + if (sticks[j]->data < sticks[high]->data) { + i++; + number_of_array_access += 3; + number_of_comparisons++; + std::swap(sticks[i], sticks[j]); + updateStickPosition(); + std::this_thread::sleep_for(std::chrono::milliseconds(current_operation_delay)); + + } + sticks[j]->stick_view->setFillColor(collection_model->element_color); + } + std::swap(sticks[i + 1], sticks[high]); + number_of_array_access += 3; + return i + 1; + } + + void StickCollectionController::quickSort(int low, int high) + { + if (low < high) { + int location = partition(low, high); + quickSort(low, location - 1); + quickSort(location + 1, high); + } + } + void StickCollectionController::completedColour() diff --git a/Sorting-Stick/source/UI/MainMenu/MainMenuUIController.cpp b/Sorting-Stick/source/UI/MainMenu/MainMenuUIController.cpp index 3a00b4a..e6b7b2c 100644 --- a/Sorting-Stick/source/UI/MainMenu/MainMenuUIController.cpp +++ b/Sorting-Stick/source/UI/MainMenu/MainMenuUIController.cpp @@ -79,7 +79,7 @@ namespace UI insertion_sort_button->registerCallbackFuntion(std::bind(&MainMenuUIController::insertionSortButtonCallback, this)); selection_sort_button->registerCallbackFuntion(std::bind(&MainMenuUIController::selectionSortButtonCallback, this)); merge_sort_button->registerCallbackFuntion(std::bind(&MainMenuUIController::mergeSortButtonCallback, this)); - //quick_sort_button->registerCallbackFuntion(std::bind(&MainMenuUIController::quickSortButtonCallback, this)); + quick_sort_button->registerCallbackFuntion(std::bind(&MainMenuUIController::quickSortButtonCallback, this)); //radix_sort_button->registerCallbackFuntion(std::bind(&MainMenuUIController::radixSortButtonCallback, this)); quit_button->registerCallbackFuntion(std::bind(&MainMenuUIController::quitButtonCallback, this)); } @@ -116,6 +116,8 @@ namespace UI void MainMenuUIController::quickSortButtonCallback() { ServiceLocator::getInstance()->getSoundService()->playSound(SoundType::BUTTON_CLICK); + GameService::setGameState(GameState::GAMEPLAY); + ServiceLocator::getInstance()->getGameplayService()->sortElement(Gameplay::Collection::SortType::QUICK_SORT); } void MainMenuUIController::radixSortButtonCallback() From 5eac59553b777b920e77ae1fb3a095dede9117e2 Mon Sep 17 00:00:00 2001 From: Tarun Date: Tue, 22 Jul 2025 21:28:20 +0530 Subject: [PATCH 6/7] Update MainMenuUIController.cpp --- Sorting-Stick/source/UI/MainMenu/MainMenuUIController.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Sorting-Stick/source/UI/MainMenu/MainMenuUIController.cpp b/Sorting-Stick/source/UI/MainMenu/MainMenuUIController.cpp index e6b7b2c..b7aa5c4 100644 --- a/Sorting-Stick/source/UI/MainMenu/MainMenuUIController.cpp +++ b/Sorting-Stick/source/UI/MainMenu/MainMenuUIController.cpp @@ -80,7 +80,7 @@ namespace UI selection_sort_button->registerCallbackFuntion(std::bind(&MainMenuUIController::selectionSortButtonCallback, this)); merge_sort_button->registerCallbackFuntion(std::bind(&MainMenuUIController::mergeSortButtonCallback, this)); quick_sort_button->registerCallbackFuntion(std::bind(&MainMenuUIController::quickSortButtonCallback, this)); - //radix_sort_button->registerCallbackFuntion(std::bind(&MainMenuUIController::radixSortButtonCallback, this)); + radix_sort_button->registerCallbackFuntion(std::bind(&MainMenuUIController::radixSortButtonCallback, this)); quit_button->registerCallbackFuntion(std::bind(&MainMenuUIController::quitButtonCallback, this)); } @@ -123,6 +123,8 @@ namespace UI void MainMenuUIController::radixSortButtonCallback() { ServiceLocator::getInstance()->getSoundService()->playSound(SoundType::BUTTON_CLICK); + GameService::setGameState(GameState::GAMEPLAY); + ServiceLocator::getInstance()->getGameplayService()->sortElement(Gameplay::Collection::SortType::RADIX_SORT); } void MainMenuUIController::quitButtonCallback() From 2cceaf358b5a2634c9632d24c94297feafb854ac Mon Sep 17 00:00:00 2001 From: Tarun Date: Sun, 27 Jul 2025 21:18:03 +0530 Subject: [PATCH 7/7] radix sort --- .../Collection/StickCollectionController.h | 5 +- .../Collection/StickCollectionController.cpp | 183 ++++++++++++------ 2 files changed, 128 insertions(+), 60 deletions(-) diff --git a/Sorting-Stick/include/Gameplay/Collection/StickCollectionController.h b/Sorting-Stick/include/Gameplay/Collection/StickCollectionController.h index 2e675e4..b5dbb4a 100644 --- a/Sorting-Stick/include/Gameplay/Collection/StickCollectionController.h +++ b/Sorting-Stick/include/Gameplay/Collection/StickCollectionController.h @@ -77,8 +77,11 @@ namespace Gameplay void processQuickSort(); int partition(int low, int high); void quickSort(int low, int high); - + void ProcessRadixSort(); void completedColour(); + void countSort(int exponent); + void RadixSort(); + void updateStickPosition(int i); }; } } \ No newline at end of file diff --git a/Sorting-Stick/source/Gameplay/Collection/StickCollectionController.cpp b/Sorting-Stick/source/Gameplay/Collection/StickCollectionController.cpp index 14f86e0..72392b5 100644 --- a/Sorting-Stick/source/Gameplay/Collection/StickCollectionController.cpp +++ b/Sorting-Stick/source/Gameplay/Collection/StickCollectionController.cpp @@ -33,7 +33,7 @@ namespace Gameplay { sort_State = SortingState::NotSorting; collection_view->initialize(this); - + initializeSticks(); reset(); } @@ -168,7 +168,7 @@ namespace Gameplay case Gameplay::Collection::SortType::SELECTION_SORT: sort_thread = std::thread(&StickCollectionController::processSelectionSort, this); - break; + break; case Gameplay::Collection::SortType::MERGE_SORT: sort_thread = std::thread(&StickCollectionController::processMergeSort, this); @@ -176,10 +176,13 @@ namespace Gameplay case Gameplay::Collection::SortType::QUICK_SORT: sort_thread = std::thread(&StickCollectionController::processQuickSort, this); // Placeholder for quick sort implementation + + case Gameplay::Collection::SortType::RADIX_SORT: + sort_thread = std::thread(&StickCollectionController::ProcessRadixSort, this); // Placeholder for radix sort implementation } - - + + } bool StickCollectionController::isCollectionSorted() @@ -214,7 +217,7 @@ namespace Gameplay void StickCollectionController::processBubbleSort() { - SoundService* sound = Global::ServiceLocator::getInstance()->getSoundService(); + SoundService* sound = Global::ServiceLocator::getInstance()->getSoundService(); for (int j = 0; j < sticks.size(); j++) { // Loop through the sticks array if (sort_State == SortingState::NotSorting) { break; } // Check if sorting has stopped or been interrupted @@ -296,53 +299,53 @@ namespace Gameplay } completedColour(); } - void StickCollectionController::processSelectionSort() - { - for (int i = 0; i < sticks.size() - 1; i++) { - - if (sort_State == SortingState::NotSorting) { break; } // Check if sorting has stopped or been interrupted - - int min = i; - sticks[i]->stick_view->setFillColor(collection_model->selected_element_color); - number_of_array_access++; - - for (int j = i; j < sticks.size(); j++) { - if (sort_State == SortingState::NotSorting) { break; } - number_of_array_access++; - number_of_comparisons++; - sticks[j]->stick_view->setFillColor(collection_model->processing_element_color); - std::this_thread::sleep_for(std::chrono::milliseconds(current_operation_delay)); - if (sticks[j]->data < sticks[min]->data) { - sticks[min]->stick_view->setFillColor(collection_model->element_color); - min = j; - sticks[min]->stick_view->setFillColor(collection_model->processing_element_color); + void StickCollectionController::processSelectionSort() + { + for (int i = 0; i < sticks.size() - 1; i++) { - } - else { - sticks[j]->stick_view->setFillColor(collection_model->element_color); - } + if (sort_State == SortingState::NotSorting) { break; } // Check if sorting has stopped or been interrupted - number_of_array_access += 3; - + int min = i; + sticks[i]->stick_view->setFillColor(collection_model->selected_element_color); + number_of_array_access++; - sticks[i]->stick_view->setFillColor(collection_model->placement_position_element_color); // Mark as sorted - updateStickPosition(); + for (int j = i; j < sticks.size(); j++) { + if (sort_State == SortingState::NotSorting) { break; } + number_of_array_access++; + number_of_comparisons++; + sticks[j]->stick_view->setFillColor(collection_model->processing_element_color); + std::this_thread::sleep_for(std::chrono::milliseconds(current_operation_delay)); + if (sticks[j]->data < sticks[min]->data) { + sticks[min]->stick_view->setFillColor(collection_model->element_color); + min = j; + sticks[min]->stick_view->setFillColor(collection_model->processing_element_color); } + else { + sticks[j]->stick_view->setFillColor(collection_model->element_color); + } number_of_array_access += 3; - std::swap(sticks[min], sticks[i]); // Place the found minimum at its final position + sticks[i]->stick_view->setFillColor(collection_model->placement_position_element_color); // Mark as sorted updateStickPosition(); } - - - // Ensure the last stick is also marked as sorted - sticks[sticks.size() - 1]->stick_view->setFillColor(collection_model->placement_position_element_color); - completedColour(); - + + number_of_array_access += 3; + std::swap(sticks[min], sticks[i]); // Place the found minimum at its final position + + sticks[i]->stick_view->setFillColor(collection_model->placement_position_element_color); // Mark as sorted + updateStickPosition(); + + } + + + // Ensure the last stick is also marked as sorted + sticks[sticks.size() - 1]->stick_view->setFillColor(collection_model->placement_position_element_color); + completedColour(); + } void StickCollectionController::processMergeSort() @@ -351,7 +354,7 @@ namespace Gameplay completedColour(); } - + void StickCollectionController::InPlaceMerge(int left, int mid, int right) { int i = left; @@ -361,10 +364,10 @@ namespace Gameplay number_of_comparisons++; number_of_array_access += 2; - + if (sticks[i]->data <= sticks[j]->data) { - + i++; } else { @@ -373,7 +376,7 @@ namespace Gameplay int index = j; // Shift elements rightward - while (index!=i) { + while (index != i) { sticks[index] = sticks[index - 1]; index--; number_of_array_access += 2; @@ -389,7 +392,7 @@ namespace Gameplay updateStickPosition(); } - sticks[i- 1]->stick_view->setFillColor(collection_model->processing_element_color); + sticks[i - 1]->stick_view->setFillColor(collection_model->processing_element_color); std::this_thread::sleep_for(std::chrono::milliseconds(current_operation_delay)); sticks[i - 1]->stick_view->setFillColor(collection_model->element_color); } @@ -400,11 +403,11 @@ namespace Gameplay if (left < right) { int mid = left + (right - left) / 2; - + MergeSort(left, mid); MergeSort(mid + 1, right); - + InPlaceMerge(left, mid, right); } } @@ -418,7 +421,7 @@ namespace Gameplay int StickCollectionController::partition(int low, int high) { sticks[high]->stick_view->setFillColor(collection_model->selected_element_color); - int i = low- 1; + int i = low - 1; for (int j = low; j < high; j++) { sticks[j]->stick_view->setFillColor(collection_model->processing_element_color); @@ -432,7 +435,7 @@ namespace Gameplay std::this_thread::sleep_for(std::chrono::milliseconds(current_operation_delay)); } - sticks[j]->stick_view->setFillColor(collection_model->element_color); + sticks[j]->stick_view->setFillColor(collection_model->element_color); } std::swap(sticks[i + 1], sticks[high]); number_of_array_access += 3; @@ -448,8 +451,14 @@ namespace Gameplay } } - - + void StickCollectionController::ProcessRadixSort() + { + RadixSort(); + completedColour(); + } + + + void StickCollectionController::completedColour() { @@ -458,26 +467,82 @@ namespace Gameplay if (sort_State == SortingState::NotSorting) { break; } - - sticks[i]->stick_view->setFillColor(collection_model->element_color); // Reset to default color if not completed - - + sticks[i]->stick_view->setFillColor(collection_model->element_color); // Reset to default color if not completed + + + for (int j = 0; j < sticks.size(); j++) { - + if (sort_State == SortingState::NotSorting) { break; } - + /*ServiceLocator::getInstance()->getSoundService()->playSound(Sound::SoundType::COMPARE_SFX);*/ sticks[j]->stick_view->setFillColor(collection_model->placement_position_element_color); std::this_thread::sleep_for(std::chrono::milliseconds(color_delay)); - + } - + } } + void StickCollectionController::countSort(int exponent) + { + SoundService* sound = Global::ServiceLocator::getInstance()->getSoundService(); + std::vector output(sticks.size()); + std::vector count(10, 0); + + // Process each element to count digits + for (int i = 0; i < sticks.size(); ++i) { + sound->playSound(SoundType::COMPARE_SFX); + int digit = (sticks[i]->data / exponent) % 10; + count[digit]++; + number_of_array_access++; + sticks[i]->stick_view->setFillColor(collection_model->processing_element_color); + std::this_thread::sleep_for(std::chrono::milliseconds(current_operation_delay / 2)); // Delay for visual processing + sticks[i]->stick_view->setFillColor(collection_model->element_color); // Reset color after processing + } + + // Accumulate the count array + for (int i = 1; i < 10; ++i) { + count[i] += count[i - 1]; + } + + // Sorting based on the current digit + for (int i = sticks.size() - 1; i >= 0; --i) { + + int digit = (sticks[i]->data / exponent) % 10; + output[count[digit] - 1] = sticks[i]; + output[count[digit] - 1]->stick_view->setFillColor(collection_model->processing_element_color); + count[digit]--; + number_of_array_access++; + + } + + // Place elements back into the main array + for (int i = 0; i < sticks.size(); ++i) { + sticks[i] = output[i]; + sticks[i]->stick_view->setFillColor(collection_model->placement_position_element_color); // Final sorted color for this digit + updateStickPosition(i); + std::this_thread::sleep_for(std::chrono::milliseconds(current_operation_delay)); // Delay to observe final sorting state + } + } + void StickCollectionController::RadixSort() + { + int maxElement = INT_MIN; + const int size = sticks.size(); + + for (int i = 0; i < size; ++i) maxElement = std::max(sticks[i]->data, maxElement); + for (int exponent = 1; maxElement / exponent > 0; exponent *= 10) countSort(exponent); + } + void StickCollectionController::updateStickPosition(int i) + { + float x_position = (i * sticks[i]->stick_view->getSize().x) + ((i)*collection_model->elements_spacing); + float y_position = collection_model->element_y_position - sticks[i]->stick_view->getSize().y; + + sticks[i]->stick_view->setPosition(sf::Vector2f(x_position, y_position)); + } } }