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:)
Sorting Benchmarks Write a program that generates and sorts an array of a user-specified number (arraySize)...
Sorting Benchmarks Write a program that generates and sorts an array of a user-specified number (arraySize) of randomly generated numbers. To keep the values to a reasonable range by using the array size as the upper bound for the random numbers (between 1 and arraySize). Your program should call the individual functions that implement the five sorting algorithms discussed in class (see the lecture slides). Each function should keep a count of the number of comparisons/exchanges it makes. Display the...
8) Sorting - C++ You have this function that sorts any vector of char data: void...
8) Sorting - C++ You have this function that sorts any vector of char data: void good_bubble(vector<char> & data, vector<char>::size_type start, vector<char>::size_type end) { vector<char>::size_type loop{0}, cur; bool done{false}; while (loop <= end-start+1 && !done) { done = true; for (cur = start; cur <= end-1-loop; ++cur) { if (data[cur] > data[cur+1]) { swap(data[cur], data[cur+1]); done = false; } } ++loop; } return; } But now you have to sort Date objects! As luck would have it, the Date class...
Write a Java program that sorts an array of “Student” in an aescending order of their...
Write a Java program that sorts an array of “Student” in an aescending order of their “last names”. The program should be able to apply (Insertion sort): Student [] studs = new Student[8]; s[0] = new Student("Saoud", "Mohamed", 3.61); s[1] = new Student("Abdelkader", "Farouk", 2.83); s[2] = new Student("Beshr" , "Alsharqawy", 1.99); s[3] = new Student("Nader", "Salah", 3.02); s[4] = new Student("Basem", "Hawary", 2.65); s[5] = new Student("Abdullah", "Babaker", 2.88); s[6] = new Student("Abdelaal", "Khairy", 3.13); s[7] = new Student("Mohamedain",...
Write and test a Python program to print a set of real numbers in descending order....
Write and test a Python program to print a set of real numbers in descending order. The program should also print out the median of the numbers input and how many numbers were input. The program should read in numbers until a negative number is read. The negative number serves as a sentinel or marker telling the program when to stop reading numbers. The program should then sort the numbers and print them out in order from largest to smallest....
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[ ] ).
Sorting (merge) Sort the array (as in Part 2) using merge sort. Write the array content...
Sorting (merge) Sort the array (as in Part 2) using merge sort. Write the array content after each pass (i.e., from pass 1 to pass 9). Each pass is defined as the completion of one merge operation. Suppose an array contains the following integers: -1, 20, 10, -5, 0, -7, 100, -7, 30, -10. Sort the array using the following algorithms: selection sort, bubble sort, and insertion sort. For each algorithm, write the array content after each pass (i.e., from...
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...
***C++ Coding*** Write a program for sorting a list of integers in ascending order using the...
***C++ Coding*** Write a program for sorting a list of integers in ascending order using the bubble sort algorithm. Please include comments to understand code. Requirements Implement the following functions: int readData( int **arr) arr is a pointer to pointer for storing the integers. The function returns the number of integers. The function readData reads the list of integers from a file call data.txt into the array arr. The first integer number in the file is the number of intergers....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT