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 :: How to do an infinte while in python 
Python :: anaconda snake 
Python :: what value do we get from NULL database python 
Python :: how to reduce width of image in pygame 
Python :: how to load wav file with python 
Python :: pyspark split dataframe by rows 
Python :: python get file path from in os.walk 
Python :: python datetime module 
Python :: python check string not exist in array 
Python :: pandas plot move legend 
Python :: how to clear the screen of the terminal using python os 
Python :: Python function to calculate LCM of 2 numbers. 
Python :: python get last element of iterator 
Python :: permutations of a set 
Python :: how can i make a list of leftovers that are str to make them int in python 
Python :: how to delete file in python 
Python :: scikit learn lda 
Python :: count frequency of characters in string 
Python :: how to convert cost to float in python 
Python :: one-line for loop python 
Python :: input and ouput array in python 
Python :: override python print for class 
Python :: read files and write into another files python 
Python :: colorbar min max matplotlib 
Python :: reportlab page size a4 
Python :: decimal in python 
Python :: np.random 
Python :: np.polyfit plot 
Python :: pandas drop column in dataframe 
Python :: from array to tuple python 
ADD CONTENT
Topic
Content
Source link
Name
8+8 =