From 61490b7de67e781563d7ab52c43411a6c49458bf Mon Sep 17 00:00:00 2001 From: DANIEL DRORI Date: Mon, 1 Jun 2026 20:27:49 +0300 Subject: [PATCH] Add Tournament Sort implementation --- sorting/tournament_sort.cpp | 109 ++++++++++++++++++++++++++++++++++++ 1 file changed, 109 insertions(+) create mode 100644 sorting/tournament_sort.cpp diff --git a/sorting/tournament_sort.cpp b/sorting/tournament_sort.cpp new file mode 100644 index 00000000000..b18c8f2c1f9 --- /dev/null +++ b/sorting/tournament_sort.cpp @@ -0,0 +1,109 @@ +/** + * @file tournament_sort.cpp + * @brief Implementation of [Tournament Sort] + * (https://en.wikipedia.org/wiki/Tournament_sort) + * + * @details + * Tournament Sort is a sorting algorithm that improves upon naive selection + * sort by using a min-heap (priority queue) to find the next minimum element. + * + * Instead of scanning all n elements each round (O(n)), a heap finds the + * minimum in O(log n), giving an overall time complexity of O(n log n). + * + * The name comes from its resemblance to a single-elimination sports + * tournament where the "winner" (minimum element) is found each round. + * + * Time Complexity: O(n log n) + * Space Complexity: O(n) + * + * @author drori12 + */ + +#include /// for std::is_sorted +#include /// for assert +#include /// for std::cout +#include /// for std::priority_queue +#include /// for std::vector + +/** + * @namespace sorting + * @brief Sorting algorithms + */ +namespace sorting { + +/** + * @brief Tournament sort using a min-heap priority queue. + * @tparam T type of elements in the array (must support < operator) + * @param arr input vector to be sorted + * @return sorted vector in ascending order + */ +template +std::vector tournament_sort(std::vector arr) { + // min-heap: smallest element always on top + std::priority_queue, std::greater> min_heap( + arr.begin(), arr.end()); + + std::vector sorted; + sorted.reserve(arr.size()); + + while (!min_heap.empty()) { + sorted.push_back(min_heap.top()); + min_heap.pop(); + } + + return sorted; +} + +} // namespace sorting + +/** + * @brief Self-test implementations + * @returns void + */ +static void tests() { + // Test 1: regular unsorted array + std::vector arr1 = {5, 3, 1, 4, 2}; + std::vector sorted1 = sorting::tournament_sort(arr1); + assert(std::is_sorted(sorted1.begin(), sorted1.end())); + + // Test 2: already sorted + std::vector arr2 = {1, 2, 3, 4, 5}; + std::vector sorted2 = sorting::tournament_sort(arr2); + assert(std::is_sorted(sorted2.begin(), sorted2.end())); + + // Test 3: reverse sorted + std::vector arr3 = {5, 4, 3, 2, 1}; + std::vector sorted3 = sorting::tournament_sort(arr3); + assert(std::is_sorted(sorted3.begin(), sorted3.end())); + + // Test 4: single element + std::vector arr4 = {42}; + std::vector sorted4 = sorting::tournament_sort(arr4); + assert(std::is_sorted(sorted4.begin(), sorted4.end())); + + // Test 5: empty array + std::vector arr5 = {}; + std::vector sorted5 = sorting::tournament_sort(arr5); + assert(sorted5.empty()); + + // Test 6: duplicates + std::vector arr6 = {3, 1, 2, 1, 3}; + std::vector sorted6 = sorting::tournament_sort(arr6); + assert(std::is_sorted(sorted6.begin(), sorted6.end())); + + // Test 7: negative numbers + std::vector arr7 = {-3, 0, -1, 5, 2}; + std::vector sorted7 = sorting::tournament_sort(arr7); + assert(std::is_sorted(sorted7.begin(), sorted7.end())); + + std::cout << "All tests passed!\n"; +} + +/** + * @brief Main function + * @returns 0 on successful exit + */ +int main() { + tests(); + return 0; +}