# Write a function that takes in an array of at least three integers and, without
# sorting the input array, returns a sorted array of the three largest integers
# in the input array.
# The function should return duplicate integers if necessary; for example, it
# should return [10, 10, 12] for an input array of [10, 5, 9, 10, 12].
# Sample Input
# array = [141, 1, 17, -7, -17, -27, 18, 541, 8, 7, 7]
# Sample Output
# [18, 141, 541]
def findThreeLargestNumbers(arr):
result = [float('-inf'), float('-inf'), float('-inf')]
for item in arr:
for idx in range(2, -1, -1):
if result[idx] < item:
shifting_insert(result, idx, item)
break
return result
def shifting_insert(arr, idx, item):
for i in range(idx):
arr[i] = arr[i + 1]
arr[idx] = item
print(findThreeLargestNumbers([141, 1, 17, -7, -17, -27, 18, 541, 8, 7, 7]))
To embed this program on your website, copy the following code and paste it into your website's HTML: