Skip to main content
failed to come up with a good max_2(a)
Source Link
greybeard
  • 7.7k
  • 3
  • 21
  • 56

You can almost halfhalve the number of comparisons to expect for large uniform random input comparing to the runner-up first:

    if x > second_max:
        if x >= max_:
            second_max = max_
            max_ = x
        else:
            second_max = x

To keep with the definition of a product of a single factor, initialise both to 1 - positive should exclude 0.
I guess I'd initialise max_ = a[0] in a premature attempt to further improve run-time, needlessly excluding iterators as input.

You can almost half the number of comparisons to expect comparing to the runner-up first:

    if x > second_max:
        if x >= max_:
            second_max = max_
            max_ = x
        else:
            second_max = x

To keep with the definition of a product of a single factor, initialise both to 1 - positive should exclude 0.
I guess I'd initialise max_ = a[0] in a premature attempt to further improve run-time, needlessly excluding iterators as input.

You can almost halve the number of comparisons to expect for large uniform random input comparing to the runner-up first:

    if x > second_max:
        if x >= max_:
            second_max = max_
            max_ = x
        else:
            second_max = x

To keep with the definition of a product of a single factor, initialise both to 1 - positive should exclude 0.
I guess I'd initialise max_ = a[0] in a premature attempt to further improve run-time, needlessly excluding iterators as input.

added 1 character in body
Source Link
greybeard
  • 7.7k
  • 3
  • 21
  • 56

You can almost half the number of comparisons to expect comparing to the runner-up first:

    if x > second_max:
        if x >>= max_:
            second_max = max_
            max_ = x
        else:
            second_max = x

To keep with the definition of a product of a single factor, initialise both to 1 - positive should exclude 0.
I guess I'd initialise max_ = a[0] in a premature attempt to further improve run-time, needlessly excluding iterators as input.

You can almost half the number of comparisons to expect comparing to the runner-up first:

    if x > second_max:
        if x > max_:
            second_max = max_
            max_ = x
        else:
            second_max = x

To keep with the definition of a product of a single factor, initialise both to 1 - positive should exclude 0.
I guess I'd initialise max_ = a[0] in a premature attempt to further improve run-time, needlessly excluding iterators as input.

You can almost half the number of comparisons to expect comparing to the runner-up first:

    if x > second_max:
        if x >= max_:
            second_max = max_
            max_ = x
        else:
            second_max = x

To keep with the definition of a product of a single factor, initialise both to 1 - positive should exclude 0.
I guess I'd initialise max_ = a[0] in a premature attempt to further improve run-time, needlessly excluding iterators as input.

Source Link
greybeard
  • 7.7k
  • 3
  • 21
  • 56

You can almost half the number of comparisons to expect comparing to the runner-up first:

    if x > second_max:
        if x > max_:
            second_max = max_
            max_ = x
        else:
            second_max = x

To keep with the definition of a product of a single factor, initialise both to 1 - positive should exclude 0.
I guess I'd initialise max_ = a[0] in a premature attempt to further improve run-time, needlessly excluding iterators as input.