In: Computer Science
Write a function that receives a StaticArray and sorts its content in non-descending order. Sorting must be done ‘in place’, meaning the original input array will be modified. You may assume that the input array will contain at least one element and that values stored in the array are all of the same type (either all numbers, or strings, or custom objects, but never a mix of those). You do not need to write checks for these conditions. You can implement any sort method of your choice. Sorting does not have to be very efficient or fast, a simple insertion or bubble sort will suffice. Duplicates in the original array can be placed in any relative order in the sorted array (in other words, your sort does not have to be ‘stable’).
**PYTHON ONLY and no built in python data structures allowed
Example #1:
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:
arr = StaticArray(len(case)) for i, value in enumerate(case):
arr[i] = value print(arr)
sa_sort(arr) print(arr)
Output:
STAT_ARR Size: 9 [1, 10, 2, 20, 3, 30, 4, 40, 5] STAT_ARR Size: 9 [1, 2, 3, 4, 5, 10, 20, 30, 40] STAT_ARR Size: 5 ['zebra2', 'apple', 'tomato', 'apple', 'zebra1'] STAT_ARR Size: 5 ['apple', 'apple', 'tomato', 'zebra1', 'zebra2'] STAT_ARR Size: 4 [(1, 1), (20, 1), (1, 20), (2, 20)] STAT_ARR Size: 4 [(1, 1), (1, 20), (2, 20), (20, 1)]
Thanks for the question. Below is the code you will be needing. Let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please leave a +ve feedback : ) Let me know for any help with any other questions. Thank You! =========================================================================== # bubble sort algorithm implementation 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)
==================================================================