heap algo modification

how can i access each permutation of an array individually found by heap algo?
i.e.,permutations of heap algo for {1,2,3}
generate
1 2 3
2 1 3
3 1 2
1 3 2
2 3 1
3 2 1
so i want to access each array permuatation and perform some operation on it
If the array does not contain duplicate values:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <algorithm>
#include <iterator>
#include <iomanip>

int main()
{
    int array[] { 1, 4, 3, 2 } ;

    // https://en.cppreference.com/w/cpp/algorithm/sort
    // https://en.cppreference.com/w/cpp/iterator/begin
    std::sort( std::begin(array), std::end(array) ) ;

    int cnt = 0 ;
    do
    {
        std::cout << std::setw(5) << ++cnt << ". [ " ;
        // http://www.stroustrup.com/C++11FAQ.html#for
        for( int v : array ) std::cout << v << ' ' ;
        std::cout << "]\n" ;
    }
    while( std::next_permutation( std::begin(array), std::end(array) ) ) ;
    // https://en.cppreference.com/w/cpp/algorithm/next_permutation
}

http://coliru.stacked-crooked.com/a/1067e36caa029666
I'm not sure that's what was asked.

A "heap" is just an array that is accessed in rows of increasing length. (For a binary heap, each row has twice as many elements as the previous.)

Whenever you perform a push down, just print the elements of the array. You can print every time there is a swap, or every time a complete push down is done.

For terminology, each time you modify the heap, you are "permuting" it.
Topic archived. No new replies allowed.