187#include <tclap/CmdLine.h>
197using namespace Aleph;
198using namespace std::chrono;
235 stop_time = high_resolution_clock::now();
242 return duration<double, milli>(end_time -
start_time).count();
261 uniform_int_distribution<int> dist(1,
static_cast<int>(n * 10));
262 for (
size_t i = 0; i < n; ++i)
270 for (
size_t i = 0; i < n; ++i)
271 arr.
append(
static_cast<int>(i));
278 for (
size_t i = n; i > 0; --i)
279 arr.
append(
static_cast<int>(i));
286 size_t swaps = n / 20;
287 uniform_int_distribution<size_t> dist(0, n - 1);
288 for (
size_t i = 0; i < swaps; ++i)
290 size_t a = dist(
rng);
291 size_t b = dist(
rng);
292 swap(arr(a), arr(b));
300 uniform_int_distribution<int> dist(1, 10);
301 for (
size_t i = 0; i < n; ++i)
309 size_t run_size =
max(n / 10,
size_t(1));
310 for (
size_t i = 0; i < n; ++i)
311 arr.
append(
static_cast<int>(i % run_size));
323 for (
size_t i = 0; i < arr.
size(); ++i)
331 for (
size_t i = 0; i < arr.
size(); ++i)
339 for (
size_t i = 0; i < src.
size(); ++i)
349 cout <<
"\n" << string(79,
'=') <<
"\n";
350 cout << setw(18) << left <<
"Algorithm"
351 << setw(15) << left <<
"Distribution"
352 << setw(12) << left <<
"Container"
353 << setw(14) << right <<
"Time (ms)"
354 << setw(10) <<
"Status"
356 cout << string(79,
'-') << endl;
360 const string& container,
double time_ms,
bool ok)
362 cout << setw(18) << left << algo
363 << setw(15) << left << dist
364 << setw(12) << left << container
365 << setw(14) << right << fixed << setprecision(2) << time_ms
366 << setw(10) << (ok ?
"OK" :
"FAIL")
372 cout << string(79,
'-') << endl;
380 const string& dist_name,
448 const string& dist_name,
485 const string& dist_name,
544 cout <<
"============================================================\n";
545 cout <<
" ALEPH-W SORTING ALGORITHMS BENCHMARK\n";
546 cout <<
"============================================================\n";
547 cout <<
"\nConfiguration:\n";
550 cout <<
" Test arrays: " << (config.
test_arrays ?
"Yes" :
"No") <<
"\n";
551 cout <<
" Test lists: " << (config.
test_lists ?
"Yes" :
"No") <<
"\n";
552 cout <<
" Random seed: " << config.
seed <<
"\n";
564 DistInfo distributions[] = {
575 for (
const auto& dist : distributions)
592 cout <<
"\nBenchmark completed successfully!\n\n";
602 cout <<
"============================================================\n";
603 cout <<
" COMPLEXITY DEMONSTRATION\n";
604 cout <<
"============================================================\n";
605 cout <<
"\nShows how sorting time scales with input size.\n";
606 cout <<
"For O(n log n): doubling n roughly doubles time.\n";
607 cout <<
"For O(n^2): doubling n roughly quadruples time.\n\n";
616 size_t sizes[] = {1000, 2000, 4000, 8000, 16000, 32000};
618 cout << setw(10) <<
"Size"
619 << setw(15) <<
"Introsort"
620 << setw(15) <<
"Intro(Array)"
621 << setw(15) <<
"Quicksort"
622 << setw(15) <<
"Merge (list)"
624 cout << string(70,
'-') << endl;
626 for (
size_t n : sizes)
629 cout << setw(10) << n;
635 cout << setw(15) << fixed << setprecision(2) << timer.
elapsed_ms();
658 cout <<
"\nIntrosort = quicksort + heapsort fallback + insertion sort for small.\n";
659 cout <<
"Array uses contiguous memory, DynArray uses segmented blocks.\n";
669 cout <<
"============================================================\n";
670 cout <<
" ALGORITHM SELECTION GUIDE\n";
671 cout <<
"============================================================\n";
673+-------------------+------------------------+-------------------------+
674| Scenario | Best Choice | Why |
675+-------------------+------------------------+-------------------------+
676| General purpose | introsort() | Fast + O(n log n) guarantee |
677| Nearly sorted | insertion_sort() | O(n) for sorted data |
678| Guaranteed O(nlogn)| introsort()/heapsort()| No worst case O(n^2) |
679| Linked lists | mergesort() | O(1) extra space |
680| Stability needed | mergesort() | Preserves equal order |
681| Limited memory | heapsort() | O(1) extra space |
682| Small arrays (<50)| insertion_sort() | Low overhead |
683| External sorting | mergesort() | Sequential access |
684| Medium arrays | shellsort() | Good balance |
685| Adversarial data | introsort() | Immune to quicksort killers |
686+-------------------+------------------------+-------------------------+
688For Aleph-w containers:
689 - DynArray: Use introsort() (recommended), quicksort_op(), or heapsort()
690 - DynList: Use mergesort() (O(1) extra space for lists!)
691 - DynDlist: Use mergesort() or quicksort()
693Note: introsort() = quicksort + heapsort fallback + insertion sort for small
694 partitions. Same speed as quicksort but with O(n log n) guarantee.
702int main(
int argc,
char* argv[])
707 "Comprehensive sorting algorithm benchmark for Aleph-w.\n"
708 "Tests multiple algorithms across different data distributions "
709 "and container types.",
713 TCLAP::ValueArg<size_t> nArg(
715 "Number of elements to sort",
716 false, 10000,
"count",
cmd
719 TCLAP::SwitchArg allArg(
721 "Include O(n^2) algorithms (selection, insertion, bubble)",
725 TCLAP::SwitchArg arrayOnlyArg(
727 "Only test array-based containers",
731 TCLAP::SwitchArg listOnlyArg(
733 "Only test linked list containers",
737 TCLAP::SwitchArg complexityArg(
739 "Run complexity demonstration (time vs size)",
743 TCLAP::SwitchArg guideArg(
745 "Print algorithm selection guide",
749 TCLAP::ValueArg<unsigned> seedArg(
751 "Random seed for reproducible results",
752 false, 42,
"seed",
cmd
755 TCLAP::SwitchArg verboseArg(
761 cmd.parse(argc, argv);
767 config.
test_lists = not arrayOnlyArg.getValue();
768 config.
seed = seedArg.getValue();
769 config.
verbose = verboseArg.getValue();
771 if (listOnlyArg.getValue()
and arrayOnlyArg.getValue())
773 cerr <<
"Error: Cannot specify both --array-only and --list-only\n";
777 if (guideArg.getValue())
783 if (complexityArg.getValue())
793 catch (TCLAP::ArgException& e)
795 cerr <<
"Error: " << e.error() <<
" for argument " << e.argId() << endl;
800 cerr <<
"Error: " << e.what() << endl;
High-level sorting functions for Aleph containers.
Simple dynamic array with automatic resizing and functional operations.
constexpr size_t size() const noexcept
Return the number of elements stored in the stack.
void empty() noexcept
Empties the container.
T & append(const T &data)
Append a copy of data
size_t size() const noexcept
Return the current dimension of array.
T & append()
Allocate a new entry to the end of array.
void empty() noexcept
Empty the array.
Dynamic doubly linked list with O(1) size and bidirectional access.
T & append(const T &item)
Append a copied item at the end of the list.
void empty() noexcept
@brief Empties the container.
Doubly-linked list (defined in tpl_dynList.H).
T & append(const T &item)
void empty() noexcept
empty the list
void nearly_sorted(DynArray< int > &arr, size_t n)
Nearly sorted: 5% of elements are swapped.
void sawtooth(DynArray< int > &arr, size_t n)
Sawtooth pattern: multiple ascending runs.
void sorted_desc(DynArray< int > &arr, size_t n)
Sorted in descending order.
DataGenerator(unsigned seed)
void random(DynArray< int > &arr, size_t n)
Uniformly distributed random integers.
void sorted_asc(DynArray< int > &arr, size_t n)
Already sorted in ascending order.
void few_unique(DynArray< int > &arr, size_t n)
Few unique values (high repetition)
high_resolution_clock::time_point stop_time
double elapsed_ms() const
chrono::high_resolution_clock::time_point start
high_resolution_clock::time_point start_time
__gmp_expr< typename __gmp_resolve_expr< T, V >::value_type, __gmp_binary_expr< __gmp_expr< T, U >, __gmp_expr< V, W >, __gmp_max_function > > max(const __gmp_expr< T, U > &expr1, const __gmp_expr< V, W > &expr2)
Singly linked list implementations with head-tail access.
Main namespace for Aleph-w library functions.
Array< typename C::Item_Type > to_array(const C &c)
Convenience wrapper identical to to_Array (copy contents).
void insertion_sort(T *a, const long l, const long r, const Compare &cmp=Compare()) noexcept(noexcept(static_cast< bool >(cmp(std::declval< T & >(), std::declval< T & >()))) and std::is_nothrow_move_constructible_v< T > and std::is_nothrow_move_assignable_v< T >)
Sort an array using insertion sort.
bool is_sorted(const Container< T > &cont, const Compare &cmp=Compare())
Check if a container is sorted in ascending order.
T & swap(T &t1, T &t2)
Generic swap using object's swap method.
DynArray< T > & in_place_sort(DynArray< T > &c, Cmp cmp=Cmp())
Sorts a DynArray in place.
and
Check uniqueness with explicit hash + equality functors.
void bubble_sort(DynArray< T > &a, const Compare &cmp=Compare())
Sort a dynamic array using bubble sort.
void quicksort(T *a, const long l, const long r, const Compare &cmp=Compare())
Sort an array using iterative quicksort with optimizations.
void mergesort(T *a, const long l, const long r, Array< T > &buf, Compare cmp)
Sort an array using merge sort with a reusable buffer.
void shellsort(DynArray< T > &a, const Compare &cmp=Compare())
Sort a dynamic array using Shell sort.
void selection_sort(T *a, const size_t n, const Compare &cmp=Compare()) noexcept(noexcept(static_cast< bool >(cmp(a[0], a[0]))) and std::is_nothrow_swappable_v< T >)
Sort an array using the selection sort algorithm.
constexpr void faster_heapsort(T *array, const size_t n, const Compare &cmp=Compare())
Optimized version of heapsort.
constexpr void heapsort(T *array, const size_t n, const Compare &cmp=Compare())
Sort an array using the heapsort algorithm.
void introsort(T *a, const long l, const long r, const Compare &cmp=Compare())
Sort an array using introsort (introspective sort).
void quicksort_op(C< T > &a, const Compare &cmp=Compare(), const size_t threshold=Quicksort_Threshold)
Optimized quicksort for containers using operator().
void to_dynlist(const DynArray< T > &arr, DynList< T > &list)
void run_benchmarks(const BenchmarkConfig &config)
void benchmark_contiguous_array(const BenchmarkConfig &config, const string &dist_name, const DynArray< int > &source_data)
void to_dyndlist(const DynArray< T > &arr, DynDlist< T > &list)
void benchmark_array_algorithms(const BenchmarkConfig &config, const string &dist_name, const DynArray< int > &source_data)
void demonstrate_complexity(const BenchmarkConfig &base_config)
void print_result(const string &algo, const string &dist, const string &container, double time_ms, bool ok)
void benchmark_list_algorithms(const BenchmarkConfig &config, const string &dist_name, const DynArray< int > &source_data)
void print_recommendations()
Dynamic array container with automatic resizing.
Lazy and scalable dynamic array implementation.
Dynamic doubly linked list implementation.
Comprehensive sorting algorithms and search utilities for Aleph-w.