Home>

I'm sorry for the amateur bare questions.
I want to sort by selection without using sort ().
When I looked up on the Internet, there are many ascending orders, but I don't know how to fix them in descending order.

Applicable source code
void selection_sort (int * array, size_t size)
{
    size_t i, j;
    int min_index;
    for (i = 0;i<size-1;++ i) {
        min_index = i;
        for (j = i + 1;j<size;++ j) {
            if (array [min_index]>array [j]) {
                min_index = j;
            }
        }
        SWAP (int, array [i], array [min_index]);
    }
}

I think I should probably change min to max and reverse the inequality sign, but I think everything else is refreshing.

c++
  • Answer # 1

      

    I think I should probably change min to max and reverse the inequality sign ...

    That's fine, but haven't you corrected it just because you thought?

  • Answer # 2

      

    if (array [min_index]>array [j]) {

    Do you just flip the direction of the inequality sign here?

  • Answer # 3

      

    I don't know how to fix this in descending order.

    There is also a way to avoid remodeling or messing around, such as "leave the sort as it is, and finally reverse it withstd :: reverse".

    (Of course I think it ’s better to be honestly modified.)