Z
Z
zlodiak2019-02-19 22:27:05
Python
zlodiak, 2019-02-19 22:27:05

Which algorithm is more efficient in finding the minimum?

There are two algorithms for finding the minimum value in an unordered list.

#!/usr/bin/env python3

def find_smallest_int(arr):
    min = arr[0]
    for i in arr:
        if i < min:
            min = i
    return min

#!/usr/bin/env python3

def findSmallestInt(arr):
    arr.sort()
    return arr[0]

Please help to estimate their complexity in O-notation.
In my opinion, the complexity of the first algorithm is O(N). I find it difficult to determine the complexity of the second algorithm.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Astrohas, 2019-02-19
@zlodiak

first O(N)
second N*log(n) * C
for searching for one minimum, the first one is good
, and for multiple searches, the second one is good, paired with a banal binary search

G
GavriKos, 2019-02-19
@GavriKos

The first one is correct.
With the second - you need to know what kind of sorting python uses and watch its complexity.

B
BorLaze, 2019-02-19
@BorLaze

The task is to evaluate the complexity of algorithms or really find out "which algorithm is more efficient in finding a minimum"?
Because in the given examples it is an attempt to compare "warm with soft".
The first is REALLY looking for a minimum.
The second one REALLY sorts the array, and getting the minimum is a by-product.
Do you catch the difference?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question