find the min of list using O(n log n) [closed]
up vote
-2
down vote
favorite
Working on the efficiency of the Big-O notation i find myself in a series of doubts with the elaboration of a type of code that works in time O(n log n)
, to perform the next task:
· find the minimum number of a list (the order of the factors is not important)
· alternative to using... min method
I know that the in this magnitude means time goes up linearly while the n
goes up exponentially (thanks to other users)
To understand it this magnitude, for exapmle i have this code.
def x(my_list):
n = len(my_list)
print(my_list)
if n <= 1:
print("return")
return 0
return x(my_list[:n // 2]) + x(my_list[n // 2:])
print(x([2, 3, 4, 5, 6, 7]))
Also works with lists, but it does not do what i want.
This returns once for each element in the list O(n)
...It then also implements a bisection search O(log n) == O(n log n)
Thanks :)
python algorithm big-o
closed as unclear what you're asking by Netwave, MrSmith42, Goyo, gsamaras, meowgoesthedog Nov 19 at 13:42
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
add a comment |
up vote
-2
down vote
favorite
Working on the efficiency of the Big-O notation i find myself in a series of doubts with the elaboration of a type of code that works in time O(n log n)
, to perform the next task:
· find the minimum number of a list (the order of the factors is not important)
· alternative to using... min method
I know that the in this magnitude means time goes up linearly while the n
goes up exponentially (thanks to other users)
To understand it this magnitude, for exapmle i have this code.
def x(my_list):
n = len(my_list)
print(my_list)
if n <= 1:
print("return")
return 0
return x(my_list[:n // 2]) + x(my_list[n // 2:])
print(x([2, 3, 4, 5, 6, 7]))
Also works with lists, but it does not do what i want.
This returns once for each element in the list O(n)
...It then also implements a bisection search O(log n) == O(n log n)
Thanks :)
python algorithm big-o
closed as unclear what you're asking by Netwave, MrSmith42, Goyo, gsamaras, meowgoesthedog Nov 19 at 13:42
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
add a comment |
up vote
-2
down vote
favorite
up vote
-2
down vote
favorite
Working on the efficiency of the Big-O notation i find myself in a series of doubts with the elaboration of a type of code that works in time O(n log n)
, to perform the next task:
· find the minimum number of a list (the order of the factors is not important)
· alternative to using... min method
I know that the in this magnitude means time goes up linearly while the n
goes up exponentially (thanks to other users)
To understand it this magnitude, for exapmle i have this code.
def x(my_list):
n = len(my_list)
print(my_list)
if n <= 1:
print("return")
return 0
return x(my_list[:n // 2]) + x(my_list[n // 2:])
print(x([2, 3, 4, 5, 6, 7]))
Also works with lists, but it does not do what i want.
This returns once for each element in the list O(n)
...It then also implements a bisection search O(log n) == O(n log n)
Thanks :)
python algorithm big-o
Working on the efficiency of the Big-O notation i find myself in a series of doubts with the elaboration of a type of code that works in time O(n log n)
, to perform the next task:
· find the minimum number of a list (the order of the factors is not important)
· alternative to using... min method
I know that the in this magnitude means time goes up linearly while the n
goes up exponentially (thanks to other users)
To understand it this magnitude, for exapmle i have this code.
def x(my_list):
n = len(my_list)
print(my_list)
if n <= 1:
print("return")
return 0
return x(my_list[:n // 2]) + x(my_list[n // 2:])
print(x([2, 3, 4, 5, 6, 7]))
Also works with lists, but it does not do what i want.
This returns once for each element in the list O(n)
...It then also implements a bisection search O(log n) == O(n log n)
Thanks :)
python algorithm big-o
python algorithm big-o
edited Nov 20 at 6:44
asked Nov 19 at 11:07
JULIO R
145
145
closed as unclear what you're asking by Netwave, MrSmith42, Goyo, gsamaras, meowgoesthedog Nov 19 at 13:42
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
closed as unclear what you're asking by Netwave, MrSmith42, Goyo, gsamaras, meowgoesthedog Nov 19 at 13:42
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
up vote
0
down vote
accepted
Answer to the question that is asked in the title:
def min_of(lst):
if len(lst) <= 2:
return lst[0] if lst[0] < lst[-1] else lst[-1]
a = min_of(lst[len(lst)/2:])
b = min_of(lst[:len(lst)/2])
return a if a < b else b
UPDATE
Sorry, this is O(n) method
1
This is O(n) method
– MBo
Nov 19 at 13:36
add a comment |
up vote
0
down vote
Is your list sorted? Then to find the minimum value it's just the first element of the list: my_list[0]
. This is O(1) (constant time - no looping required).
Is your list not sorted? Then you'll have to scan through every single value in the list to determine which one is the smallest compared to the rest. This is at minimum O(n) (note that O(n) is more efficient than O(nlogn)).
current_min = my_list[0]
for element in my_list:
if element < current_min:
current_min = element
Of course, the options you propose will fulfill the objective, but i´m working about Big-o notation and i want to understand that kind of magnitude. Otherwise I would have done that. Thanks your iterator code, it helps me a lot.
– JULIO R
Nov 20 at 10:53
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
Answer to the question that is asked in the title:
def min_of(lst):
if len(lst) <= 2:
return lst[0] if lst[0] < lst[-1] else lst[-1]
a = min_of(lst[len(lst)/2:])
b = min_of(lst[:len(lst)/2])
return a if a < b else b
UPDATE
Sorry, this is O(n) method
1
This is O(n) method
– MBo
Nov 19 at 13:36
add a comment |
up vote
0
down vote
accepted
Answer to the question that is asked in the title:
def min_of(lst):
if len(lst) <= 2:
return lst[0] if lst[0] < lst[-1] else lst[-1]
a = min_of(lst[len(lst)/2:])
b = min_of(lst[:len(lst)/2])
return a if a < b else b
UPDATE
Sorry, this is O(n) method
1
This is O(n) method
– MBo
Nov 19 at 13:36
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
Answer to the question that is asked in the title:
def min_of(lst):
if len(lst) <= 2:
return lst[0] if lst[0] < lst[-1] else lst[-1]
a = min_of(lst[len(lst)/2:])
b = min_of(lst[:len(lst)/2])
return a if a < b else b
UPDATE
Sorry, this is O(n) method
Answer to the question that is asked in the title:
def min_of(lst):
if len(lst) <= 2:
return lst[0] if lst[0] < lst[-1] else lst[-1]
a = min_of(lst[len(lst)/2:])
b = min_of(lst[:len(lst)/2])
return a if a < b else b
UPDATE
Sorry, this is O(n) method
edited Nov 19 at 14:37
answered Nov 19 at 11:57
Andrey Suglobov
1248
1248
1
This is O(n) method
– MBo
Nov 19 at 13:36
add a comment |
1
This is O(n) method
– MBo
Nov 19 at 13:36
1
1
This is O(n) method
– MBo
Nov 19 at 13:36
This is O(n) method
– MBo
Nov 19 at 13:36
add a comment |
up vote
0
down vote
Is your list sorted? Then to find the minimum value it's just the first element of the list: my_list[0]
. This is O(1) (constant time - no looping required).
Is your list not sorted? Then you'll have to scan through every single value in the list to determine which one is the smallest compared to the rest. This is at minimum O(n) (note that O(n) is more efficient than O(nlogn)).
current_min = my_list[0]
for element in my_list:
if element < current_min:
current_min = element
Of course, the options you propose will fulfill the objective, but i´m working about Big-o notation and i want to understand that kind of magnitude. Otherwise I would have done that. Thanks your iterator code, it helps me a lot.
– JULIO R
Nov 20 at 10:53
add a comment |
up vote
0
down vote
Is your list sorted? Then to find the minimum value it's just the first element of the list: my_list[0]
. This is O(1) (constant time - no looping required).
Is your list not sorted? Then you'll have to scan through every single value in the list to determine which one is the smallest compared to the rest. This is at minimum O(n) (note that O(n) is more efficient than O(nlogn)).
current_min = my_list[0]
for element in my_list:
if element < current_min:
current_min = element
Of course, the options you propose will fulfill the objective, but i´m working about Big-o notation and i want to understand that kind of magnitude. Otherwise I would have done that. Thanks your iterator code, it helps me a lot.
– JULIO R
Nov 20 at 10:53
add a comment |
up vote
0
down vote
up vote
0
down vote
Is your list sorted? Then to find the minimum value it's just the first element of the list: my_list[0]
. This is O(1) (constant time - no looping required).
Is your list not sorted? Then you'll have to scan through every single value in the list to determine which one is the smallest compared to the rest. This is at minimum O(n) (note that O(n) is more efficient than O(nlogn)).
current_min = my_list[0]
for element in my_list:
if element < current_min:
current_min = element
Is your list sorted? Then to find the minimum value it's just the first element of the list: my_list[0]
. This is O(1) (constant time - no looping required).
Is your list not sorted? Then you'll have to scan through every single value in the list to determine which one is the smallest compared to the rest. This is at minimum O(n) (note that O(n) is more efficient than O(nlogn)).
current_min = my_list[0]
for element in my_list:
if element < current_min:
current_min = element
answered Nov 19 at 11:22
TerryA
42.6k87799
42.6k87799
Of course, the options you propose will fulfill the objective, but i´m working about Big-o notation and i want to understand that kind of magnitude. Otherwise I would have done that. Thanks your iterator code, it helps me a lot.
– JULIO R
Nov 20 at 10:53
add a comment |
Of course, the options you propose will fulfill the objective, but i´m working about Big-o notation and i want to understand that kind of magnitude. Otherwise I would have done that. Thanks your iterator code, it helps me a lot.
– JULIO R
Nov 20 at 10:53
Of course, the options you propose will fulfill the objective, but i´m working about Big-o notation and i want to understand that kind of magnitude. Otherwise I would have done that. Thanks your iterator code, it helps me a lot.
– JULIO R
Nov 20 at 10:53
Of course, the options you propose will fulfill the objective, but i´m working about Big-o notation and i want to understand that kind of magnitude. Otherwise I would have done that. Thanks your iterator code, it helps me a lot.
– JULIO R
Nov 20 at 10:53
add a comment |