Question

In: Computer Science

Write a function that receives a StaticArray and sorts its content in non-descending order. Sorting must...

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)]

Solutions

Expert Solution

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)

==================================================================


Related Solutions

Function 6: Sorting string function name (str) a. This function receives an string. Your task is...
Function 6: Sorting string function name (str) a. This function receives an string. Your task is loop through the each character of the string and separate all digists/letters/special characters. Display all digits at the beginning, followed by letters then the special chars and display result in console. For example if following string is passed to this function “ha1m2i3:n)” Result should be: 123hamin:)
Write a function in C that uses the Merge Sort sorting algorithm with arrays. The function...
Write a function in C that uses the Merge Sort sorting algorithm with arrays. The function must not be void and must output type int* i.e. it must take the form: int* merge_sort(int a[], int n) where a[ ] is the input matrix and n is the size of the matrix. You may use an auxiliary functions such as "merge." The returned array should be sorted using merge_sort and should not modify the array that was input (a[ ] ).
Write a program that sorts prices of 10 tacos in ascending order based on the price,...
Write a program that sorts prices of 10 tacos in ascending order based on the price, using arrays. Requirements: The user enters the name of the taco and then the price of the taco HINT: Two arrays make this problem simpler. One with the names and the other with the prices. The indices indicate the combination. For instance, a taco price at index 5 has its name also at index 5 of the other array. HINT: It is a good...
Just need the desktop test for this two function a) Write a function that receives the...
Just need the desktop test for this two function a) Write a function that receives the number of terms (n) and returns a container with the first n TunaPoke numbers. Present a "print screen" of the results and their respective desktop test when the function is invoked with the 16. (b) Write a function that receives a limit number and returns a container with the first numbers TunaPoke that do not exceed the specified limit. Present a "print screen" of...
Write a method that takes an integer array as its parameter and sorts the contents of...
Write a method that takes an integer array as its parameter and sorts the contents of the array in ascending order using the Insertion Sort algorithm. Call this method after the original array and other stats have been displayed. Once the array has been sorted by your method, display its contents to the screen in the same manner as the original array was displayed. CODE SO FAR: import java.util.*; public class ArrayInteger { public static void getRandomNumber(int A[],int n){ Random...
C++ Write the implementation of the function concatenateIntArrays. This function receives 4 parameters in the following...
C++ Write the implementation of the function concatenateIntArrays. This function receives 4 parameters in the following order: An array of integer values (array1). An integer representing the size of array1 (size1). An array of integer values (array2). An integer representing the size of array2 (size). The function creates a dynamic array of integers of size size1+size2 to store all the values in array1, followed by all the values in array2. The function returns the pointer used to create the dynamic...
In PYTHON: Write a function that receives a sentence and returns the last word of that...
In PYTHON: Write a function that receives a sentence and returns the last word of that sentence. You may assume that there is exactly one space between every two words, and that there are no other spaces at the sentence. To make the problem simpler, you may assume that the sentence contains no hyphens, and you may return the word together with punctuation at its end.
"sum_between" function Write a function named "sum_between" that receives 2 parameters - "start" (an int) and...
"sum_between" function Write a function named "sum_between" that receives 2 parameters - "start" (an int) and "end" (an int). It should return the sum (total) of all of the integers between (and including) "start" and "end". If "end" is less than "start", the function should return -1 instead. e.g. if you give the function a start of 10 and an end of 15, it should return 75 (i.e. 10+11+12+13+14+15)
A tree can be considered as a structured graph. Write a C++ function that receives a...
A tree can be considered as a structured graph. Write a C++ function that receives a BST object and returns its corresponding graph representation, implemented via an adjacency list. What will be the impact on the search complexity?
Write a function that receives a StaticArray with integers and returns a new StaticArray object with...
Write a function that receives a StaticArray with integers and returns a new StaticArray object with the content from the original array, modified as follows: 1) If the number in the original array is divisible by 3, the corresponding element in the new array should be a string ‘fizz’. 2) If the number in the original array is divisible by 5, the corresponding element in the new array should be a string ‘buzz’. 3) If the number in the original...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT