Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

How to find the three largest values of an array efficiently, in Python?

"""
This implementation finds the three largest
numbers in an array in an efficient manner.

The idea is to maintain a subarray holding
the three largest values encountered so 
far. When the algorithm concludes that 
subarray would end up holding the desired
output value (i.e., 3 largest values of 
main array).

Let n be the size of the array
Time complexity: O(n)
Space complexity: O(1)
"""
def find_three_largest_values(arr):
    # Container for 3 largest values
    result = [None]*3
    # Make sure result holds always
    # 3 largest values encountered so far
    for val in arr:
        update_result(result, val)
    return result

def update_result(result, val):
    # Insert val into appropriate position
    # Only vals of interest are those bigger
    # than values already in result array.
    if result[2] is None or val > result[2]:
        update_and_shift(result, val, 2)
    elif result[1] is None or val > result[1]:
        update_and_shift(result, val, 1)
    elif result[0] is None or val > result[0]:
        update_and_shift(result, val, 0)

def update_and_shift(result, val, idx):
    for i in range(idx+1):
        if i == idx:
            result[i] = val
        else:
            result[i] = result[i+1]

arr = [1, 2, 3, 7, 8, 9]
print(find_three_largest_values(arr))  # [7, 8, 9]
Comment

python find in largest 3 numbers in an array

array = [1, 2, 3, 4, 5, 6, 7, 8, 9, ]
result = []
num = int(input('Enter N: '))

for x in range(0, num):
    largeNum = 0
    for y in range(len(array)):
        if array[y] > largeNum:
            largeNum = array[y]
    array.remove(largeNum)
    result.append(largeNum)

print(result)
Comment

PREVIOUS NEXT
Code Example
Python :: convert hex to decimal python 
Python :: flask make static directory 
Python :: python opencv open camera 
Python :: python print dict new line 
Python :: remove duplicates based on two columns in dataframe 
Python :: embed_author discord.py 
Python :: convert array to dataframe python 
Python :: reset index with pandas 
Python :: get index of list item in loop 
Python :: how to reapete the code in python 
Python :: python get object attribute by string 
Python :: python know the number of a loop 
Python :: codeforces 677a python solution 
Python :: python check list contains another list 
Python :: get information about dataframe 
Python :: django queryset group by sum 
Python :: random forest cross validation python 
Python :: write list of dicts to csv python 
Python :: split list in 3 part 
Python :: forbidden (csrf cookie not set.) django rest framework 
Python :: python previous answer 
Python :: django sort queryset 
Python :: how to subtract dates in python 
Python :: where to import reverse_lazy in django 
Python :: get request header flask 
Python :: python to golang 
Python :: convert set to list python time complexity 
Python :: print fibonacci series in reverse in python 
Python :: access-control-allow-origin django 
Python :: python numpy arrays equal 
ADD CONTENT
Topic
Content
Source link
Name
7+8 =