38#include <gtest/gtest.h>
57 for (
const int x :
xs)
126 int a[] = {3, 1, 2, 1, 0};
128 EXPECT_TRUE(std::is_sorted(std::begin(a), std::end(a)));
149 for (
size_t i = 1; i < a.size(); ++i)
157 for (
size_t i = 1; i < a.size(); ++i)
165 for (
size_t i = 1; i < a.size(); ++i)
173 for (
size_t i = 1; i < a.size(); ++i)
184 for (
size_t child = 2; child <= n; ++child)
186 size_t parent = child / 2;
187 if (a(parent - 1) > a(child - 1))
212 for (
int x : {5, 4, 3, 2, 1, 0})
224 for (
size_t i = 0; i < a.
size(); ++i)
231 const int pivot = a(p);
232 for (
long i = 0; i < p; ++i)
237 std::vector<int>
after;
239 for (
size_t i = 0; i < a.
size(); ++i)
240 after.push_back(a(i));
249 for (
int x : {4, 1, 3, 2, 0, 2})
254 for (
size_t i = 0; i < a.
size(); ++i)
261 const int pivot = a(p);
262 for (
long i = 0; i < p; ++i)
267 std::vector<int>
after;
269 for (
size_t i = 0; i < a.
size(); ++i)
270 after.push_back(a(i));
280 auto expected = std::vector<int>{4, 1, 3, 2, 0, 2};
289 for (
int x : {4, 1, 3, 2, 0, 2})
291 auto expected = std::vector<int>{4, 1, 3, 2, 0, 2};
310 int a[] = {4, 1, 3, 2, 0, 2};
324 int a[] = {0, 2, 4, 1, 3, 5};
326 EXPECT_TRUE(std::is_sorted(std::begin(a), std::end(a)));
340 int a[] = {5, 4, 3, 2, 1, 0, 0, 9};
342 EXPECT_TRUE(std::is_sorted(std::begin(a), std::end(a)));
377 delete_all_nodes(
h2);
392 for (
int x : {3, 1, 2, 0})
426 int a[] = {4, 1, 3, 2, 0, 2};
427 std::vector<int>
expected(std::begin(a), std::end(a));
467 int a[] = {5, 4, 3, 2, 1, 0, 0, 9};
468 mergesort(a, 0,
static_cast<int>((
sizeof(a) /
sizeof(a[0])) - 1));
469 EXPECT_TRUE(std::is_sorted(std::begin(a), std::end(a)));
474 int a[] = {5, 4, 3, 2, 1, 0, 0, 9};
475 quicksort(a, 0,
static_cast<int>((
sizeof(a) /
sizeof(a[0])) - 1));
476 EXPECT_TRUE(std::is_sorted(std::begin(a), std::end(a)));
481 int a[] = {5, 4, 3, 2, 1, 0, 0, 9};
482 quicksort_rec(a, 0,
static_cast<int>((
sizeof(a) /
sizeof(a[0])) - 1));
483 EXPECT_TRUE(std::is_sorted(std::begin(a), std::end(a)));
488 int a[] = {5, 4, 3, 2, 1, 0, 0, 9};
490 EXPECT_TRUE(std::is_sorted(std::begin(a), std::end(a)));
495 int a[] = {9, 1, 8, 2, 7, 3, 6, 4, 5, 0};
497 EXPECT_TRUE(std::is_sorted(std::begin(a), std::end(a)));
501 EXPECT_TRUE(std::is_sorted(std::begin(b), std::end(b)));
507 int a[] = {5, 4, 3, 2, 1, 0, 0, 9};
508 introsort(a, 0
L,
static_cast<long>((
sizeof(a) /
sizeof(a[0])) - 1));
509 EXPECT_TRUE(std::is_sorted(std::begin(a), std::end(a)));
514 int a[] = {9, 1, 8, 2, 7, 3, 6, 4, 5, 0};
516 EXPECT_TRUE(std::is_sorted(std::begin(a), std::end(a)));
521 int a[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
523 EXPECT_TRUE(std::is_sorted(std::begin(a), std::end(a), std::greater<int>()));
530 for (
size_t i = 1; i < a.size(); ++i)
553 int a[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
555 EXPECT_TRUE(std::is_sorted(std::begin(a), std::end(a)));
560 int a[] = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1};
562 EXPECT_TRUE(std::is_sorted(std::begin(a), std::end(a)));
567 int a[] = {5, 5, 5, 5, 5, 5, 5, 5, 5, 5};
569 EXPECT_TRUE(std::is_sorted(std::begin(a), std::end(a)));
576 const size_t n = 10000;
577 std::vector<int> v(n);
578 for (
size_t i = 0; i < n; ++i)
579 v[i] =
static_cast<int>(n - i);
588 const size_t n = 5000;
591 for (
size_t i = 0; i < n; ++i)
592 a(i) =
static_cast<int>(n - i);
595 for (
size_t i = 1; i < n; ++i)
596 ASSERT_LE(a(i - 1), a(i)) <<
"Failed at index " << i;
602 int a[] = {9, 1, 8, 2, 7, 3, 6, 4, 5, 0};
604 EXPECT_TRUE(std::is_sorted(std::begin(a), std::end(a)));
609 int a[] = {9, 1, 8, 2, 7, 3, 6, 4, 5, 0};
621 int a[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
622 introsort(a, a + 9, std::greater<int>());
623 EXPECT_TRUE(std::is_sorted(std::begin(a), std::end(a), std::greater<int>()));
647 for (
size_t i = 1; i < arr.
size(); ++i)
654 for (
int i = 1; i <= 10; ++i)
659 for (
size_t i = 1; i < arr.
size(); ++i)
681 const size_t n = 5000;
683 for (
size_t i = 0; i < n; ++i)
684 arr.
append(
static_cast<int>(n - i));
688 for (
size_t i = 1; i < n; ++i)
689 ASSERT_LE(arr(i - 1), arr(i)) <<
"Failed at index " << i;
696 for (
size_t i = 1; i < a.size(); ++i)
704 for (
size_t i = 1; i < a.size(); ++i)
719 for (
size_t i = 1; i < a.
size(); ++i)
727 for (
size_t i = 1; i < a.
size(); ++i)
735 for (
size_t i = 1; i < a.
size(); ++i)
810 delete_all_nodes(
out);
883 int a[] = {4, 1, 3, 2, 0, 2};
941 int a[] = {4, 1, 3, 2, 0, 2};
1006 delete_all_nodes(
h);
1013 delete_all_nodes(
h);
1028 delete_all_nodes(
h);
1098 for (
size_t i = 1; i < idx.size(); ++i)
1103 for (
size_t i = 1; i <
ptrs.
size(); ++i)
1169 delete_all_nodes(
h);
1174 int a[] = {4, 1, 3, 2, 0, 2};
1181 idx =
random_search(d, 3, 0,
static_cast<long>(d.size() - 1));
1190 for (
int x : {4, 1, 3, 2, 0, 2})
1216 int b[] = {4, 1, 3};
1218 auto fn =
static_cast<const int & (*)(
int *,
const long,
const long,
const Cmp &)
>(
1238 delete_all_nodes(
h);
1262 int a[] = {0, 2, 4, 6};
1272 int a[] = {0, 2, 4, 6};
1286 int b[] = {4, 1, 3, 2, 0, 2};
1288 auto fn =
static_cast<const int & (*)(
int *,
const long,
const long,
const Cmp &)
>(
1299 for (
size_t i = 0; i < a.size(); ++i)
1327 ptrs.reserve(a.size());
1328 for (
size_t i = 0; i < a.size(); ++i)
1344 ptrs.reserve(a.size());
1345 for (
size_t i = 0; i < a.size(); ++i)
1358 ptrs.reserve(a.size());
1359 for (
size_t i = 0; i < a.size(); ++i)
1379 ptrs.reserve(a.size());
1380 for (
size_t i = 0; i < a.size(); ++i)
1393 ptrs.reserve(a.size());
1394 for (
size_t i = 0; i < a.size(); ++i)
1407 ptrs.reserve(a.size());
1408 for (
size_t i = 0; i < a.size(); ++i)
1423 ptrs.reserve(a.size());
1424 for (
size_t i = 0; i < a.size(); ++i)
static bool is_min_heap(const std::vector< int > &v)
Simple dynamic array with automatic resizing and functional operations.
constexpr size_t size() const noexcept
Return the number of elements stored in the stack.
constexpr bool is_empty() const noexcept
Return true if stack is empty.
T & append(const T &data)
Append a copy of data
bool has_curr() const noexcept
Return true if the iterator has current item.
Doubly linked circular list node.
Iterator on a list of Dnode objects.
Node belonging to a double circular linked list with header node.
Dnode< T > * remove_first_ne() noexcept
Remove the first node and return its address.
size_t size() const noexcept
Return the current dimension of array.
bool is_empty() const noexcept
Return true if the array is empty.
void reserve(const size_t l, const size_t r)
Allocate a range of entries.
Dynamic doubly linked list with O(1) size and bidirectional access.
Dynamic singly linked list with functional programming support.
T & append(const T &item)
Append a new item by copy.
T & get_last() const
Return the last item of the list.
T & get_first() const
Return the first item of the list.
Single linked list of nodes.
constexpr bool is_empty() const noexcept
Return true if list is empty.
size_t size() const noexcept
Count the number of elements of the list.
Comparator wrapper that inverts the comparison order.
Link of a single linked list non-circular and without header node.
constexpr bool is_empty() const noexcept
Return true if this is empty.
Slinknc * remove_next() noexcept
Remove for linked list the node pointed by this
void insert(Slinknc *p) noexcept
Insert p after this
Node belonging to a single non-circular linked list without header node.
iterator end() noexcept
Return an STL-compatible end iterator.
iterator begin() noexcept
Return an STL-compatible iterator to the first element.
int cmp(const __gmp_expr< T, U > &expr1, const __gmp_expr< V, W > &expr2)
Main namespace for Aleph-w library functions.
DynList< size_t > binary_search_dup(const C< T > &a, const T &x, const Compare &cmp=Compare())
long search_min(T *a, const long l, const long r, const Compare &cmp=Compare())
Returns the smallest element of the array a between l and r.
bool is_sorted(const Container< T > &cont, const Compare &cmp=Compare())
Check if a container is sorted in ascending order.
std::pair< bool, size_t > search_inversion(const Container< T > &cont, const Compare &cmp=Compare())
Find the first inversion in a container.
Dnode< T > * dlink_random_search(Dlink &list, const T &x, const Compare &cmp=Compare())
Random search for an element in a dlink list.
DynArray< T * > build_index_ptr(const C< T > &a, const Compare &cmp=Compare())
Builds and returns an array of pointers to the elements of container a sorted according to the compar...
void quicksort_no_tail(T *a, long l, long r, const Compare &cmp=Compare())
long random_search(T *a, const T &x, const long l, const long r, const Compare &cmp=Compare())
Random search for an element in an array.
const int Not_Found
Return value for search functions when element is not found.
std::pair< bool, size_t > test_sorted(const Container< T > &cont, const Compare &cmp=Compare())
Test if a container is sorted, returning the inversion position.
long search_max(T *a, const long l, const long r, const Compare &cmp=Compare())
Returns the maximum element of the array a between l and r.
void mergeinsertsort(Tlist< T > &list, const Compare &cmp=Compare(), const size_t lsz=Aleph::Insertion_Threshold)
Sort a list by mergesort combined with the insert method.
void insert_sorted(Dlink &list, Dlink *p, const Compare &cmp)
Inserts a node orderly into a doubly linked list.
Dlink * dlink_random_select(Dlink &list, const size_t i, const Compare &cmp=Compare())
Random selection of the ith element from a list based about Dlink.
void heapsort(T *array, const size_t n, const Compare &cmp=Compare())
Sort an array using the heapsort algorithm.
void mergesort(T *a, const long l, const long r, std::vector< T > &buf, Compare cmp)
T * bsearch(const C< T > &a, const T &x, const Compare &cmp=Compare())
DynArray< size_t > build_index(const C< T > &a, const Compare &cmp=Compare())
Build an index array for indirect sorting.
static const T & __random_select(T *a, const long i, const long l, const long r, const Compare &cmp)
const T & random_select(DynArray< T > &a, const long i, const Compare &cmp=Compare())
static long back_index(const long i) noexcept
void bubble_sort(DynArray< T > &a, const Compare &cmp=Compare())
Sort a dynamic array using bubble sort.
DynList< T * > bsearch_dup(const C< T > &a, const T &x, const Compare &cmp=Compare())
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.
Itor3 merge(Itor1 source1Beg, Itor1 source1End, Itor2 source2Beg, Itor2 source2End, Itor3 destBeg)
Merge two sorted ranges.
void quicksort_rec_min(T *a, const long l, const long r, const Compare &cmp=Compare())
Sorts an array according to the quicksort method with minimum space consumption.
long sequential_search(T *a, const T &x, const long l, const long r, Equal eq=Equal())
Linear search for an element in an array.
Link * search_extreme(const Link &list, const Compare &cmp)
Find the extreme (minimum or maximum) element in a linked list.
bool binary_search(Itor beg, Itor end, const T &value)
Binary search for a value.
void quicksort_rec(T *a, const long l, const long r, const Compare &cmp=Compare())
Sorts an array using the quicksort method.
DynList< long > binindex_dup(const C< T > &a, const T &x, const Compare &cmp=Compare())
Returns the indices of all occurrences of a value in a sorted container.
bool is_inversely_sorted(const Container< T > &cont, const Compare &cmp=Compare())
Check if a container is sorted in descending order.
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).
long binindex(const C< T > &a, const T &x, const Compare &cmp=Compare())
Returns the index where a value appears (or should be inserted) in a sorted container.
void quicksort_insertion(T *a, const long l, const long r, const Compare &cmp=Compare())
Sorts an array by the improved quicksort method.
long binary_search_rec(T *a, const T &x, const long l, const long r, const Compare &cmp=Compare())
Recursive binary search on an ordered array.
void merge_lists(Tlist &l1, Tlist &l2, Tlist &result, const Compare &cmp=Compare())
Merge two sorted lists into a single sorted list.
DynList< T > maps(const C &c, Op op)
Classic map operation.
void push2(Stack &stack, const A &a, const B &b)
void quicksort_op(C< T > &a, const Compare &cmp=Compare(), const size_t threshold=Quicksort_Threshold)
Optimized quicksort for containers using operator().
Dynamic array container with automatic resizing.
Doubly linked list node with typed data.
Lazy and scalable dynamic array implementation.
Dynamic doubly linked list implementation.
Alias for htlist.H (DynList implementation).
Comprehensive sorting algorithms and search utilities for Aleph-w.