187#include <tclap/CmdLine.h>
197using namespace Aleph;
198using namespace std::chrono;
227 auto end_time = high_resolution_clock::now();
248 for (
size_t i = 0; i < n; ++i)
256 for (
size_t i = 0; i < n; ++i)
257 arr.
append(
static_cast<int>(i));
264 for (
size_t i = n; i > 0; --i)
265 arr.
append(
static_cast<int>(i));
272 size_t swaps = n / 20;
274 for (
size_t i = 0; i <
swaps; ++i)
276 size_t a = dist(
rng);
277 size_t b = dist(
rng);
278 swap(arr(a), arr(b));
287 for (
size_t i = 0; i < n; ++i)
296 for (
size_t i = 0; i < n; ++i)
309 for (
size_t i = 0; i < arr.
size(); ++i)
317 for (
size_t i = 0; i < arr.
size(); ++i)
325 for (
size_t i = 0; i < src.
size(); ++i)
335 cout <<
"\n" << string(79,
'=') <<
"\n";
336 cout <<
setw(18) << left <<
"Algorithm"
337 <<
setw(15) << left <<
"Distribution"
338 <<
setw(12) << left <<
"Container"
339 <<
setw(14) << right <<
"Time (ms)"
340 <<
setw(10) <<
"Status"
346 const string& container,
double time_ms,
bool ok)
349 <<
setw(15) << left << dist
350 <<
setw(12) << left << container
352 <<
setw(10) << (
ok ?
"OK" :
"FAIL")
514 cout <<
"============================================================\n";
515 cout <<
" ALEPH-W SORTING ALGORITHMS BENCHMARK\n";
516 cout <<
"============================================================\n";
517 cout <<
"\nConfiguration:\n";
520 cout <<
" Test arrays: " << (config.
test_arrays ?
"Yes" :
"No") <<
"\n";
521 cout <<
" Test lists: " << (config.
test_lists ?
"Yes" :
"No") <<
"\n";
522 cout <<
" Random seed: " << config.
seed <<
"\n";
562 cout <<
"\nBenchmark completed successfully!\n\n";
572 cout <<
"============================================================\n";
573 cout <<
" COMPLEXITY DEMONSTRATION\n";
574 cout <<
"============================================================\n";
575 cout <<
"\nShows how sorting time scales with input size.\n";
576 cout <<
"For O(n log n): doubling n roughly doubles time.\n";
577 cout <<
"For O(n^2): doubling n roughly quadruples time.\n\n";
586 size_t sizes[] = {1000, 2000, 4000, 8000, 16000, 32000};
589 <<
setw(15) <<
"Introsort"
590 <<
setw(15) <<
"Intro(Array)"
591 <<
setw(15) <<
"Quicksort"
592 <<
setw(15) <<
"Merge (list)"
596 for (
size_t n :
sizes)
628 cout <<
"\nIntrosort = quicksort + heapsort fallback + insertion sort for small.\n";
629 cout <<
"Array uses contiguous memory, DynArray uses segmented blocks.\n";
639 cout <<
"============================================================\n";
640 cout <<
" ALGORITHM SELECTION GUIDE\n";
641 cout <<
"============================================================\n";
643+-------------------+------------------------+-------------------------+
644| Scenario | Best Choice | Why |
645+-------------------+------------------------+-------------------------+
646| General purpose | introsort() | Fast + O(n log n) guarantee |
647| Nearly sorted | insertion_sort() | O(n) for sorted data |
648| Guaranteed O(nlogn)| introsort()/heapsort()| No worst case O(n^2) |
649| Linked lists | mergesort() | O(1) extra space |
650| Stability needed | mergesort() | Preserves equal order |
651| Limited memory | heapsort() | O(1) extra space |
652| Small arrays (<50)| insertion_sort() | Low overhead |
653| External sorting | mergesort() | Sequential access |
654| Medium arrays | shellsort() | Good balance |
655| Adversarial data | introsort() | Immune to quicksort killers |
656+-------------------+------------------------+-------------------------+
658For Aleph-w containers:
659 - DynArray: Use introsort() (recommended), quicksort_op(), or heapsort()
660 - DynList: Use mergesort() (O(1) extra space for lists!)
661 - DynDlist: Use mergesort() or quicksort()
663Note: introsort() = quicksort + heapsort fallback + insertion sort for small
664 partitions. Same speed as quicksort but with O(n log n) guarantee.
677 "Comprehensive sorting algorithm benchmark for Aleph-w.\n"
678 "Tests multiple algorithms across different data distributions "
679 "and container types.",
683 TCLAP::ValueArg<size_t>
nArg(
685 "Number of elements to sort",
686 false, 10000,
"count",
cmd
691 "Include O(n^2) algorithms (selection, insertion, bubble)",
697 "Only test array-based containers",
703 "Only test linked list containers",
709 "Run complexity demonstration (time vs size)",
715 "Print algorithm selection guide",
719 TCLAP::ValueArg<unsigned>
seedArg(
721 "Random seed for reproducible results",
722 false, 42,
"seed",
cmd
743 cerr <<
"Error: Cannot specify both --array-only and --list-only\n";
763 catch (TCLAP::ArgException& e)
765 cerr <<
"Error: " << e.error() <<
" for argument " << e.argId() <<
endl;
770 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.
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.
void empty() noexcept
Empty the list.
T & append(const T &item)
Append a copied item at the end of the list.
Dynamic singly linked list with functional programming support.
T & append(const T &item)
Append a new item by copy.
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)
double elapsed_ms() const
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.
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.
void heapsort(T *array, const size_t n, const Compare &cmp=Compare())
Sort an array using the heapsort algorithm.
void faster_heapsort(T *array, const size_t n, const Compare &cmp=Compare())
Optimized version of heapsort.
void mergesort(T *a, const long l, const long r, std::vector< T > &buf, Compare cmp)
DynArray< T > & in_place_sort(DynArray< T > &c, Cmp cmp=Cmp())
Sorts a DynArray in place.
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 insertion_sort(T *a, const long l, const long r, const Compare &cmp=Compare()) noexcept(noexcept(cmp(a[0], a[0])) &&std::is_nothrow_move_assignable_v< T >)
Sort an array using insertion sort.
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(cmp(a[0], a[0])) &&std::is_nothrow_swappable_v< T >)
Sort an array using the selection sort algorithm.
void introsort(T *a, const long l, const long r, const Compare &cmp=Compare())
Sort an array using introsort (introspective sort).
DynList< T > maps(const C &c, Op op)
Classic map operation.
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 to_array(const DynArray< T > &src, Array< T > &dst)
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.