Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
random_select_example.cc
Go to the documentation of this file.
1/*
2 Aleph_w
3
4 Data structures & Algorithms
5 version 2.0.0b
6 https://github.com/lrleon/Aleph-w
7
8 This file is part of Aleph-w library
9
10 Copyright (c) 2002-2026 Leandro Rabindranath Leon
11
12 Permission is hereby granted, free of charge, to any person obtaining a copy
13 of this software and associated documentation files (the "Software"), to deal
14 in the Software without restriction, including without limitation the rights
15 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16 copies of the Software, and to permit persons to whom the Software is
17 furnished to do so, subject to the following conditions:
18
19 The above copyright notice and this permission notice shall be included in all
20 copies or substantial portions of the Software.
21
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 SOFTWARE.
29*/
30
40#include <iostream>
41#include <iomanip>
42
43#include <tpl_sort_utils.H>
44#include <tpl_dynArray.H>
45#include <print_rule.H>
46
47using namespace Aleph;
48
49 template <typename Container>
50 void print_array(const char * label, const Container & a)
51 {
52 std::cout << std::left << std::setw(15) << label << ": [";
53 for (size_t i = 0; i < a.size(); ++i)
54 {
55 std::cout << a[i];
56 if (i < a.size() - 1)
57 std::cout << ", ";
58 }
59 std::cout << "]\n";
60 }
61
62int main()
63{
64 std::cout << "\n=== Random Select (Quickselect) Demonstration ===\n\n";
65
66 {
67 std::cout << "Scenario A: Finding the median of an array\n";
68 print_rule();
69 DynArray<int> arr = {9, 2, 4, 7, 3, 7, 10, 2, 7, 1, 8};
70 print_array("Original Array", arr);
71
72 const size_t median_idx = arr.size() / 2;
73
74 // random_select takes (array, k-th index, comparator)
75 const int median = random_select(arr, static_cast<long>(median_idx));
76
77 std::cout << "Position k : " << median_idx << " (Median)\n";
78 std::cout << "Selected Value : " << median << "\n";
79
80 // Note that the array has been partially sorted so that all elements
81 // before `median_idx` are <= median, and all elements after are >= median.
82 print_array("Partially Sorted", arr);
83 print_rule();
84 std::cout << "\n";
85 }
86
87 {
88 std::cout << "Scenario B: Finding the top 3 largest elements\n";
89 print_rule();
90 DynArray<int> arr = {15, 3, 9, 8, 22, 17, 4, 12, 19};
91 print_array("Original Array", arr);
92
93 // To find the top 3, we select the (n-3)-th element
94 const size_t k = arr.size() - 3;
95 const int k_val = random_select(arr, static_cast<long>(k));
96
97 std::cout << "Position k : " << k << " (n - 3)\n";
98 std::cout << "Selected Value : " << k_val << "\n";
99 print_array("Partially Sorted", arr);
100
101 std::cout << "Top 3 elements : [";
102 for (size_t i = k; i < arr.size(); ++i)
103 {
104 std::cout << arr[i];
105 if (i < arr.size() - 1)
106 std::cout << ", ";
107 }
108 std::cout << "]\n";
109
110 print_rule();
111 std::cout << "\n";
112 }
113
114 {
115 std::cout << "Scenario C: Handling arrays with many duplicates\n";
116 print_rule();
117 // The 3-way partition (Dutch National Flag) efficiently handles duplicates
118 DynArray<int> arr = {5, 5, 5, 2, 5, 5, 8, 5, 5, 1, 5, 9};
119 print_array("Original Array", arr);
120
121 const size_t k = 8;
122 const int val = random_select(arr, static_cast<long>(k));
123
124 std::cout << "Position k : " << k << "\n";
125 std::cout << "Selected Value : " << val << "\n";
126 print_array("Partially Sorted", arr);
127
128 print_rule();
129 std::cout << "\n";
130 }
131
132 std::cout << "Done.\n";
133 return 0;
134}
size_t size() const noexcept
Return the current dimension of array.
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
const T * median(const T &a, const T &b, const T &c, const Compare &cmp=Compare())
Return a pointer to the median value among three elements.
Definition ahUtils.H:84
Divide_Conquer_DP_Result< Cost > divide_and_conquer_partition_dp(const size_t groups, const size_t n, Transition_Cost_Fn transition_cost, const Cost inf=dp_optimization_detail::default_inf< Cost >())
Optimize partition DP using divide-and-conquer optimization.
const T & random_select(DynArray< T > &a, const long i, const Compare &cmp=Compare())
Select the i-th smallest element in a DynArray.
void print_rule()
Prints a horizontal rule for example output separation.
Definition print_rule.H:39
void print_array(const char *label, const Container &a)
int main()
static int * k
Lazy and scalable dynamic array implementation.
Comprehensive sorting algorithms and search utilities for Aleph-w.