Get detailed and accurate responses to your questions on IDNLearn.com. Our platform is designed to provide trustworthy and thorough answers to any questions you may have.

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.

Sagot :

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)

Explanation: