195,490 views
24 votes
24 votes
Write a function that receives a StaticArray where the elements are in sorted order, and returns a new StaticArray with squares of the values from the original array, sorted in non-descending order. The original array should not be modified.

User Parth Dave
by
2.7k points

1 Answer

16 votes
16 votes

Answer:

def sa_sort(arr):

for i in range(len(arr)):

for j in range(0, len(arr) - i-1):

if arr[j] > arr[j + 1]:

arr[j], arr[j + 1] = arr[j + 1], arr[j]

test_cases = (

[1, 10, 2, 20, 3, 30, 4, 40, 5], ['zebra2', 'apple', 'tomato', 'apple', 'zebra1'],

[(1, 1), (20, 1), (1, 20), (2, 20)])

for case in test_cases:

print(case)

sa_sort(case)

print(case)

Step-by-step explanation:

User STWilson
by
3.4k points