Algorithms

Credits- 105 STL Algorithms in Less Than an Hourarrow-up-right

Sort Example

The code examples will probably be given on the test.

1st way - Lambda

2nd way - Functor (Function object)

3rd way - function

Now will sort work on a list or a deque?

Hint: look at the iterator type

Lets Start 🤩

Permutations

Province of Heaps

  • make_heap

  • push_heap

  • pop_heap

Max Heap Max heap

std::make_heap

make_heap - C++ Referencearrow-up-right

std::push_heap

push_heap - C++ Referencearrow-up-right

std::pop_head

pop_heap - C++ Referencearrow-up-right

Heap Sort

sorting using pop_head

Shore of Sorting

  • sort

  • partial_sort

  • nth_element

  • sort_heap

  • inplace_merge

std::sort

sort - C++ Referencearrow-up-right

std::nth_element

nth_element - C++ Referencearrow-up-right

std::inplace_merge

inplace_merge - C++ Referencearrow-up-right

Takes two sorted partitions and combines them to one sorted partition

Partioning

  • std::partition

  • std::partition_point

partition_point - C++ Referencearrow-up-right

Take any element that fulfills the predicate (the blues) and divide into two groups (blues and whites)

The first white is the partition point

Other Permutations

  • std::rotate

  • std::shuffle

  • std::next_permutation

  • std::prev_permutation

rotate - C++ Referencearrow-up-right

shuffle - C++ Referencearrow-up-right

next_permutation - C++ Referencearrow-up-right

prev_permutation - C++ Referencearrow-up-right

Algo combination

Algos we can combine with other algos to create new algos

  • stable

    • stable_sort - std::stable_sort preserves the original order for equal elements, while std::sort doesn't.

    • stable_partition - while partitioning keep relative order

  • is

    • is_sorted

    • is_partitioned

    • is_heap

  • is_until

    • is_sort_until

    • is_partitioned_until

    • is_heap_until

std::stable_sort

stable_sort - C++ Referencearrow-up-right

Here we cast the doubles to ints: int(i)< int(j)

std::is_sorted

is_sorted - C++ Referencearrow-up-right

Returns true if the range is sorted into ascending order.

std::is_sorted_until

is_sorted_until - C++ Referencearrow-up-right

Find first unsorted element in range

Queries

Numerical Algos

  • count

  • accumulate

  • transform

  • reduce (c++17, can run in parrallel)

  • partial_sum

  • inclusive_scan ( can run in parrallel)

  • exclusive_scan

  • inner_product

  • adjacent_difference

  • sample

std::count

count - C++ Referencearrow-up-right

std::accumulate

accumulate - C++ Referencearrow-up-right

sum=i=15i=15sum=\sum_{i=1}^{5} i = 15 x+3y=13+23+33+43+53=x=153x=45x+3*y=1*3 + 2*3 + 3*3 + 4*3 + 5*3=\sum_{x=1}^{5} 3*x=45

std::partial_sum

partial_sum - C++ Referencearrow-up-right

x[i]=n=0ix[n]x[i]=\sum_{n=0}^{i} x[n]

std::sample

std::sample - cppreference.comarrow-up-right

Selects n elements from the sequence without replacement such that each possible sample has equal probability of appearance

or

Querying a property - 1 range

  • all_of

  • any_of

  • none_of

std::any_of

any_of - C++ Referencearrow-up-right

std::all_of

Querying a property - 2 ranges

  • equal

  • lexicographical_compare

  • mismatch

Searching a value

  • unsorted

    • find

    • adjacent_find

  • sorted

    • equal_range

    • lower_bound

    • upper_bound

    • binary_search

Movers

  • move

  • copy

  • swap_ranges

  • move_backward

  • copy_backward

std::copy

Example 2

  • A std::front_insert_iterator which can be used to add elements to the beginning of the container that supports a push_front operation

  • A std::back_insert_iterator which can be used to add elements to the end of the container that supports a push_back operation

std::move

Example 2

Notice the size is 0.

For more about std::move look at R-value references and move constructors.

std::copy_backward

Value Modifiers

  • fill

  • generate

  • iota

  • replace

std::generate

std::generate_n

Structure Changers

  • remove

  • unique

std::remove

std::unique

Notice it didn't get rid of all the duplicates and return a set but rather got rid of each duplicate adjacent to another.

std-unique.jpg

*_COPY

  • remove_copy

  • unique_copy

  • reverse_copy

  • rotate_copy

  • replace_copy

  • partition_copy

  • partial_sort_copy

std::remove_copy

Example 1:

Example 2:

*_IF

  • find_if

  • find_if_not

  • count_if

  • remove_if

  • remove_copy_if

  • replace_if

  • replace_copy_if

  • copy_if

std::count_if

transform

foo.begin(), std::plus: foo is the output and std::plus adds together its two arguments, we could have use a lambda instead

Last updated